commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [commons-compress] 06/06: COMPRESS-124 review cosmetics
Date Wed, 01 Jan 2020 17:11:38 GMT
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

commit b465bfa6a73cfdaf440d26e56966cf60d6761f83
Author: Stefan Bodewig <bodewig@apache.org>
AuthorDate: Wed Jan 1 18:11:01 2020 +0100

    COMPRESS-124 review cosmetics
    
    see #87
---
 .../compress/archivers/tar/TarArchiveEntry.java    |  5 +++-
 .../archivers/tar/TarArchiveInputStream.java       | 26 +++++++++---------
 .../archivers/tar/TarArchiveSparseEntry.java       |  3 +++
 .../archivers/tar/TarArchiveStructSparse.java      |  1 +
 .../compress/archivers/tar/TarConstants.java       | 10 +++----
 .../commons/compress/archivers/tar/TarUtils.java   |  4 +++
 .../commons/compress/utils/BoundedInputStream.java |  3 +++
 .../compress/archivers/tar/SparseFilesTest.java    | 31 +++-------------------
 8 files changed, 38 insertions(+), 45 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 2aac73e..b240157 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
@@ -754,6 +754,7 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants {
     /**
      * Set this entry's sparse headers
      * @param sparseHeaders The new sparse headers
+     * @since 1.20
      */
     public void setSparseHeaders(List<TarArchiveStructSparse> sparseHeaders) {
         this.sparseHeaders = sparseHeaders;
@@ -763,6 +764,7 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants {
      * Get this entry's sparse headers
      *
      * @return This entry's sparse headers
+     * @since 1.20
      */
     public List<TarArchiveStructSparse> getSparseHeaders() {
         return sparseHeaders;
@@ -772,6 +774,7 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants {
      * Get if this entry is a sparse file with 1.X PAX Format or not
      *
      * @return True if this entry is a sparse file with 1.X PAX Format
+     * @since 1.20
      */
     public boolean isPaxGNU1XSparse() {
         return paxGNU1XSparse;
@@ -852,7 +855,7 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants {
 
     /**
      * Get this entry's real file size in case of a sparse file.
-     * If the file is not a sparse file, return size instead of realSize.
+     * <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.
      */
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 0f6b70f..9687ec7 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
@@ -26,7 +26,12 @@ package org.apache.commons.compress.archivers.tar;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -264,10 +269,10 @@ public class TarArchiveInputStream extends ArchiveInputStream {
 
         final long available = currEntry.getRealSize() - entryOffset;
         final long skipped;
-        if(!currEntry.isSparse()) {
+        if (!currEntry.isSparse()) {
             skipped = IOUtils.skip(inputStream, Math.min(n, available));
         } else {
-            skipped = skipSparse(n);
+            skipped = skipSparse(Math.min(n, available));
         }
         count(skipped);
         entryOffset += skipped;
@@ -284,15 +289,14 @@ public class TarArchiveInputStream extends ArchiveInputStream {
      * @throws IOException
      */
     private long skipSparse(final long n) throws IOException {
-        if (sparseInputStreams.size() == 0) {
+        if (sparseInputStreams == null || sparseInputStreams.size() == 0) {
             return inputStream.skip(n);
         }
 
         long bytesSkipped = 0;
-        InputStream currentInputStream;
 
         while (bytesSkipped < n && currentSparseInputStreamIndex < sparseInputStreams.size())
{
-            currentInputStream = sparseInputStreams.get(currentSparseInputStreamIndex);
+            final InputStream  currentInputStream = sparseInputStreams.get(currentSparseInputStreamIndex);
             bytesSkipped += currentInputStream.skip(n - bytesSkipped);
 
             if (bytesSkipped < n) {
@@ -605,11 +609,9 @@ public class TarArchiveInputStream extends ArchiveInputStream {
         // for 1.X PAX Headers
         List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>();
         long bytesRead = 0;
-        long[] readResult;
-        long sparseHeadersCount;
 
-        readResult = readLineOfNumberForPax1X(inputStream);
-        sparseHeadersCount = readResult[0];
+        long[] readResult = readLineOfNumberForPax1X(inputStream);
+        long sparseHeadersCount = readResult[0];
         bytesRead += readResult[1];
         while (sparseHeadersCount-- > 0) {
             readResult = readLineOfNumberForPax1X(inputStream);
@@ -670,7 +672,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
      *    Map of non-null data chunks. It is a string consisting of comma-separated values
"offset,size[,offset-1,size-1...]"
      *
      * @param i                 inputstream to read keys and values
-     * @param sparseHeaders used in PAX Format 0.0 & 0.1, as it may appear multi times,
+     * @param sparseHeaders used in PAX Format 0.0 &amp; 0.1, as it may appear multi
times,
      *                      the sparse headers need to be stored in an array, not a map
      * @return
      * @throws IOException
@@ -895,7 +897,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
      */
     private int readSparse(final byte[] buf, final int offset, int numToRead) throws IOException
{
         // if there are no actual input streams, just read from the original input stream
-        if (sparseInputStreams.size() == 0) {
+        if (sparseInputStreams == null || sparseInputStreams.size() == 0) {
             return inputStream.read(buf, offset, numToRead);
         }
 
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseEntry.java
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseEntry.java
index 5599e06..1d89fca 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseEntry.java
@@ -76,6 +76,9 @@ public class TarArchiveSparseEntry implements TarConstants {
         return isExtended;
     }
 
+    /**
+     * @since 1.20
+     */
     public List<TarArchiveStructSparse> getSparseHeaders() {
         return sparseHeaders;
     }
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
index 8221a25..6bd4450 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
@@ -30,6 +30,7 @@ import java.util.Objects;
  * char numbytes[12]; // offset 12
  * };
  * </pre>
+ * @since 1.20
  */
 public class TarArchiveStructSparse {
     private long offset;
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarConstants.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarConstants.java
index 6fd959d..ffd2aa5 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarConstants.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarConstants.java
@@ -187,27 +187,27 @@ public interface TarConstants {
 
     /**
      * The length of offset in struct sparse
-     *
+     * @since 1.20
      */
     int    SPARSE_OFFSET_LEN = 12;
 
     /**
      * The length of numbytes in struct sparse
-     *
+     * @since 1.20
      */
     int    SPARSE_NUMBYTES_LEN = 12;
 
     /**
      *  The number of sparse headers in an old GNU header
-     *
+     * @since 1.20
      */
     int    SPARSE_HEADERS_IN_OLDGNU_HEADER = 4;
 
     /**
      *  The number of sparse headers in an extension header
-     *
+     * @since 1.20
      */
-    int SPARSE_HEADERS_IN_EXTENSION_HEADER = 21;
+    int    SPARSE_HEADERS_IN_EXTENSION_HEADER = 21;
 
     /**
      * The sum of the length of all sparse headers in a sparse header buffer.
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
index 1ecb57e..860cd19 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
@@ -303,6 +303,10 @@ public class TarUtils {
         return "";
     }
 
+    /**
+     * Parses the content of a PAX 1.0 sparse block.
+     * @since 1.20
+     */
     public static TarArchiveStructSparse parseSparse(final byte[] buffer, final int offset)
{
         long sparseOffset = parseOctalOrBinary(buffer, offset, SPARSE_OFFSET_LEN);
         long sparseNumbytes = parseOctalOrBinary(buffer, offset + SPARSE_OFFSET_LEN, SPARSE_NUMBYTES_LEN);
diff --git a/src/main/java/org/apache/commons/compress/utils/BoundedInputStream.java b/src/main/java/org/apache/commons/compress/utils/BoundedInputStream.java
index 6d33445..b6b3622 100644
--- a/src/main/java/org/apache/commons/compress/utils/BoundedInputStream.java
+++ b/src/main/java/org/apache/commons/compress/utils/BoundedInputStream.java
@@ -74,6 +74,9 @@ public class BoundedInputStream extends InputStream {
         // stream is controlled externally
     }
 
+    /**
+     * @since 1.20
+     */
     @Override
     public long skip(final long n) throws IOException {
         long bytesToSkip = Math.min(bytesRemaining, n);
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
b/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
index 33b0c91..6056feb 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
@@ -133,22 +133,10 @@ public class SparseFilesTest extends AbstractTestCase {
         }
 
         final File file = getFile("oldgnu_sparse.tar");
-        InputStream sparseFileInputStream = null;
-        TarArchiveInputStream tin = null;
-        try {
-            sparseFileInputStream = extractTarAndGetInputStream(file, "sparsefile");
-            tin = new TarArchiveInputStream(new FileInputStream(file));
+        try (InputStream sparseFileInputStream = extractTarAndGetInputStream(file, "sparsefile");
+             TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(file)))
{
             tin.getNextTarEntry();
-
             Assert.assertTrue(IOUtils.contentEquals(tin, sparseFileInputStream));
-        } finally {
-            if (sparseFileInputStream != null) {
-                sparseFileInputStream.close();
-            }
-
-            if (tin != null) {
-                tin.close();
-            }
         }
     }
 
@@ -159,11 +147,8 @@ public class SparseFilesTest extends AbstractTestCase {
         }
 
         final File file = getFile("oldgnu_extended_sparse.tar");
-        InputStream sparseFileInputStream = null;
-        TarArchiveInputStream tin = null;
-        try {
-            sparseFileInputStream = extractTarAndGetInputStream(file, "sparse6");
-            tin = new TarArchiveInputStream(new FileInputStream(file));
+        try (InputStream sparseFileInputStream = extractTarAndGetInputStream(file, "sparse6");
+             TarArchiveInputStream tin = new TarArchiveInputStream(new FileInputStream(file)))
{
             final TarArchiveEntry ae = tin.getNextTarEntry();
 
             Assert.assertTrue(IOUtils.contentEquals(tin, sparseFileInputStream));
@@ -191,14 +176,6 @@ public class SparseFilesTest extends AbstractTestCase {
 
             assertEquals(51200, sparseHeaders.get(6).getOffset());
             assertEquals(0, sparseHeaders.get(6).getNumbytes());
-        } finally {
-            if (sparseFileInputStream != null) {
-                sparseFileInputStream.close();
-            }
-
-            if (tin != null) {
-                tin.close();
-            }
         }
     }
 


Mime
View raw message