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-codec.git
commit f13d80ecb575e0dca18f2a7a66d431ed9a0643e1 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Aug 29 08:20:59 2023 -0400 Use try-with-resources --- .../codec/binary/Base64InputStreamTest.java | 20 +++++------ .../codec/binary/Base64OutputStreamTest.java | 42 +++++++++++----------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java index 30bd668a..06622eb1 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java @@ -295,21 +295,17 @@ public class Base64InputStreamTest { * Usually signifies a bug in the Base64 commons-codec implementation. */ private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception { - // Start with encode. - InputStream in; - in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator); - byte[] output = BaseNTestData.streamToBytes(in); - - assertEquals(-1, in.read(), "EOF"); - assertEquals(-1, in.read(), "Still EOF"); - assertArrayEquals(encoded, output, "Streaming base64 encode"); - - in.close(); + try (InputStream in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator)) { + byte[] output = BaseNTestData.streamToBytes(in); + assertEquals(-1, in.read(), "EOF"); + assertEquals(-1, in.read(), "Still EOF"); + assertArrayEquals(encoded, output, "Streaming base64 encode"); + } // Now let's try to decode. - in = new Base64InputStream(new ByteArrayInputStream(encoded)); - output = BaseNTestData.streamToBytes(in); + InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded)); + byte[] output = BaseNTestData.streamToBytes(in); assertEquals(-1, in.read(), "EOF"); assertEquals(-1, in.read(), "Still EOF"); diff --git a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java index 1448a14b..4c1d229f 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java @@ -191,23 +191,23 @@ public class Base64OutputStreamTest { // Start with encode. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator); - out.write(decoded); - out.close(); + try (OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator)) { + out.write(decoded); + } byte[] output = byteOut.toByteArray(); assertArrayEquals(encoded, output, "Streaming chunked base64 encode"); // Now let's try to decode. byteOut = new ByteArrayOutputStream(); - out = new Base64OutputStream(byteOut, false); - out.write(encoded); - out.close(); + try (OutputStream out = new Base64OutputStream(byteOut, false)) { + out.write(encoded); + } output = byteOut.toByteArray(); assertArrayEquals(decoded, output, "Streaming chunked base64 decode"); // I always wanted to do this! (wrap encoder with decoder etc.). byteOut = new ByteArrayOutputStream(); - out = byteOut; + OutputStream out = byteOut; for (int i = 0; i < 10; i++) { out = new Base64OutputStream(out, false); out = new Base64OutputStream(out, true, chunkSize, separator); @@ -241,38 +241,38 @@ public class Base64OutputStreamTest { // Start with encode. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator); - for (final byte element : decoded) { - out.write(element); + try (OutputStream out = new Base64OutputStream(byteOut, true, chunkSize, separator)) { + for (final byte element : decoded) { + out.write(element); + } } - out.close(); byte[] output = byteOut.toByteArray(); assertArrayEquals(encoded, output, "Streaming byte-by-byte base64 encode"); // Now let's try to decode. byteOut = new ByteArrayOutputStream(); - out = new Base64OutputStream(byteOut, false); - for (final byte element : encoded) { - out.write(element); + try (OutputStream out = new Base64OutputStream(byteOut, false)) { + for (final byte element : encoded) { + out.write(element); + } } - out.close(); output = byteOut.toByteArray(); assertArrayEquals(decoded, output, "Streaming byte-by-byte base64 decode"); // Now let's try to decode with tonnes of flushes. byteOut = new ByteArrayOutputStream(); - out = new Base64OutputStream(byteOut, false); - for (final byte element : encoded) { - out.write(element); - out.flush(); + try (OutputStream out = new Base64OutputStream(byteOut, false)) { + for (final byte element : encoded) { + out.write(element); + out.flush(); + } } - out.close(); output = byteOut.toByteArray(); assertArrayEquals(decoded, output, "Streaming byte-by-byte flush() base64 decode"); // I always wanted to do this! (wrap encoder with decoder etc.). byteOut = new ByteArrayOutputStream(); - out = byteOut; + OutputStream out = byteOut; for (int i = 0; i < 10; i++) { out = new Base64OutputStream(out, false); out = new Base64OutputStream(out, true, chunkSize, separator);