properly document difference between tar getSize and getRealSize ...
... and simplify a few unnecessary isSparse branches
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 ff10db2..44dcf54 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
@@ -905,6 +905,9 @@ public int getMode() {
/**
* Get this entry's file size.
*
+ * <p>This is the size the entry's data uses inside of the archive. Usually this is the same as {@link
+ * #getRealSize}, but it doesn't take the "holes" into account when the entry represents a sparse file.
+ *
* @return This entry's file size.
*/
@Override
@@ -1057,13 +1060,16 @@ public boolean isExtended() {
/**
* Get this entry's real file size in case of a sparse file.
+ *
+ * <p>This is the size a file would take on disk if the entry was expanded.</p>
+ *
* <p>If the file is not a sparse file, return size instead of realSize.</p>
*
* @return This entry's real file size, if the file is not a sparse file, return size instead of realSize.
*/
public long getRealSize() {
if (!isSparse()) {
- return size;
+ return getSize();
}
return realSize;
}
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index 6311bd3..7bf705e 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -725,15 +725,8 @@ public int read(final byte[] buf, final int offset, int numToRead) throws IOExce
throw new IllegalStateException("No current tar entry");
}
- if (!currEntry.isSparse()) {
- if (entryOffset >= entrySize) {
- return -1;
- }
- } else {
- // for sparse entries, there are actually currEntry.getRealSize() bytes to read
- if (entryOffset >= currEntry.getRealSize()) {
- return -1;
- }
+ if (entryOffset >= currEntry.getRealSize()) {
+ return -1;
}
numToRead = Math.min(numToRead, available());
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 15adb18..e79d390 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
@@ -666,22 +666,15 @@ private final class BoundedTarEntryInputStream extends BoundedArchiveInputStream
private int currentSparseInputStreamIndex;
BoundedTarEntryInputStream(final TarArchiveEntry entry, final SeekableByteChannel channel) {
- super(entry.getDataOffset(), entry.isSparse() ? entry.getRealSize() : entry.getSize());
+ super(entry.getDataOffset(), entry.getRealSize());
this.entry = entry;
this.channel = channel;
}
@Override
protected int read(final long pos, final ByteBuffer buf) throws IOException {
- if (entry.isSparse()) {
- // for sparse entries, there are actually currEntry.getRealSize() bytes to read
- if (entryOffset >= entry.getRealSize()) {
- return -1;
- }
- } else {
- if (entryOffset >= entry.getSize()) {
- return -1;
- }
+ if (entryOffset >= entry.getRealSize()) {
+ return -1;
}
final int totalRead;