You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2021/06/05 10:48:46 UTC

[commons-compress] branch master updated: properly document difference between tar getSize and getRealSize ...

This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new 9450bcc  properly document difference between tar getSize and getRealSize ...
9450bcc is described below

commit 9450bcc7eefa2d6acf1bdbf49740934a6426cf5e
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Sat Jun 5 12:47:31 2021 +0200

    properly document difference between tar getSize and getRealSize ...
    
    ... and simplify a few unnecessary isSparse branches
---
 .../commons/compress/archivers/tar/TarArchiveEntry.java     |  8 +++++++-
 .../compress/archivers/tar/TarArchiveInputStream.java       | 11 ++---------
 .../org/apache/commons/compress/archivers/tar/TarFile.java  | 13 +++----------
 3 files changed, 12 insertions(+), 20 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 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 class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
     /**
      * 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 class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
 
     /**
      * 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 class TarArchiveInputStream extends ArchiveInputStream {
             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 @@ public class TarFile implements Closeable {
         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;