don't move backwards while scanning the archive
@@ -233,9 +233,8 @@ private TarArchiveEntry getNextTarEntry() throws IOException {
if (currEntry != null) {
// Skip to the end of the entry
- archive.position(currEntry.getDataOffset() + currEntry.getSize());
+ repositionForwardTo(currEntry.getDataOffset() + currEntry.getSize());
throwExceptionIfPositionIsNotInArchive();
-
skipRecordPadding();
}
@@ -513,11 +512,23 @@ private void skipRecordPadding() throws IOException {
if (!isDirectory() && currEntry.getSize() > 0 && currEntry.getSize() % recordSize != 0) {
final long numRecords = (currEntry.getSize() / recordSize) + 1;
final long padding = (numRecords * recordSize) - currEntry.getSize();
- archive.position(archive.position() + padding);
+ repositionForwardBy(padding);
throwExceptionIfPositionIsNotInArchive();
}
}
+ private void repositionForwardTo(final long newPosition) throws IOException {
+ final long currPosition = archive.position();
+ if (newPosition < currPosition) {
+ throw new IOException("trying to move backwards inside of the archive");
+ }
+ archive.position(newPosition);
+ }
+
+ private void repositionForwardBy(final long offset) throws IOException {
+ repositionForwardTo(archive.position() + offset);
+ }
+
/**
* Checks if the current position of the SeekableByteChannel is in the archive.
* @throws IOException If the position is not in the archive
@@ -586,7 +597,7 @@ private void tryToConsumeSecondEOFRecord() throws IOException {
private void consumeRemainderOfLastBlock() throws IOException {
final long bytesReadOfLastBlock = archive.position() % blockSize;
if (bytesReadOfLastBlock > 0) {
- archive.position(archive.position() + blockSize - bytesReadOfLastBlock);
+ repositionForwardBy(blockSize - bytesReadOfLastBlock);
}
}
