diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2013-04-14 20:24:14 +0300 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2013-04-14 20:24:14 +0300 |
commit | 1ea440146d98f164339f89b44a069b8af3323191 (patch) | |
tree | 8ef48c1d0a9ca51acf7c1b0c4aeee39803fd8e86 /src/org/tukaani | |
parent | 1c13405af44f8791307e3c7165b169d8499425e0 (diff) | |
download | xz-java-1ea440146d98f164339f89b44a069b8af3323191.tar.gz |
Add getBlockNumber to SeekableXZInputStream.
Diffstat (limited to 'src/org/tukaani')
-rw-r--r-- | src/org/tukaani/xz/SeekableXZInputStream.java | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/src/org/tukaani/xz/SeekableXZInputStream.java b/src/org/tukaani/xz/SeekableXZInputStream.java index 3979818..45106e4 100644 --- a/src/org/tukaani/xz/SeekableXZInputStream.java +++ b/src/org/tukaani/xz/SeekableXZInputStream.java @@ -515,6 +515,21 @@ public class SeekableXZInputStream extends SeekableInputStream { } /** + * Gets the number of the Block that contains the byte at the given + * uncompressed position. + * + * @throws IndexOutOfBoundsException if + * <code>pos < 0</code> or + * <code>pos >= length()</code>. + * + * @since 1.3 + */ + public int getBlockNumber(long pos) { + locateBlockByPos(queriedBlockInfo, pos); + return queriedBlockInfo.blockNumber; + } + + /** * Decompresses the next byte from this input stream. * * @return the next decompressed byte, or <code>-1</code> @@ -736,22 +751,8 @@ public class SeekableXZInputStream extends SeekableInputStream { endReached = false; - // Locate the Stream that contains the uncompressed target position. - IndexDecoder index; - for (int i = 0; ; ++i) { - index = (IndexDecoder)streams.get(i); - if (index.hasUncompressedOffset(seekPos)) - break; - } - // Locate the Block that contains the uncompressed target position. - index.locateBlock(curBlockInfo, seekPos); - - assert (curBlockInfo.compressedOffset & 3) == 0; - assert curBlockInfo.uncompressedSize > 0; - assert seekPos >= curBlockInfo.uncompressedOffset; - assert seekPos < curBlockInfo.uncompressedOffset - + curBlockInfo.uncompressedSize; + locateBlockByPos(curBlockInfo, seekPos); // Seek in the underlying stream and create a new Block decoder // only if really needed. We can skip it if the current position @@ -791,6 +792,31 @@ public class SeekableXZInputStream extends SeekableInputStream { } /** + * Locates the Block that contains the given uncompressed position. + */ + private void locateBlockByPos(BlockInfo info, long pos) { + if (pos < 0 || pos >= uncompressedSize) + throw new IndexOutOfBoundsException( + "Invalid uncompressed position: " + pos); + + // Locate the Stream that contains the target position. + IndexDecoder index; + for (int i = 0; ; ++i) { + index = (IndexDecoder)streams.get(i); + if (index.hasUncompressedOffset(pos)) + break; + } + + // Locate the Block from the Stream that contains the target position. + index.locateBlock(info, pos); + + assert (info.compressedOffset & 3) == 0; + assert info.uncompressedSize > 0; + assert pos >= info.uncompressedOffset; + assert pos < info.uncompressedOffset + info.uncompressedSize; + } + + /** * Locates the given Block and stores information about it * to <code>info</code>. */ |