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
commit 5ff8af5fdffb355c57d1e83a8c8a1bd288546a00 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Aug 29 15:22:55 2023 -0400 Use try-with-resources --- .../bzip2/BZip2CompressorInputStreamTest.java | 40 ++++++++++------------ .../bzip2/PythonTruncatedBzip2Test.java | 6 ++-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java index 7c32c828..57c62bd2 100644 --- a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java @@ -54,13 +54,11 @@ public class BZip2CompressorInputStreamTest extends AbstractTestCase { public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { final File input = getFile("bla.txt.bz2"); final byte[] buf = new byte[2]; - try (InputStream is = Files.newInputStream(input.toPath())) { - final BZip2CompressorInputStream in = - new BZip2CompressorInputStream(is); + try (InputStream is = Files.newInputStream(input.toPath()); + final BZip2CompressorInputStream in = new BZip2CompressorInputStream(is)) { IOUtils.toByteArray(in); assertEquals(-1, in.read(buf)); assertEquals(-1, in.read(buf)); - in.close(); } } @@ -71,27 +69,27 @@ public class BZip2CompressorInputStreamTest extends AbstractTestCase { public void readOfLength0ShouldReturn0() throws Exception { // Create a big random piece of data final byte[] rawData = new byte[1048576]; - for (int i=0; i < rawData.length; ++i) { - rawData[i] = (byte) Math.floor(Math.random()*256); + for (int i = 0; i < rawData.length; ++i) { + rawData[i] = (byte) Math.floor(Math.random() * 256); } // Compress it final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos); - bzipOut.write(rawData); - bzipOut.flush(); - bzipOut.close(); - baos.flush(); - baos.close(); + try (BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos)) { + bzipOut.write(rawData); + bzipOut.flush(); + bzipOut.close(); + baos.flush(); + } // Try to read it back in final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); - final BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais); - final byte[] buffer = new byte[1024]; - assertEquals(1024, bzipIn.read(buffer, 0, 1024)); - assertEquals(0, bzipIn.read(buffer, 1024, 0)); - assertEquals(1024, bzipIn.read(buffer, 0, 1024)); - bzipIn.close(); + try (BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais)) { + final byte[] buffer = new byte[1024]; + assertEquals(1024, bzipIn.read(buffer, 0, 1024)); + assertEquals(0, bzipIn.read(buffer, 1024, 0)); + assertEquals(1024, bzipIn.read(buffer, 0, 1024)); + } } @Test @@ -150,13 +148,11 @@ public class BZip2CompressorInputStreamTest extends AbstractTestCase { @Test public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException { final File input = getFile("bla.txt.bz2"); - try (InputStream is = Files.newInputStream(input.toPath())) { - final BZip2CompressorInputStream in = - new BZip2CompressorInputStream(is); + try (InputStream is = Files.newInputStream(input.toPath()); + final BZip2CompressorInputStream in = new BZip2CompressorInputStream(is)) { IOUtils.toByteArray(in); assertEquals(-1, in.read()); assertEquals(-1, in.read()); - in.close(); } } } diff --git a/src/test/java/org/apache/commons/compress/compressors/bzip2/PythonTruncatedBzip2Test.java b/src/test/java/org/apache/commons/compress/compressors/bzip2/PythonTruncatedBzip2Test.java index 1bfe717b..376d5b96 100644 --- a/src/test/java/org/apache/commons/compress/compressors/bzip2/PythonTruncatedBzip2Test.java +++ b/src/test/java/org/apache/commons/compress/compressors/bzip2/PythonTruncatedBzip2Test.java @@ -57,9 +57,9 @@ public class PythonTruncatedBzip2Test { @BeforeAll public static void initializeTestData() throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final BZip2CompressorOutputStream bz2out = new BZip2CompressorOutputStream(out); - bz2out.write(TEXT.getBytes(), 0, TEXT.getBytes().length); - bz2out.close(); + try (BZip2CompressorOutputStream bz2out = new BZip2CompressorOutputStream(out)) { + bz2out.write(TEXT.getBytes(), 0, TEXT.getBytes().length); + } DATA = out.toByteArray(); // Drop the eos_magic field (6 bytes) and CRC (4 bytes).