This is an automated email from the ASF dual-hosted git repository. peterlee pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 9f7c5aa03075bda181a7a06ea47d110665979eb8 Author: theobisproject <[email protected]> AuthorDate: Fri Jul 24 10:21:07 2020 +0200 COMPRESS-540: Implement storage of data offset in tar entries via EntryStreamOffsets instead of reinventing it --- .../compress/archivers/tar/TarArchiveEntry.java | 38 +++++++++++++--------- .../commons/compress/archivers/tar/TarFile.java | 12 +++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java index d4eb2ed..37e76c9 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java @@ -40,6 +40,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.EntryStreamOffsets; import org.apache.commons.compress.archivers.zip.ZipEncoding; import org.apache.commons.compress.utils.ArchiveUtils; import org.apache.commons.compress.utils.IOUtils; @@ -158,8 +159,7 @@ import org.apache.commons.compress.utils.IOUtils; * @NotThreadSafe */ -public class TarArchiveEntry implements ArchiveEntry, TarConstants { - +public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamOffsets { private static final TarArchiveEntry[] EMPTY_TAR_ARCHIVE_ENTRIES = new TarArchiveEntry[0]; /** @@ -256,7 +256,7 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants { /** Convert millis to seconds */ public static final int MILLIS_PER_SECOND = 1000; - private long dataPosition = -1; + private long dataOffset = EntryStreamOffsets.OFFSET_UNKNOWN; /** * Construct an empty entry and prepares the header values. @@ -560,15 +560,15 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants { * @param encoding encoding to use for file names * @param lenient when set to true illegal values for group/userid, mode, device numbers and timestamp will be * ignored and the fields set to {@link #UNKNOWN}. When set to false such illegal fields cause an exception instead. - * @param dataPosition Position of the entry data in the random access file + * @param dataOffset Position of the entry data in the random access file * @since 1.21 * @throws IllegalArgumentException if any of the numeric fields have an invalid format * @throws IOException on error */ public TarArchiveEntry(final byte[] headerBuf, final ZipEncoding encoding, final boolean lenient, - final long dataPosition) throws IOException { + final long dataOffset) throws IOException { this(headerBuf, encoding, lenient); - this.dataPosition = dataPosition; + this.dataOffset = dataOffset; } /** @@ -1203,22 +1203,30 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants { } /** - * Data position of the archive entry in a random access tar - * @return position of the data in the tar file. If the entry is created from a stream and therefore the data - * position is unknown this will return -1. + * {@inheritDoc} + * @since 1.21 + */ + @Override + public long getDataOffset() { + return dataOffset; + } + + /** + * Set the offset of the data for the tar entry. + * @param dataOffset the position of the data in the tar * @since 1.21 */ - public long getDataPosition() { - return dataPosition; + public void setDataOffset(final long dataOffset) { + this.dataOffset = dataOffset; } /** - * Set the position of the data for the tar entry. - * @param dataPosition the position of the data in the tar + * {@inheritDoc} * @since 1.21 */ - public void setDataPosition(final long dataPosition) { - this.dataPosition = dataPosition; + @Override + public boolean isStreamContiguous() { + return true; } /** diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java index 714ff8a..c33ba2e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java @@ -233,7 +233,7 @@ public class TarFile implements Closeable { if (currEntry != null) { // Skip to the end of the entry - archive.position(currEntry.getDataPosition() + currEntry.getSize()); + archive.position(currEntry.getDataOffset() + currEntry.getSize()); skipRecordPadding(); } @@ -315,7 +315,7 @@ public class TarFile implements Closeable { } entry = new TarArchiveSparseEntry(headerBuf.array()); currEntry.getSparseHeaders().addAll(entry.getSparseHeaders()); - currEntry.setDataPosition(currEntry.getDataPosition() + recordSize); + currEntry.setDataOffset(currEntry.getDataOffset() + recordSize); } while (entry.isExtended()); } @@ -373,7 +373,7 @@ public class TarFile implements Closeable { // only store the input streams with non-zero size if (sparseHeader.getNumbytes() > 0) { long start = - currEntry.getDataPosition() + sparseHeader.getOffset() - numberOfZeroBytesInSparseEntry; + currEntry.getDataOffset() + sparseHeader.getOffset() - numberOfZeroBytesInSparseEntry; streams.add(new BoundedSeekableByteChannelInputStream(start, sparseHeader.getNumbytes(), archive)); } @@ -443,7 +443,7 @@ public class TarFile implements Closeable { } currEntry.setSparseHeaders(sparseHeaders); // data of the entry is after the pax gnu entry. So we need to update the data position once again - currEntry.setDataPosition(currEntry.getDataPosition() + recordSize); + currEntry.setDataOffset(currEntry.getDataOffset() + recordSize); } // sparse headers are all done reading, we need to build @@ -620,7 +620,7 @@ public class TarFile implements Closeable { private int currentSparseInputStreamIndex; BoundedTarEntryInputStream(final TarArchiveEntry entry, final SeekableByteChannel channel) { - super(entry.getDataPosition(), entry.isSparse() ? entry.getRealSize() : entry.getSize()); + super(entry.getDataOffset(), entry.isSparse() ? entry.getRealSize() : entry.getSize()); this.entry = entry; this.channel = channel; } @@ -660,7 +660,7 @@ public class TarFile implements Closeable { // if there are no actual input streams, just read from the original archive final List<InputStream> entrySparseInputStreams = sparseInputStreams.get(entry.getName()); if (entrySparseInputStreams == null || entrySparseInputStreams.isEmpty()) { - return readArchive(entry.getDataPosition() + pos, buf); + return readArchive(entry.getDataOffset() + pos, buf); } if (currentSparseInputStreamIndex >= entrySparseInputStreams.size()) {
