You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/12/26 00:54:46 UTC
[commons-compress] branch master updated: FIx leaky test that
causes builds to fail (at least on Oracle Java 8 and Windows 10) because
many file handles are left open.
This is an automated email from the ASF dual-hosted git repository.
ggregory 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 eecd6ae FIx leaky test that causes builds to fail (at least on Oracle Java 8 and Windows 10) because many file handles are left open.
eecd6ae is described below
commit eecd6ae89ada048cea11f8a18bd369e609eae33e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 25 19:54:42 2019 -0500
FIx leaky test that causes builds to fail (at least on Oracle Java 8 and
Windows 10) because many file handles are left open.
- Refactor File to byte array code pattern into new IOUtils method.
- Use less obtuse names.
---
.../archivers/tar/TarArchiveInputStream.java | 30 +--
.../commons/compress/archivers/zip/BinaryTree.java | 6 +-
.../compressors/CompressorStreamFactory.java | 14 +-
.../lz4/FramedLZ4CompressorInputStream.java | 22 +-
.../snappy/FramedSnappyCompressorInputStream.java | 22 +-
.../org/apache/commons/compress/utils/IOUtils.java | 35 ++-
.../commons/compress/archivers/ZipTestCase.java | 264 ++++++++++-----------
.../compress/archivers/zip/ScatterSampleTest.java | 27 ++-
.../brotli/BrotliCompressorInputStreamTest.java | 7 +-
.../lz4/FramedLZ4CompressorInputStreamTest.java | 5 +-
.../FramedSnappyCompressorInputStreamTest.java | 5 +-
.../zstandard/ZstdCompressorInputStreamTest.java | 3 +-
12 files changed, 216 insertions(+), 224 deletions(-)
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 7ec8ed7..65b7e32 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
@@ -66,7 +66,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
private long entryOffset;
/** An input stream to read from */
- private final InputStream is;
+ private final InputStream inputStream;
/** The meta-data about the current entry */
private TarArchiveEntry currEntry;
@@ -170,7 +170,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
*/
public TarArchiveInputStream(final InputStream is, final int blockSize, final int recordSize,
final String encoding, boolean lenient) {
- this.is = is;
+ this.inputStream = is;
this.hasHitEOF = false;
this.encoding = encoding;
this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
@@ -185,7 +185,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
*/
@Override
public void close() throws IOException {
- is.close();
+ inputStream.close();
}
/**
@@ -244,7 +244,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
}
final long available = entrySize - entryOffset;
- final long skipped = IOUtils.skip(is, Math.min(n, available));
+ final long skipped = IOUtils.skip(inputStream, Math.min(n, available));
count(skipped);
entryOffset += skipped;
return skipped;
@@ -372,7 +372,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
if (!isDirectory() && this.entrySize > 0 && this.entrySize % this.recordSize != 0) {
final long numRecords = (this.entrySize / this.recordSize) + 1;
final long padding = (numRecords * this.recordSize) - this.entrySize;
- final long skipped = IOUtils.skip(is, padding);
+ final long skipped = IOUtils.skip(inputStream, padding);
count(skipped);
}
}
@@ -456,7 +456,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
final byte[] record = new byte[recordSize];
- final int readNow = IOUtils.readFully(is, record);
+ final int readNow = IOUtils.readFully(inputStream, record);
count(readNow);
if (readNow != recordSize) {
return null;
@@ -479,7 +479,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
// NOTE, using a Map here makes it impossible to ever support GNU
// sparse files using the PAX Format 0.0, see
// https://www.gnu.org/software/tar/manual/html_section/tar_92.html#SEC188
- Map<String, String> parsePaxHeaders(final InputStream i)
+ Map<String, String> parsePaxHeaders(final InputStream inputStream)
throws IOException {
final Map<String, String> headers = new HashMap<>(globalPaxHeaders);
// Format is "length keyword=value\n";
@@ -487,14 +487,14 @@ public class TarArchiveInputStream extends ArchiveInputStream {
int ch;
int len = 0;
int read = 0;
- while((ch = i.read()) != -1) {
+ while((ch = inputStream.read()) != -1) {
read++;
if (ch == '\n') { // blank line in header
break;
} else if (ch == ' '){ // End of length string
// Get keyword
final ByteArrayOutputStream coll = new ByteArrayOutputStream();
- while((ch = i.read()) != -1) {
+ while((ch = inputStream.read()) != -1) {
read++;
if (ch == '='){ // end of keyword
final String keyword = coll.toString(CharsetNames.UTF_8);
@@ -504,7 +504,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
headers.remove(keyword);
} else {
final byte[] rest = new byte[restLen];
- final int got = IOUtils.readFully(i, rest);
+ final int got = IOUtils.readFully(inputStream, rest);
if (got != restLen) {
throw new IOException("Failed to read "
+ "Paxheader. Expected "
@@ -595,16 +595,16 @@ public class TarArchiveInputStream extends ArchiveInputStream {
*/
private void tryToConsumeSecondEOFRecord() throws IOException {
boolean shouldReset = true;
- final boolean marked = is.markSupported();
+ final boolean marked = inputStream.markSupported();
if (marked) {
- is.mark(recordSize);
+ inputStream.mark(recordSize);
}
try {
shouldReset = !isEOFRecord(readRecord());
} finally {
if (shouldReset && marked) {
pushedBackBytes(recordSize);
- is.reset();
+ inputStream.reset();
}
}
}
@@ -639,7 +639,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
numToRead = Math.min(numToRead, available());
- totalRead = is.read(buf, offset, numToRead);
+ totalRead = inputStream.read(buf, offset, numToRead);
if (totalRead == -1) {
if (numToRead > 0) {
@@ -697,7 +697,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
private void consumeRemainderOfLastBlock() throws IOException {
final long bytesReadOfLastBlock = getBytesRead() % blockSize;
if (bytesReadOfLastBlock > 0) {
- final long skipped = IOUtils.skip(is, blockSize - bytesReadOfLastBlock);
+ final long skipped = IOUtils.skip(inputStream, blockSize - bytesReadOfLastBlock);
count(skipped);
}
}
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java b/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java
index e742175..0a01ad6 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/BinaryTree.java
@@ -113,19 +113,19 @@ class BinaryTree {
/**
* Decodes the packed binary tree from the specified stream.
*/
- static BinaryTree decode(final InputStream in, final int totalNumberOfValues) throws IOException {
+ static BinaryTree decode(final InputStream inputStream, final int totalNumberOfValues) throws IOException {
if (totalNumberOfValues < 0) {
throw new IllegalArgumentException("totalNumberOfValues must be bigger than 0, is "
+ totalNumberOfValues);
}
// the first byte contains the size of the structure minus one
- final int size = in.read() + 1;
+ final int size = inputStream.read() + 1;
if (size == 0) {
throw new IOException("Cannot read the size of the encoded tree, unexpected end of stream");
}
final byte[] encodedTree = new byte[size];
- final int read = IOUtils.readFully(in, encodedTree);
+ final int read = IOUtils.readFully(inputStream, encodedTree);
if (read != size) {
throw new EOFException();
}
diff --git a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
index d730b9d..55ade9b 100644
--- a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java
@@ -455,7 +455,7 @@ public class CompressorStreamFactory implements CompressorStreamProvider {
/**
* Try to detect the type of compressor stream.
*
- * @param in input stream
+ * @param inputStream input stream
* @return type of compressor stream detected
* @throws CompressorException if no compressor stream type was detected
* or if something else went wrong
@@ -463,21 +463,21 @@ public class CompressorStreamFactory implements CompressorStreamProvider {
*
* @since 1.14
*/
- public static String detect(final InputStream in) throws CompressorException {
- if (in == null) {
+ public static String detect(final InputStream inputStream) throws CompressorException {
+ if (inputStream == null) {
throw new IllegalArgumentException("Stream must not be null.");
}
- if (!in.markSupported()) {
+ if (!inputStream.markSupported()) {
throw new IllegalArgumentException("Mark is not supported.");
}
final byte[] signature = new byte[12];
- in.mark(signature.length);
+ inputStream.mark(signature.length);
int signatureLength = -1;
try {
- signatureLength = IOUtils.readFully(in, signature);
- in.reset();
+ signatureLength = IOUtils.readFully(inputStream, signature);
+ inputStream.reset();
} catch (IOException e) {
throw new CompressorException("IOException while reading signature.", e);
}
diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java
index c316fd3..85f83f8 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java
@@ -70,7 +70,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
}
};
- private final CountingInputStream in;
+ private final CountingInputStream inputStream;
private final boolean decompressConcatenated;
private boolean expectBlockChecksum;
@@ -112,7 +112,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
* @throws IOException if reading fails
*/
public FramedLZ4CompressorInputStream(InputStream in, boolean decompressConcatenated) throws IOException {
- this.in = new CountingInputStream(in);
+ this.inputStream = new CountingInputStream(in);
this.decompressConcatenated = decompressConcatenated;
init(true);
}
@@ -132,7 +132,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
currentBlock = null;
}
} finally {
- in.close();
+ inputStream.close();
}
}
@@ -168,7 +168,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
*/
@Override
public long getCompressedCount() {
- return in.getBytesRead();
+ return inputStream.getBytesRead();
}
private void init(boolean firstFrame) throws IOException {
@@ -181,7 +181,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
private boolean readSignature(boolean firstFrame) throws IOException {
String garbageMessage = firstFrame ? "Not a LZ4 frame stream" : "LZ4 frame stream followed by garbage";
final byte[] b = new byte[4];
- int read = IOUtils.readFully(in, b);
+ int read = IOUtils.readFully(inputStream, b);
count(read);
if (0 == read && !firstFrame) {
// good LZ4 frame and nothing after it
@@ -231,7 +231,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
contentHash.update(bdByte);
if (expectContentSize) { // for now we don't care, contains the uncompressed size
byte[] contentSize = new byte[8];
- int skipped = IOUtils.readFully(in, contentSize);
+ int skipped = IOUtils.readFully(inputStream, contentSize);
count(skipped);
if (8 != skipped) {
throw new IOException("Premature end of stream while reading content size");
@@ -266,7 +266,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
}
return;
}
- InputStream capped = new BoundedInputStream(in, realLen);
+ InputStream capped = new BoundedInputStream(inputStream, realLen);
if (expectBlockChecksum) {
capped = new ChecksumCalculatingInputStream(blockHash, capped);
}
@@ -303,7 +303,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
private void verifyChecksum(XXHash32 hash, String kind) throws IOException {
byte[] checksum = new byte[4];
- int read = IOUtils.readFully(in, checksum);
+ int read = IOUtils.readFully(inputStream, checksum);
count(read);
if (4 != read) {
throw new IOException("Premature end of stream while reading " + kind + " checksum");
@@ -315,7 +315,7 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
}
private int readOneByte() throws IOException {
- final int b = in.read();
+ final int b = inputStream.read();
if (b != -1) {
count(1);
return b & 0xFF;
@@ -363,12 +363,12 @@ public class FramedLZ4CompressorInputStream extends CompressorInputStream
if (len < 0) {
throw new IOException("Found illegal skippable frame with negative size");
}
- long skipped = IOUtils.skip(in, len);
+ long skipped = IOUtils.skip(inputStream, len);
count(skipped);
if (len != skipped) {
throw new IOException("Premature end of stream while skipping frame");
}
- read = IOUtils.readFully(in, b);
+ read = IOUtils.readFully(inputStream, b);
count(read);
}
return read;
diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
index b2864bc..c1d07b5 100644
--- a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java
@@ -65,7 +65,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
private final CountingInputStream countingStream;
/** The underlying stream to read compressed data from */
- private final PushbackInputStream in;
+ private final PushbackInputStream inputStream;
/** The dialect to expect */
private final FramedSnappyDialect dialect;
@@ -131,7 +131,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
throw new IllegalArgumentException("blockSize must be bigger than 0");
}
countingStream = new CountingInputStream(in);
- this.in = new PushbackInputStream(countingStream, 1);
+ this.inputStream = new PushbackInputStream(countingStream, 1);
this.blockSize = blockSize;
this.dialect = dialect;
if (dialect.hasStreamIdentifier()) {
@@ -154,7 +154,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
currentCompressedChunk = null;
}
} finally {
- in.close();
+ inputStream.close();
}
}
@@ -180,7 +180,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
public int available() throws IOException {
if (inUncompressedChunk) {
return Math.min(uncompressedBytesRemaining,
- in.available());
+ inputStream.available());
} else if (currentCompressedChunk != null) {
return currentCompressedChunk.available();
}
@@ -209,7 +209,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
if (amount == 0) {
return -1;
}
- read = in.read(b, off, amount);
+ read = inputStream.read(b, off, amount);
if (read != -1) {
uncompressedBytesRemaining -= read;
count(read);
@@ -237,7 +237,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
if (type == -1) {
endReached = true;
} else if (type == STREAM_IDENTIFIER_TYPE) {
- in.unread(type);
+ inputStream.unread(type);
unreadBytes++;
pushedBackBytes(1);
readStreamIdentifier();
@@ -269,7 +269,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
expectedChecksum = -1;
}
currentCompressedChunk =
- new SnappyCompressorInputStream(new BoundedInputStream(in, size), blockSize);
+ new SnappyCompressorInputStream(new BoundedInputStream(inputStream, size), blockSize);
// constructor reads uncompressed size
count(currentCompressedChunk.getBytesRead());
} else {
@@ -281,7 +281,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
private long readCrc() throws IOException {
final byte[] b = new byte[4];
- final int read = IOUtils.readFully(in, b);
+ final int read = IOUtils.readFully(inputStream, b);
count(read);
if (read != 4) {
throw new IOException("Premature end of stream");
@@ -306,7 +306,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
if (size < 0) {
throw new IOException("Found illegal chunk with negative size");
}
- final long read = IOUtils.skip(in, size);
+ final long read = IOUtils.skip(inputStream, size);
count(read);
if (read != size) {
throw new IOException("Premature end of stream");
@@ -315,7 +315,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
private void readStreamIdentifier() throws IOException {
final byte[] b = new byte[10];
- final int read = IOUtils.readFully(in, b);
+ final int read = IOUtils.readFully(inputStream, b);
count(read);
if (10 != read || !matches(b, 10)) {
throw new IOException("Not a framed Snappy stream");
@@ -323,7 +323,7 @@ public class FramedSnappyCompressorInputStream extends CompressorInputStream
}
private int readOneByte() throws IOException {
- final int b = in.read();
+ final int b = inputStream.read();
if (b != -1) {
count(1);
return b & 0xFF;
diff --git a/src/main/java/org/apache/commons/compress/utils/IOUtils.java b/src/main/java/org/apache/commons/compress/utils/IOUtils.java
index 0eda0fb..7fa502a 100644
--- a/src/main/java/org/apache/commons/compress/utils/IOUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/IOUtils.java
@@ -21,6 +21,8 @@ package org.apache.commons.compress.utils;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.EOFException;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -127,6 +129,25 @@ public final class IOUtils {
}
/**
+ * Reads as much from the file as possible to fill the given array.
+ *
+ * <p>This method may invoke read repeatedly to fill the array and
+ * only read less bytes than the length of the array if the end of
+ * the stream has been reached.</p>
+ *
+ * @param file file to read
+ * @param array buffer to fill
+ * @return the number of bytes actually read
+ * @throws IOException on error
+ * @since 1.20
+ */
+ public static int read(final File file, final byte[] array) throws IOException {
+ try (FileInputStream inputStream = new FileInputStream(file)) {
+ return readFully(inputStream, array, 0, array.length);
+ }
+ }
+
+ /**
* Reads as much from input as possible to fill the given array.
*
* <p>This method may invoke read repeatedly to fill the array and
@@ -134,12 +155,12 @@ public final class IOUtils {
* the stream has been reached.</p>
*
* @param input stream to read from
- * @param b buffer to fill
+ * @param array buffer to fill
* @return the number of bytes actually read
* @throws IOException on error
*/
- public static int readFully(final InputStream input, final byte[] b) throws IOException {
- return readFully(input, b, 0, b.length);
+ public static int readFully(final InputStream input, final byte[] array) throws IOException {
+ return readFully(input, array, 0, array.length);
}
/**
@@ -151,21 +172,21 @@ public final class IOUtils {
* the stream has been reached.</p>
*
* @param input stream to read from
- * @param b buffer to fill
+ * @param array buffer to fill
* @param offset offset into the buffer to start filling at
* @param len of bytes to read
* @return the number of bytes actually read
* @throws IOException
* if an I/O error has occurred
*/
- public static int readFully(final InputStream input, final byte[] b, final int offset, final int len)
+ public static int readFully(final InputStream input, final byte[] array, final int offset, final int len)
throws IOException {
- if (len < 0 || offset < 0 || len + offset > b.length) {
+ if (len < 0 || offset < 0 || len + offset > array.length) {
throw new IndexOutOfBoundsException();
}
int count = 0, x = 0;
while (count != len) {
- x = input.read(b, offset + count, len - count);
+ x = input.read(array, offset + count, len - count);
if (x == -1) {
break;
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
index 30b5492..c45db11 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
@@ -55,6 +55,7 @@ import org.junit.Assert;
import org.junit.Test;
public final class ZipTestCase extends AbstractTestCase {
+
/**
* Archives 2 files and unarchives it again. If the file length of result
* and source is the same, it looks like the operations have worked
@@ -67,49 +68,39 @@ public final class ZipTestCase extends AbstractTestCase {
final File file1 = getFile("test1.xml");
final File file2 = getFile("test2.xml");
- final OutputStream out = new FileOutputStream(output);
- ArchiveOutputStream os = null;
- try {
- os = new ArchiveStreamFactory()
- .createArchiveOutputStream("zip", out);
- os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
- IOUtils.copy(new FileInputStream(file1), os);
- os.closeArchiveEntry();
-
- os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
- IOUtils.copy(new FileInputStream(file2), os);
- os.closeArchiveEntry();
- } finally {
- if (os != null) {
- os.close();
+ try (final OutputStream out = new FileOutputStream(output)) {
+ try (ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
+ os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
+ try (final FileInputStream input = new FileInputStream(file1)) {
+ IOUtils.copy(input, os);
+ }
+ os.closeArchiveEntry();
+
+ os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
+ try (final FileInputStream input = new FileInputStream(file2)) {
+ IOUtils.copy(input, os);
+ }
+ os.closeArchiveEntry();
}
}
- out.close();
// Unarchive the same
final List<File> results = new ArrayList<>();
- final InputStream is = new FileInputStream(output);
- ArchiveInputStream in = null;
- try {
- in = new ArchiveStreamFactory()
- .createArchiveInputStream("zip", is);
-
- ZipArchiveEntry entry = null;
- while((entry = (ZipArchiveEntry)in.getNextEntry()) != null) {
- final File outfile = new File(resultDir.getCanonicalPath() + "/result/" + entry.getName());
- outfile.getParentFile().mkdirs();
- try (OutputStream o = new FileOutputStream(outfile)) {
- IOUtils.copy(in, o);
+ try (final InputStream fileInputStream = new FileInputStream(output)) {
+ try (ArchiveInputStream archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip",
+ fileInputStream)) {
+ ZipArchiveEntry entry = null;
+ while ((entry = (ZipArchiveEntry) archiveInputStream.getNextEntry()) != null) {
+ final File outfile = new File(resultDir.getCanonicalPath() + "/result/" + entry.getName());
+ outfile.getParentFile().mkdirs();
+ try (OutputStream o = new FileOutputStream(outfile)) {
+ IOUtils.copy(archiveInputStream, o);
+ }
+ results.add(outfile);
}
- results.add(outfile);
- }
- } finally {
- if (in != null) {
- in.close();
}
}
- is.close();
assertEquals(results.size(), 2);
File result = results.get(0);
@@ -129,34 +120,33 @@ public final class ZipTestCase extends AbstractTestCase {
final File file2 = getFile("test2.xml");
final byte[] file1Contents = new byte[(int) file1.length()];
final byte[] file2Contents = new byte[(int) file2.length()];
- IOUtils.readFully(new FileInputStream(file1), file1Contents);
- IOUtils.readFully(new FileInputStream(file2), file2Contents);
+ IOUtils.read(file1, file1Contents);
+ IOUtils.read(file2, file2Contents);
+ final List<byte[]> results = new ArrayList<>();
- SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel();
- try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(channel)) {
- os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
- os.write(file1Contents);
- os.closeArchiveEntry();
+ try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel()) {
+ try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(channel)) {
+ os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
+ os.write(file1Contents);
+ os.closeArchiveEntry();
- os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
- os.write(file2Contents);
- os.closeArchiveEntry();
- }
-
- // Unarchive the same
- final List<byte[]> results = new ArrayList<>();
+ os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
+ os.write(file2Contents);
+ os.closeArchiveEntry();
+ }
- try (ArchiveInputStream in = new ArchiveStreamFactory()
- .createArchiveInputStream("zip", new ByteArrayInputStream(channel.array()))) {
+ // Unarchive the same
+ try (ArchiveInputStream inputStream = new ArchiveStreamFactory().createArchiveInputStream("zip",
+ new ByteArrayInputStream(channel.array()))) {
- ZipArchiveEntry entry;
- while((entry = (ZipArchiveEntry)in.getNextEntry()) != null) {
- byte[] result = new byte[(int) entry.getSize()];
- IOUtils.readFully(in, result);
- results.add(result);
+ ZipArchiveEntry entry;
+ while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) {
+ byte[] result = new byte[(int) entry.getSize()];
+ IOUtils.readFully(inputStream, result);
+ results.add(result);
+ }
}
}
-
assertArrayEquals(results.get(0), file1Contents);
assertArrayEquals(results.get(1), file2Contents);
}
@@ -188,8 +178,8 @@ public final class ZipTestCase extends AbstractTestCase {
final ArrayList<String> al = new ArrayList<>();
al.add("test1.xml");
al.add("test2.xml");
- try (InputStream is = new FileInputStream(input)) {
- checkArchiveContent(new ZipArchiveInputStream(is), al);
+ try (InputStream fis = new FileInputStream(input)) {
+ checkArchiveContent(new ZipArchiveInputStream(fis), al);
}
}
@@ -200,11 +190,11 @@ public final class ZipTestCase extends AbstractTestCase {
*/
@Test
public void testTokenizationCompressionMethod() throws IOException {
- final ZipFile moby = new ZipFile(getFile("moby.zip"));
- final ZipArchiveEntry entry = moby.getEntry("README");
- assertEquals("method", ZipMethod.TOKENIZATION.getCode(), entry.getMethod());
- assertFalse(moby.canReadEntryData(entry));
- moby.close();
+ try (final ZipFile moby = new ZipFile(getFile("moby.zip"))) {
+ final ZipArchiveEntry entry = moby.getEntry("README");
+ assertEquals("method", ZipMethod.TOKENIZATION.getCode(), entry.getMethod());
+ assertFalse(moby.canReadEntryData(entry));
+ }
}
/**
@@ -248,10 +238,8 @@ public final class ZipTestCase extends AbstractTestCase {
final List<String> results = new ArrayList<>();
final List<ZipException> expectedExceptions = new ArrayList<>();
- final InputStream is = new FileInputStream(input);
- ArchiveInputStream in = null;
- try {
- in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);
+ try (final InputStream fis = new FileInputStream(input);
+ ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", fis)) {
ZipArchiveEntry entry = null;
while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {
@@ -269,12 +257,7 @@ public final class ZipTestCase extends AbstractTestCase {
}
// nested stream must not be closed here
}
- } finally {
- if (in != null) {
- in.close();
- }
}
- is.close();
assertTrue(results.contains("NestedArchiv.zip"));
assertTrue(results.contains("test1.xml"));
@@ -365,28 +348,28 @@ public final class ZipTestCase extends AbstractTestCase {
@Test
public void testCopyRawEntriesFromFile()
- throws IOException {
+ throws IOException {
final File[] tmp = createTempDirAndFile();
final File reference = createReferenceFile(tmp[0], Zip64Mode.Never, "expected.");
- final File a1 = File.createTempFile("src1.", ".zip", tmp[0]);
- try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(a1)) {
+ final File file1 = File.createTempFile("src1.", ".zip", tmp[0]);
+ try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file1)) {
zos.setUseZip64(Zip64Mode.Never);
createFirstEntry(zos).close();
}
- final File a2 = File.createTempFile("src2.", ".zip", tmp[0]);
- try (final ZipArchiveOutputStream zos1 = new ZipArchiveOutputStream(a2)) {
+ final File file2 = File.createTempFile("src2.", ".zip", tmp[0]);
+ try (final ZipArchiveOutputStream zos1 = new ZipArchiveOutputStream(file2)) {
zos1.setUseZip64(Zip64Mode.Never);
createSecondEntry(zos1).close();
}
- try (final ZipFile zf1 = new ZipFile(a1); final ZipFile zf2 = new ZipFile(a2)) {
+ try (final ZipFile zipFile1 = new ZipFile(file1); final ZipFile zipFile2 = new ZipFile(file2)) {
final File fileResult = File.createTempFile("file-actual.", ".zip", tmp[0]);
try (final ZipArchiveOutputStream zos2 = new ZipArchiveOutputStream(fileResult)) {
- zf1.copyRawEntries(zos2, allFilesPredicate);
- zf2.copyRawEntries(zos2, allFilesPredicate);
+ zipFile1.copyRawEntries(zos2, allFilesPredicate);
+ zipFile2.copyRawEntries(zos2, allFilesPredicate);
}
// copyRawEntries does not add superfluous zip64 header like regular zip output stream
// does when using Zip64Mode.AsNeeded so all the source material has to be Zip64Mode.Never,
@@ -406,17 +389,17 @@ public final class ZipTestCase extends AbstractTestCase {
createFirstEntry(zos1);
}
- final File a1 = File.createTempFile("zip64src.", ".zip", tmp[0]);
- try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(a1)) {
+ final File file1 = File.createTempFile("zip64src.", ".zip", tmp[0]);
+ try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file1)) {
zos.setUseZip64(Zip64Mode.Always);
createFirstEntry(zos).close();
}
final File fileResult = File.createTempFile("file-actual.", ".zip", tmp[0]);
- try (final ZipFile zf1 = new ZipFile(a1)) {
+ try (final ZipFile zipFile1 = new ZipFile(file1)) {
try (final ZipArchiveOutputStream zos2 = new ZipArchiveOutputStream(fileResult)) {
zos2.setUseZip64(Zip64Mode.Always);
- zf1.copyRawEntries(zos2, allFilesPredicate);
+ zipFile1.copyRawEntries(zos2, allFilesPredicate);
}
assertSameFileContents(reference, fileResult);
}
@@ -427,8 +410,8 @@ public final class ZipTestCase extends AbstractTestCase {
final File[] tmp = createTempDirAndFile();
- final File a1 = File.createTempFile("unixModeBits.", ".zip", tmp[0]);
- try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(a1)) {
+ final File file1 = File.createTempFile("unixModeBits.", ".zip", tmp[0]);
+ try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file1)) {
final ZipArchiveEntry archiveEntry = new ZipArchiveEntry("fred");
archiveEntry.setUnixMode(0664);
@@ -436,7 +419,7 @@ public final class ZipTestCase extends AbstractTestCase {
zos.addRawArchiveEntry(archiveEntry, new ByteArrayInputStream("fud".getBytes()));
}
- try (final ZipFile zf1 = new ZipFile(a1)) {
+ try (final ZipFile zf1 = new ZipFile(file1)) {
final ZipArchiveEntry fred = zf1.getEntry("fred");
assertEquals(0664, fred.getUnixMode());
}
@@ -487,12 +470,11 @@ public final class ZipTestCase extends AbstractTestCase {
assertEquals(expectedElement.getExternalAttributes(), actualElement.getExternalAttributes());
assertEquals(expectedElement.getInternalAttributes(), actualElement.getInternalAttributes());
- final InputStream actualIs = actual.getInputStream(actualElement);
- final InputStream expectedIs = expected.getInputStream(expectedElement);
- IOUtils.readFully(expectedIs, expectedBuf);
- IOUtils.readFully(actualIs, actualBuf);
- expectedIs.close();
- actualIs.close();
+ try (final InputStream actualIs = actual.getInputStream(actualElement);
+ final InputStream expectedIs = expected.getInputStream(expectedElement)) {
+ IOUtils.readFully(expectedIs, expectedBuf);
+ IOUtils.readFully(actualIs, actualBuf);
+ }
Assert.assertArrayEquals(expectedBuf, actualBuf); // Buffers are larger than payload. dont care
}
@@ -666,14 +648,15 @@ public final class ZipTestCase extends AbstractTestCase {
File directoryToZip = getFilesToZip();
File outputZipFile = new File(dir, "splitZip.zip");
long splitSize = 100 * 1024L; /* 100 KB */
- final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile, splitSize);
+ try (final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile,
+ splitSize)) {
- // create a file that has the same name of one of the created split segments
- File sameNameFile = new File(dir, "splitZip.z01");
- sameNameFile.createNewFile();
+ // create a file that has the same name of one of the created split segments
+ File sameNameFile = new File(dir, "splitZip.z01");
+ sameNameFile.createNewFile();
- addFilesToZip(zipArchiveOutputStream, directoryToZip);
- zipArchiveOutputStream.close();
+ addFilesToZip(zipArchiveOutputStream, directoryToZip);
+ }
}
@Test
@@ -688,8 +671,6 @@ public final class ZipTestCase extends AbstractTestCase {
StandardCharsets.UTF_8.toString(), true, false, true)) {
ArchiveEntry entry;
- File fileToCompare;
- InputStream inputStreamToCompare;
int filesNum = countNonDirectories(directoryToZip);
int filesCount = 0;
while ((entry = splitInputStream.getNextEntry()) != null) {
@@ -697,11 +678,11 @@ public final class ZipTestCase extends AbstractTestCase {
continue;
}
// compare all files one by one
- fileToCompare = new File(entry.getName());
- inputStreamToCompare = new FileInputStream(fileToCompare);
- Assert.assertTrue(
- shaded.org.apache.commons.io.IOUtils.contentEquals(splitInputStream, inputStreamToCompare));
- inputStreamToCompare.close();
+ File fileToCompare = new File(entry.getName());
+ try (InputStream inputStreamToCompare = new FileInputStream(fileToCompare)) {
+ Assert.assertTrue(
+ shaded.org.apache.commons.io.IOUtils.contentEquals(splitInputStream, inputStreamToCompare));
+ }
filesCount++;
}
// and the number of files should equal
@@ -766,55 +747,50 @@ public final class ZipTestCase extends AbstractTestCase {
private File getFilesToZip() throws IOException {
File originalZipFile = getFile("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip.zip");
- ZipFile zipFile = new ZipFile(originalZipFile);
- Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
- ZipArchiveEntry zipEntry;
- File outputFile;
- InputStream inputStream;
- OutputStream outputStream;
- byte[] buffer;
- int readLen;
-
- while (zipEntries.hasMoreElements()) {
- zipEntry = zipEntries.nextElement();
- if (zipEntry.isDirectory()) {
- continue;
- }
+ try (ZipFile zipFile = new ZipFile(originalZipFile)) {
+ Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
+ ZipArchiveEntry zipEntry;
+ File outputFile;
+ byte[] buffer;
+ int readLen;
+
+ while (zipEntries.hasMoreElements()) {
+ zipEntry = zipEntries.nextElement();
+ if (zipEntry.isDirectory()) {
+ continue;
+ }
- outputFile = new File(dir, zipEntry.getName());
- if (!outputFile.getParentFile().exists()) {
- outputFile.getParentFile().mkdirs();
- }
- outputFile = new File(dir, zipEntry.getName());
+ outputFile = new File(dir, zipEntry.getName());
+ if (!outputFile.getParentFile().exists()) {
+ outputFile.getParentFile().mkdirs();
+ }
+ outputFile = new File(dir, zipEntry.getName());
- inputStream = zipFile.getInputStream(zipEntry);
- outputStream = new FileOutputStream(outputFile);
- buffer = new byte[(int)zipEntry.getSize()];
- while((readLen = inputStream.read(buffer)) > 0) {
- outputStream.write(buffer, 0, readLen);
+ try (InputStream inputStream = zipFile.getInputStream(zipEntry);
+ OutputStream outputStream = new FileOutputStream(outputFile)) {
+ buffer = new byte[(int) zipEntry.getSize()];
+ while ((readLen = inputStream.read(buffer)) > 0) {
+ outputStream.write(buffer, 0, readLen);
+ }
+ }
}
-
- inputStream.close();
- outputStream.close();
}
-
return dir.listFiles()[0];
}
- private ZipArchiveOutputStream createTestSplitZipSegments() throws IOException {
+ private void createTestSplitZipSegments() throws IOException {
File directoryToZip = getFilesToZip();
File outputZipFile = new File(dir, "splitZip.zip");
long splitSize = 100 * 1024L; /* 100 KB */
- final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile, splitSize);
-
- addFilesToZip(zipArchiveOutputStream, directoryToZip);
- zipArchiveOutputStream.close();
- return zipArchiveOutputStream;
+ try (final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile,
+ splitSize)) {
+ addFilesToZip(zipArchiveOutputStream, directoryToZip);
+ }
}
private void addFilesToZip(ZipArchiveOutputStream zipArchiveOutputStream, File fileToAdd) throws IOException {
- if(fileToAdd.isDirectory()) {
- for(File file : fileToAdd.listFiles()) {
+ if (fileToAdd.isDirectory()) {
+ for (File file : fileToAdd.listFiles()) {
addFilesToZip(zipArchiveOutputStream, file);
}
} else {
@@ -822,7 +798,9 @@ public final class ZipTestCase extends AbstractTestCase {
zipArchiveEntry.setMethod(ZipEntry.DEFLATED);
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
- IOUtils.copy(new FileInputStream(fileToAdd), zipArchiveOutputStream);
+ try (final FileInputStream input = new FileInputStream(fileToAdd)) {
+ IOUtils.copy(input, zipArchiveOutputStream);
+ }
zipArchiveOutputStream.closeArchiveEntry();
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
index d94f294..ca6d032 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
@@ -52,22 +52,23 @@ public class ScatterSampleTest {
};
scatterSample.addEntry(archiveEntry, supp);
- final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(result);
- scatterSample.writeTo(zipArchiveOutputStream);
- zipArchiveOutputStream.close();
+ try (final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(result)) {
+ scatterSample.writeTo(zipArchiveOutputStream);
+ }
}
private void checkFile(final File result) throws IOException {
- final ZipFile zf = new ZipFile(result);
- final ZipArchiveEntry archiveEntry1 = zf.getEntries().nextElement();
- assertEquals( "test1.xml", archiveEntry1.getName());
- final InputStream inputStream = zf.getInputStream(archiveEntry1);
- final byte[] b = new byte[6];
- final int i = IOUtils.readFully(inputStream, b);
- assertEquals(5, i);
- assertEquals('H', b[0]);
- assertEquals('o', b[4]);
- zf.close();
+ try (final ZipFile zipFile = new ZipFile(result)) {
+ final ZipArchiveEntry archiveEntry1 = zipFile.getEntries().nextElement();
+ assertEquals("test1.xml", archiveEntry1.getName());
+ try (final InputStream inputStream = zipFile.getInputStream(archiveEntry1)) {
+ final byte[] b = new byte[6];
+ final int i = IOUtils.readFully(inputStream, b);
+ assertEquals(5, i);
+ assertEquals('H', b[0]);
+ assertEquals('o', b[4]);
+ }
+ }
result.delete();
}
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
index 721ab33..f9ad366 100644
--- a/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/brotli/BrotliCompressorInputStreamTest.java
@@ -45,13 +45,12 @@ public class BrotliCompressorInputStreamTest extends AbstractTestCase {
final File input = getFile("brotli.testdata.compressed");
final File expected = getFile("brotli.testdata.uncompressed");
try (InputStream inputStream = new FileInputStream(input);
- InputStream expectedStream = new FileInputStream(expected);
- BrotliCompressorInputStream brotliInputStream = new BrotliCompressorInputStream(inputStream)) {
+ BrotliCompressorInputStream brotliInputStream = new BrotliCompressorInputStream(inputStream)) {
final byte[] b = new byte[20];
- IOUtils.readFully(expectedStream, b);
+ IOUtils.read(expected, b);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
int readByte = -1;
- while((readByte = brotliInputStream.read()) != -1) {
+ while ((readByte = brotliInputStream.read()) != -1) {
bos.write(readByte);
}
Assert.assertArrayEquals(b, bos.toByteArray());
diff --git a/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java
index 82e5075..126be75 100644
--- a/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStreamTest.java
@@ -46,10 +46,7 @@ public final class FramedLZ4CompressorInputStreamTest
public void testMatches() throws IOException {
assertFalse(FramedLZ4CompressorInputStream.matches(new byte[10], 4));
final byte[] b = new byte[12];
- final File input = getFile("bla.tar.lz4");
- try (FileInputStream in = new FileInputStream(input)) {
- IOUtils.readFully(in, b);
- }
+ IOUtils.read(getFile("bla.tar.lz4"), b);
assertFalse(FramedLZ4CompressorInputStream.matches(b, 3));
assertTrue(FramedLZ4CompressorInputStream.matches(b, 4));
assertTrue(FramedLZ4CompressorInputStream.matches(b, 5));
diff --git a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
index 9d26d94..70e0940 100644
--- a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
@@ -40,10 +40,7 @@ public final class FramedSnappyCompressorInputStreamTest
public void testMatches() throws IOException {
assertFalse(FramedSnappyCompressorInputStream.matches(new byte[10], 10));
final byte[] b = new byte[12];
- final File input = getFile("bla.tar.sz");
- try (FileInputStream in = new FileInputStream(input)) {
- IOUtils.readFully(in, b);
- }
+ IOUtils.read(getFile("bla.tar.sz"), b);
assertFalse(FramedSnappyCompressorInputStream.matches(b, 9));
assertTrue(FramedSnappyCompressorInputStream.matches(b, 10));
assertTrue(FramedSnappyCompressorInputStream.matches(b, 12));
diff --git a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java
index 1d5f066..eae684b 100644
--- a/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStreamTest.java
@@ -47,10 +47,9 @@ public class ZstdCompressorInputStreamTest extends AbstractTestCase {
final File input = getFile("zstandard.testdata.zst");
final File expected = getFile("zstandard.testdata");
try (InputStream inputStream = new FileInputStream(input);
- InputStream expectedStream = new FileInputStream(expected);
ZstdCompressorInputStream zstdInputStream = new ZstdCompressorInputStream(inputStream)) {
final byte[] b = new byte[97];
- IOUtils.readFully(expectedStream, b);
+ IOUtils.read(expected, b);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
int readByte = -1;
while((readByte = zstdInputStream.read()) != -1) {