Author: bodewig Date: Sun Aug 14 05:39:53 2011 New Revision: 1157469 URL: http://svn.apache.org/viewvc?rev=1157469&view=rev Log: address issues detected by FindBugs and PMD
Modified: commons/proper/compress/trunk/findbugs-exclude-filter.xml commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Modified: commons/proper/compress/trunk/findbugs-exclude-filter.xml URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/findbugs-exclude-filter.xml?rev=1157469&r1=1157468&r2=1157469&view=diff ============================================================================== --- commons/proper/compress/trunk/findbugs-exclude-filter.xml (original) +++ commons/proper/compress/trunk/findbugs-exclude-filter.xml Sun Aug 14 05:39:53 2011 @@ -62,4 +62,13 @@ <Bug pattern="DE_MIGHT_IGNORE" /> </Match> + <!-- Reason: skip(Long.MAX_VALUE) called to drain stream completely, + the class overrides skip to ensure it reads the full amount + until EOF is reached --> + <Match> + <Class name="org.apache.commons.compress.archivers.zip.ZipArchiveInputStream" /> + <Method name="closeEntry" /> + <Bug pattern="SR_NOT_CHECKED" /> + </Match> + </FindBugsFilter> Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=1157469&r1=1157468&r2=1157469&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java Sun Aug 14 05:39:53 2011 @@ -23,10 +23,10 @@ package org.apache.commons.compress.archivers.tar; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -261,9 +261,14 @@ public class TarArchiveInputStream exten } private void paxHeaders() throws IOException{ - BufferedReader br = new BufferedReader(new InputStreamReader(this, "UTF-8")); + Reader br = new InputStreamReader(this, "UTF-8") { + public void close() { + // make sure GC doesn't close "this" before we are done + } + }; Map<String, String> headers = new HashMap<String, String>(); // Format is "length keyword=value\n"; + try { while(true){ // get length int ch; int len=0; @@ -298,6 +303,11 @@ public class TarArchiveInputStream exten break; } } + } finally { + // NO-OP but makes FindBugs happy + br.close(); + } + getNextEntry(); // Get the actual file entry /* * The following headers are defined for Pax. Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=1157469&r1=1157468&r2=1157469&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java Sun Aug 14 05:39:53 2011 @@ -34,7 +34,6 @@ import java.util.zip.ZipException; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveOutputStream; -import static org.apache.commons.compress.archivers.zip.ZipConstants.BYTE_MASK; import static org.apache.commons.compress.archivers.zip.ZipConstants.DATA_DESCRIPTOR_MIN_VERSION; import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD; import static org.apache.commons.compress.archivers.zip.ZipConstants.INITIAL_VERSION; @@ -271,7 +270,7 @@ public class ZipArchiveOutputStream exte if (_raf != null) { try { _raf.close(); - } catch (IOException inner) { + } catch (IOException inner) { // NOPMD // ignore } _raf = null; @@ -1198,12 +1197,11 @@ public class ZipArchiveOutputStream exte return; } - if (!hasUsedZip64) { - if (cdOffset >= ZIP64_MAGIC || cdLength >= ZIP64_MAGIC - || entries.size() >= ZIP64_MAGIC_SHORT) { + if (!hasUsedZip64 + && (cdOffset >= ZIP64_MAGIC || cdLength >= ZIP64_MAGIC + || entries.size() >= ZIP64_MAGIC_SHORT)) { // actually "will use" hasUsedZip64 = true; - } } if (!hasUsedZip64) { Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1157469&r1=1157468&r2=1157469&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Sun Aug 14 05:39:53 2011 @@ -17,6 +17,7 @@ */ package org.apache.commons.compress.archivers.zip; +import java.io.EOFException; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -679,7 +680,7 @@ public class ZipFile { */ private void positionAtCentralDirectory64() throws IOException { - archive.skipBytes(ZIP64_EOCDL_LOCATOR_OFFSET); + skipBytes(ZIP64_EOCDL_LOCATOR_OFFSET); byte[] zip64EocdOffset = new byte[DWORD]; archive.readFully(zip64EocdOffset); archive.seek(ZipEightByteInteger.getLongValue(zip64EocdOffset)); @@ -693,8 +694,8 @@ public class ZipFile { throw new ZipException("archive's ZIP64 end of central " + "directory locator is corrupt."); } - archive.skipBytes(ZIP64_EOCD_CFD_LOCATOR_OFFSET - - WORD /* signature has already been read */); + skipBytes(ZIP64_EOCD_CFD_LOCATOR_OFFSET + - WORD /* signature has already been read */); byte[] cfdOffset = new byte[DWORD]; archive.readFully(cfdOffset); archive.seek(ZipEightByteInteger.getLongValue(cfdOffset)); @@ -712,7 +713,7 @@ public class ZipFile { if (!found) { throw new ZipException("archive is not a ZIP archive"); } - archive.skipBytes(CFD_LOCATOR_OFFSET); + skipBytes(CFD_LOCATOR_OFFSET); byte[] cfdOffset = new byte[WORD]; archive.readFully(cfdOffset); archive.seek(ZipLong.getValue(cfdOffset)); @@ -759,6 +760,21 @@ public class ZipFile { } /** + * Skips the given number of bytes or throws an EOFException if + * skipping failed. + */ + private void skipBytes(final int count) throws IOException { + int totalSkipped = 0; + while (totalSkipped < count) { + int skippedNow = archive.skipBytes(count - totalSkipped); + if (skippedNow <= 0) { + throw new EOFException(); + } + totalSkipped += skippedNow; + } + } + + /** * Number of bytes in local file header up to the "length of * filename" entry. */