]> source.dussan.org Git - poi.git/commitdiff
add back some new code
authorPJ Fanning <fanningpj@apache.org>
Mon, 15 Mar 2021 20:24:22 +0000 (20:24 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 15 Mar 2021 20:24:22 +0000 (20:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1887685 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/POIFSStream.java

index 2b1b5621ab2673b3c1331e0c38b378bf614ef8c1..f34d4da6abf838dd1df1e7c42e479f5cf6b45bd2 100644 (file)
@@ -96,6 +96,15 @@ public class POIFSStream implements Iterable<ByteBuffer>
         return new StreamBlockByteBufferIterator(startBlock);
     }
 
+    Iterator<Integer> 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<ByteBuffer>
         this.startBlock = POIFSConstants.END_OF_CHAIN;
     }
 
+    /**
+     * Class that handles a streaming read of one stream
+     */
+    private class StreamBlockOffsetIterator implements Iterator<Integer> {
+        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
      */