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 2020/01/23 06:16:54 UTC

[commons-compress] branch master updated: address issues detected by FindBugs

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 2f6addb  address issues detected by FindBugs
2f6addb is described below

commit 2f6addbd38448573a7838494ce7b97b83a3f25cb
Author: Stefan Bodewig <bo...@apache.org>
AuthorDate: Thu Jan 23 07:16:33 2020 +0100

    address issues detected by FindBugs
---
 .../commons/compress/archivers/sevenz/CLI.java     |  2 -
 .../archivers/tar/TarArchiveInputStream.java       | 46 +++++++++++-----------
 .../archivers/tar/TarArchiveStructSparse.java      |  8 +++-
 .../archivers/zip/ZipArchiveOutputStream.java      |  7 ++--
 .../archivers/zip/ZipSplitOutputStream.java        | 12 ++++--
 .../zip/ZipSplitReadOnlySeekableByteChannel.java   | 19 +++++----
 6 files changed, 53 insertions(+), 41 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
index 66afeed..0d1510d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
@@ -19,8 +19,6 @@ package org.apache.commons.compress.archivers.sevenz;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.OutputStream;
-import java.nio.file.Files;
 
 public class CLI {
 
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 9687ec7..d2b690b 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
@@ -79,8 +79,6 @@ public class TarArchiveInputStream extends ArchiveInputStream {
     /** the index of current input stream being read when reading sparse entries */
     private int currentSparseInputStreamIndex;
 
-    private InputStream sparseInputStream;
-
     /** The meta-data about the current entry */
     private TarArchiveEntry currEntry;
 
@@ -564,7 +562,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
         applyPaxHeadersToCurrentEntry(headers, sparseHeaders);
 
         // for 1.0 PAX Format, the sparse map is stored in the file data block
-        if(currEntry.isPaxGNU1XSparse()) {
+        if (currEntry.isPaxGNU1XSparse()) {
             sparseHeaders = parsePAX1XSparseHeaders();
             currEntry.setSparseHeaders(sparseHeaders);
         }
@@ -1038,10 +1036,8 @@ public class TarArchiveInputStream extends ArchiveInputStream {
     private void buildSparseInputStreams() throws IOException {
         currentSparseInputStreamIndex = -1;
         sparseInputStreams = new ArrayList<>();
-        InputStream zeroInputStream = new TarArchiveSparseZeroInputStream();
 
-        long offset = 0;
-        List<TarArchiveStructSparse> sparseHeaders = currEntry.getSparseHeaders();
+        final List<TarArchiveStructSparse> sparseHeaders = currEntry.getSparseHeaders();
         // sort the sparse headers in case they are written in wrong order
         if (sparseHeaders != null && sparseHeaders.size() > 1) {
             final Comparator<TarArchiveStructSparse> sparseHeaderComparator = new Comparator<TarArchiveStructSparse>() {
@@ -1055,26 +1051,30 @@ public class TarArchiveInputStream extends ArchiveInputStream {
             Collections.sort(sparseHeaders, sparseHeaderComparator);
         }
 
-        for (TarArchiveStructSparse sparseHeader : sparseHeaders) {
-            if (sparseHeader.getOffset() == 0 && sparseHeader.getNumbytes() == 0) {
-                break;
-            }
+        if (sparseHeaders != null) {
+            final InputStream zeroInputStream = new TarArchiveSparseZeroInputStream();
+            long offset = 0;
+            for (TarArchiveStructSparse sparseHeader : sparseHeaders) {
+                if (sparseHeader.getOffset() == 0 && sparseHeader.getNumbytes() == 0) {
+                    break;
+                }
 
-            if ((sparseHeader.getOffset() - offset) < 0) {
-                throw new IOException("Corrupted struct sparse detected");
-            }
+                if ((sparseHeader.getOffset() - offset) < 0) {
+                    throw new IOException("Corrupted struct sparse detected");
+                }
 
-            // only store the input streams with non-zero size
-            if ((sparseHeader.getOffset() - offset) > 0) {
-                sparseInputStreams.add(new BoundedInputStream(zeroInputStream, sparseHeader.getOffset() - offset));
-            }
+                // only store the input streams with non-zero size
+                if ((sparseHeader.getOffset() - offset) > 0) {
+                    sparseInputStreams.add(new BoundedInputStream(zeroInputStream, sparseHeader.getOffset() - offset));
+                }
 
-            // only store the input streams with non-zero size
-            if (sparseHeader.getNumbytes() > 0) {
-                sparseInputStreams.add(new BoundedInputStream(inputStream, sparseHeader.getNumbytes()));
-            }
+                // only store the input streams with non-zero size
+                if (sparseHeader.getNumbytes() > 0) {
+                    sparseInputStreams.add(new BoundedInputStream(inputStream, sparseHeader.getNumbytes()));
+                }
 
-            offset = sparseHeader.getOffset() + sparseHeader.getNumbytes();
+                offset = sparseHeader.getOffset() + sparseHeader.getNumbytes();
+            }
         }
 
         if (sparseInputStreams.size() > 0) {
@@ -1086,7 +1086,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
      * This is an inputstream that always return 0,
      * this is used when reading the "holes" of a sparse file
      */
-    public class TarArchiveSparseZeroInputStream extends InputStream {
+    private static class TarArchiveSparseZeroInputStream extends InputStream {
         /**
          * Just return 0
          * @return
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 6bd4450..916f532 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
@@ -44,8 +44,12 @@ public class TarArchiveStructSparse {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
         TarArchiveStructSparse that = (TarArchiveStructSparse) o;
         return offset == that.offset &&
                 numbytes == that.numbytes;
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
index f630036..9df786a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
@@ -1520,7 +1520,7 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream {
             return;
         }
 
-        long numberOfThisDisk = 0;
+        int numberOfThisDisk = 0;
         if (isSplitZip) {
             numberOfThisDisk = ((ZipSplitOutputStream)this.out).getCurrentSplitSegmentIndex();
         }
@@ -1534,7 +1534,8 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream {
                     .NUMBER_OF_THE_DISK_OF_CENTRAL_DIRECTORY_TOO_BIG_MESSAGE);
         }
 
-        final int numOfEntriesOnThisDisk = numberOfCDInDiskData.get(numberOfThisDisk) == null ? 0 : numberOfCDInDiskData.get(numberOfThisDisk);
+        final int numOfEntriesOnThisDisk = numberOfCDInDiskData.get(numberOfThisDisk) == null
+            ? 0 : numberOfCDInDiskData.get(numberOfThisDisk);
         if (numOfEntriesOnThisDisk >= ZIP64_MAGIC_SHORT) {
             throw new Zip64RequiredException(Zip64RequiredException
                     .TOO_MANY_ENTRIES_ON_THIS_DISK_MESSAGE);
@@ -1607,7 +1608,7 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream {
         writeOut(ZipShort.getBytes(ZIP64_MIN_VERSION));
 
         // number of this disk
-        long numberOfThisDisk = 0;
+        int numberOfThisDisk = 0;
         if (isSplitZip) {
             numberOfThisDisk = ((ZipSplitOutputStream)this.out).getCurrentSplitSegmentIndex();
         }
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
index 0bed063..0a84053 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java
@@ -46,8 +46,8 @@ class ZipSplitOutputStream extends OutputStream {
      * Minimum segment size = 64K
      * Maximum PKSFX segment size = 2,147,483,647 bytes
      */
-    private final long ZIP_SEGMENT_MIN_SIZE = 64 * 1024L;
-    private final long ZIP_SEGMENT_MAX_SIZE = 4294967295L;
+    private final static long ZIP_SEGMENT_MIN_SIZE = 64 * 1024L;
+    private final static long ZIP_SEGMENT_MAX_SIZE = 4294967295L;
 
     /**
      * Create a split zip. If the zip file is smaller than the split size,
@@ -152,7 +152,9 @@ class ZipSplitOutputStream extends OutputStream {
         String zipFileBaseName = FileNameUtils.getBaseName(zipFile.getName());
         File lastZipSplitSegmentFile = new File(zipFile.getParentFile(), zipFileBaseName + ".zip");
         outputStream.close();
-        zipFile.renameTo(lastZipSplitSegmentFile);
+        if (!zipFile.renameTo(lastZipSplitSegmentFile)) {
+            throw new IOException("Failed to rename " + zipFile + " to " + lastZipSplitSegmentFile);
+        }
         finished = true;
     }
 
@@ -167,7 +169,9 @@ class ZipSplitOutputStream extends OutputStream {
         if (currentSplitSegmentIndex == 0) {
             outputStream.close();
             newFile = createNewSplitSegmentFile(1);
-            zipFile.renameTo(newFile);
+            if (!zipFile.renameTo(newFile)) {
+                throw new IOException("Failed to rename " + zipFile + " to " + newFile);
+            }
         }
 
         newFile = createNewSplitSegmentFile(null);
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
index 471a437..40a5129 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java
@@ -24,6 +24,7 @@ import org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.Serializable;
 import java.nio.ByteBuffer;
 import java.nio.channels.SeekableByteChannel;
 import java.nio.file.Files;
@@ -45,7 +46,7 @@ import java.util.regex.Pattern;
  * @since 1.20
  */
 public class ZipSplitReadOnlySeekableByteChannel extends MultiReadOnlySeekableByteChannel {
-    private final int ZIP_SPLIT_SIGNATURE_LENGTH = 4;
+    private static final int ZIP_SPLIT_SIGNATURE_LENGTH = 4;
     private final ByteBuffer zipSplitSignatureByteBuffer =
         ByteBuffer.allocate(ZIP_SPLIT_SIGNATURE_LENGTH);
 
@@ -166,12 +167,15 @@ public class ZipSplitReadOnlySeekableByteChannel extends MultiReadOnlySeekableBy
 
         // zip split segments should be like z01,z02....z(n-1) based on the zip specification
         Pattern pattern = Pattern.compile(Pattern.quote(fileBaseName) + ".[zZ][0-9]+");
-        for (File file : parent.listFiles()) {
-            if (!pattern.matcher(file.getName()).matches()) {
-                continue;
+        final File[] children = parent.listFiles();
+        if (children != null) {
+            for (File file : children) {
+                if (!pattern.matcher(file.getName()).matches()) {
+                    continue;
+                }
+
+                splitZipSegments.add(file);
             }
-
-            splitZipSegments.add(file);
         }
 
         Collections.sort(splitZipSegments, new ZipSplitSegmentComparator());
@@ -225,7 +229,8 @@ public class ZipSplitReadOnlySeekableByteChannel extends MultiReadOnlySeekableBy
         return forFiles(filesList.toArray(filesArray));
     }
 
-    public static class ZipSplitSegmentComparator implements Comparator<File> {
+    private static class ZipSplitSegmentComparator implements Comparator<File>, Serializable {
+        private static final long serialVersionUID = 20200123L;
         @Override
         public int compare(File file1, File file2) {
             String extension1 = FileNameUtils.getExtension(file1.getPath());