From 7359d4cee7c382692ce23caf8f0bfb4f55d30972 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 15 Mar 2021 20:24:22 +0000 Subject: [PATCH] add back some new code git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1887685 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/POIFSStream.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java b/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java index 2b1b5621ab..f34d4da6ab 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSStream.java @@ -96,6 +96,15 @@ public class POIFSStream implements Iterable return new StreamBlockByteBufferIterator(startBlock); } + Iterator getBlockOffsetIterator() { + if(startBlock == POIFSConstants.END_OF_CHAIN) { + throw new IllegalStateException( + "Can't read from a new stream before it has been written to" + ); + } + return new StreamBlockOffsetIterator(startBlock); + } + /** * Updates the contents of the stream to the new * set of bytes. @@ -137,6 +146,42 @@ public class POIFSStream implements Iterable this.startBlock = POIFSConstants.END_OF_CHAIN; } + /** + * Class that handles a streaming read of one stream + */ + private class StreamBlockOffsetIterator implements Iterator { + private final ChainLoopDetector loopDetector; + private int nextBlock; + + StreamBlockOffsetIterator(int firstBlock) { + this.nextBlock = firstBlock; + try { + this.loopDetector = blockStore.getChainLoopDetector(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public boolean hasNext() { + return nextBlock != POIFSConstants.END_OF_CHAIN; + } + + public Integer next() { + if (!hasNext()) { + throw new NoSuchElementException("Can't read past the end of the stream"); + } + + loopDetector.claim(nextBlock); + int currentBlock = nextBlock; + nextBlock = blockStore.getNextBlock(nextBlock); + return currentBlock; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + } + /** * Class that handles a streaming read of one stream */ -- 2.39.5