Author: ggregory Date: Sat Oct 21 18:27:03 2017 New Revision: 1812844 URL: http://svn.apache.org/viewvc?rev=1812844&view=rev Log: Use try-with-resources.
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Lang.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Languages.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Rule.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java Sat Oct 21 18:27:03 2017 @@ -896,11 +896,8 @@ public class DigestUtils { * @since 1.11 */ public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException { - final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data)); - try { + try (final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data))) { return updateDigest(digest, stream); - } finally { - stream.close(); } } Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java Sat Oct 21 18:27:03 2017 @@ -1064,11 +1064,8 @@ public final class HmacUtils { * @since 1.11 */ public byte[] hmac(final File valueToDigest) throws IOException { - final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest)); - try { + try (final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest))) { return hmac(stream); - } finally { - stream.close(); } } Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java Sat Oct 21 18:27:03 2017 @@ -231,11 +231,8 @@ public class DaitchMokotoffSoundex imple throw new IllegalArgumentException("Unable to load resource: " + RESOURCE_FILE); } - final Scanner scanner = new Scanner(rulesIS, CharEncoding.UTF_8); - try { + try (final Scanner scanner = new Scanner(rulesIS, CharEncoding.UTF_8)) { parseRules(scanner, RESOURCE_FILE, RULES, FOLDINGS); - } finally { - scanner.close(); } // sort RULES by pattern length in descending order Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Lang.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Lang.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Lang.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Lang.java Sat Oct 21 18:27:03 2017 @@ -134,8 +134,7 @@ public class Lang { throw new IllegalStateException("Unable to resolve required resource:" + LANGUAGE_RULES_RN); } - final Scanner scanner = new Scanner(lRulesIS, ResourceConstants.ENCODING); - try { + try (final Scanner scanner = new Scanner(lRulesIS, ResourceConstants.ENCODING)) { boolean inExtendedComment = false; while (scanner.hasNextLine()) { final String rawLine = scanner.nextLine(); @@ -178,8 +177,6 @@ public class Lang { } } } - } finally { - scanner.close(); } return new Lang(rules, languages); } Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Languages.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Languages.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Languages.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Languages.java Sat Oct 21 18:27:03 2017 @@ -175,8 +175,7 @@ public class Languages { throw new IllegalArgumentException("Unable to resolve required resource: " + languagesResourceName); } - final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING); - try { + try (final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING)) { boolean inExtendedComment = false; while (lsScanner.hasNextLine()) { final String line = lsScanner.nextLine().trim(); @@ -192,8 +191,6 @@ public class Languages { } } } - } finally { - lsScanner.close(); } return new Languages(Collections.unmodifiableSet(ls)); Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Rule.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Rule.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Rule.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/language/bm/Rule.java Sat Oct 21 18:27:03 2017 @@ -219,21 +219,15 @@ public class Rule { final Languages ls = Languages.getInstance(s); for (final String l : ls.getLanguages()) { - final Scanner scanner = createScanner(s, rt, l); - try { + try (final Scanner scanner = createScanner(s, rt, l)) { rs.put(l, parseRules(scanner, createResourceName(s, rt, l))); } catch (final IllegalStateException e) { throw new IllegalStateException("Problem processing " + createResourceName(s, rt, l), e); - } finally { - scanner.close(); } } if (!rt.equals(RuleType.RULES)) { - final Scanner scanner = createScanner(s, rt, "common"); - try { + try (final Scanner scanner = createScanner(s, rt, "common")) { rs.put("common", parseRules(scanner, createResourceName(s, rt, "common"))); - } finally { - scanner.close(); } } @@ -443,11 +437,8 @@ public class Rule { throw new IllegalArgumentException("Malformed import statement '" + rawLine + "' in " + location); } - final Scanner hashIncludeScanner = createScanner(incl); - try { + try (final Scanner hashIncludeScanner = createScanner(incl)) { lines.putAll(parseRules(hashIncludeScanner, location + "->" + incl)); - } finally { - hashIncludeScanner.close(); } } else { // rule Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java Sat Oct 21 18:27:03 2017 @@ -47,10 +47,9 @@ public class Base32InputStreamTest { @Test public void testCodec130() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final Base32OutputStream base32os = new Base32OutputStream(bos); - - base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); - base32os.close(); + try (final Base32OutputStream base32os = new Base32OutputStream(bos)) { + base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); + } final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Base32InputStream ins = new Base32InputStream(bis); @@ -68,13 +67,10 @@ public class Base32InputStreamTest { */ @Test public void testCodec105() throws IOException { - final Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null); - try { + try (final Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null)) { for (int i = 0; i < 5; i++) { in.read(); } - } finally { - in.close(); } } @@ -152,15 +148,15 @@ public class Base32InputStreamTest { @Test public void testAvailable() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - assertEquals(1, b32stream.available()); - assertEquals(3, b32stream.skip(10)); - // End of stream reached - assertEquals(0, b32stream.available()); - assertEquals(-1, b32stream.read()); - assertEquals(-1, b32stream.read()); - assertEquals(0, b32stream.available()); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + assertEquals(1, b32stream.available()); + assertEquals(3, b32stream.skip(10)); + // End of stream reached + assertEquals(0, b32stream.available()); + assertEquals(-1, b32stream.read()); + assertEquals(-1, b32stream.read()); + assertEquals(0, b32stream.available()); + } } /** @@ -389,10 +385,10 @@ public class Base32InputStreamTest { public void testMarkSupported() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - // Always returns false for now. - assertFalse("Base32InputStream.markSupported() is false", in.markSupported()); - in.close(); + try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { + // Always returns false for now. + assertFalse("Base32InputStream.markSupported() is false", in.markSupported()); + } } /** @@ -406,10 +402,10 @@ public class Base32InputStreamTest { final byte[] buf = new byte[1024]; int bytesRead = 0; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - bytesRead = in.read(buf, 0, 0); - assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); - in.close(); + try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { + bytesRead = in.read(buf, 0, 0); + assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); + } } /** @@ -422,14 +418,12 @@ public class Base32InputStreamTest { public void testReadNull() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - try { + try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { in.read(null, 0, 0); fail("Base32InputStream.read(null, 0, 0) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected } - in.close(); } /** @@ -442,36 +436,36 @@ public class Base32InputStreamTest { final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); final byte[] buf = new byte[1024]; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); + try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { - try { - in.read(buf, -1, 0); - fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, -1, 0); + fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, 0, -1); - fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, 0, -1); + fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, buf.length + 1, 0); - fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, buf.length + 1, 0); + fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, buf.length - 1, 2); - fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected + try { + in.read(buf, buf.length - 1, 2); + fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } } - in.close(); } /** @@ -482,14 +476,14 @@ public class Base32InputStreamTest { @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - final byte[] actualBytes = new byte[6]; - assertEquals(0, b32stream.skip(0)); - b32stream.read(actualBytes, 0, actualBytes.length); - assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 }); - // End of stream reached - assertEquals(-1, b32stream.read()); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + final byte[] actualBytes = new byte[6]; + assertEquals(0, b32stream.skip(0)); + b32stream.read(actualBytes, 0, actualBytes.length); + assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 }); + // End of stream reached + assertEquals(-1, b32stream.read()); + } } /** @@ -500,12 +494,12 @@ public class Base32InputStreamTest { @Test public void testSkipBig() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - assertEquals(3, b32stream.skip(1024)); - // End of stream reached - assertEquals(-1, b32stream.read()); - assertEquals(-1, b32stream.read()); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + assertEquals(3, b32stream.skip(1024)); + // End of stream reached + assertEquals(-1, b32stream.read()); + assertEquals(-1, b32stream.read()); + } } /** @@ -516,13 +510,13 @@ public class Base32InputStreamTest { @Test public void testSkipPastEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - // due to CODEC-130, skip now skips correctly decoded characters rather than encoded - assertEquals(3, b32stream.skip(10)); - // End of stream reached - assertEquals(-1, b32stream.read()); - assertEquals(-1, b32stream.read()); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + // due to CODEC-130, skip now skips correctly decoded characters rather than encoded + assertEquals(3, b32stream.skip(10)); + // End of stream reached + assertEquals(-1, b32stream.read()); + assertEquals(-1, b32stream.read()); + } } /** @@ -533,13 +527,13 @@ public class Base32InputStreamTest { @Test public void testSkipToEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - // due to CODEC-130, skip now skips correctly decoded characters rather than encoded - assertEquals(3, b32stream.skip(3)); - // End of stream reached - assertEquals(-1, b32stream.read()); - assertEquals(-1, b32stream.read()); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + // due to CODEC-130, skip now skips correctly decoded characters rather than encoded + assertEquals(3, b32stream.skip(3)); + // End of stream reached + assertEquals(-1, b32stream.read()); + assertEquals(-1, b32stream.read()); + } } /** @@ -550,8 +544,8 @@ public class Base32InputStreamTest { @Test(expected=IllegalArgumentException.class) public void testSkipWrongArgument() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); - final Base32InputStream b32stream = new Base32InputStream(ins); - b32stream.skip(-10); - b32stream.close(); + try (final Base32InputStream b32stream = new Base32InputStream(ins)) { + b32stream.skip(-10); + } } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java Sat Oct 21 18:27:03 2017 @@ -284,36 +284,36 @@ public class Base32OutputStreamTest { public void testWriteOutOfBounds() throws Exception { final byte[] buf = new byte[1024]; final ByteArrayOutputStream bout = new ByteArrayOutputStream(); - final Base32OutputStream out = new Base32OutputStream(bout); + try (final Base32OutputStream out = new Base32OutputStream(bout)) { - try { - out.write(buf, -1, 1); - fail("Expected Base32OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } - - try { - out.write(buf, 1, -1); - fail("Expected Base32OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } - - try { - out.write(buf, buf.length + 1, 0); - fail("Expected Base32OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } - - try { - out.write(buf, buf.length - 1, 2); - fail("Expected Base32OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected + try { + out.write(buf, -1, 1); + fail("Expected Base32OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, 1, -1); + fail("Expected Base32OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, buf.length + 1, 0); + fail("Expected Base32OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, buf.length - 1, 2); + fail("Expected Base32OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } } - out.close(); } /** @@ -325,14 +325,12 @@ public class Base32OutputStreamTest { @Test public void testWriteToNullCoverage() throws Exception { final ByteArrayOutputStream bout = new ByteArrayOutputStream(); - final Base32OutputStream out = new Base32OutputStream(bout); - try { + try (final Base32OutputStream out = new Base32OutputStream(bout)) { out.write(null, 0, 0); fail("Expcted Base32OutputStream.write(null) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected } - out.close(); } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java Sat Oct 21 18:27:03 2017 @@ -57,10 +57,9 @@ public class Base64InputStreamTest { @Test public void testCodec130() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final Base64OutputStream base64os = new Base64OutputStream(bos); - - base64os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); - base64os.close(); + try (final Base64OutputStream base64os = new Base64OutputStream(bos)) { + base64os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); + } final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Base64InputStream ins = new Base64InputStream(bis); @@ -78,13 +77,10 @@ public class Base64InputStreamTest { */ @Test public void testCodec105() throws IOException { - final Base64InputStream in = new Base64InputStream(new Codec105ErrorInputStream(), true, 0, null); - try { + try (final Base64InputStream in = new Base64InputStream(new Codec105ErrorInputStream(), true, 0, null)) { for (int i = 0; i < 5; i++) { in.read(); } - } finally { - in.close(); } } @@ -98,14 +94,14 @@ public class Base64InputStreamTest { public void testCodec101() throws Exception { final byte[] codec101 = StringUtils.getBytesUtf8(Base64TestData.CODEC_101_MULTIPLE_OF_3); final ByteArrayInputStream bais = new ByteArrayInputStream(codec101); - final Base64InputStream in = new Base64InputStream(bais); - final byte[] result = new byte[8192]; - int c = in.read(result); - assertTrue("Codec101: First read successful [c=" + c + "]", c > 0); + try (final Base64InputStream in = new Base64InputStream(bais)) { + final byte[] result = new byte[8192]; + int c = in.read(result); + assertTrue("Codec101: First read successful [c=" + c + "]", c > 0); - c = in.read(result); - assertTrue("Codec101: Second read should report end-of-stream [c=" + c + "]", c < 0); - in.close(); + c = in.read(result); + assertTrue("Codec101: Second read should report end-of-stream [c=" + c + "]", c < 0); + } } /** @@ -129,10 +125,10 @@ public class Base64InputStreamTest { final ByteArrayInputStream bais = new ByteArrayInputStream(codec101); final Base64InputStream in = new Base64InputStream(bais); final InputStreamReader isr = new InputStreamReader(in); - final BufferedReader br = new BufferedReader(isr); - final String line = br.readLine(); - assertNotNull("Codec101: InputStreamReader works!", line); - br.close(); + try (final BufferedReader br = new BufferedReader(isr)) { + final String line = br.readLine(); + assertNotNull("Codec101: InputStreamReader works!", line); + } } /** @@ -162,15 +158,15 @@ public class Base64InputStreamTest { @Test public void testAvailable() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - assertEquals(1, b64stream.available()); - assertEquals(6, b64stream.skip(10)); - // End of stream reached - assertEquals(0, b64stream.available()); - assertEquals(-1, b64stream.read()); - assertEquals(-1, b64stream.read()); - assertEquals(0, b64stream.available()); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + assertEquals(1, b64stream.available()); + assertEquals(6, b64stream.skip(10)); + // End of stream reached + assertEquals(0, b64stream.available()); + assertEquals(-1, b64stream.read()); + assertEquals(-1, b64stream.read()); + assertEquals(0, b64stream.available()); + } } /** @@ -403,10 +399,10 @@ public class Base64InputStreamTest { public void testMarkSupported() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - // Always returns false for now. - assertFalse("Base64InputStream.markSupported() is false", in.markSupported()); - in.close(); + try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { + // Always returns false for now. + assertFalse("Base64InputStream.markSupported() is false", in.markSupported()); + } } /** @@ -420,10 +416,10 @@ public class Base64InputStreamTest { final byte[] buf = new byte[1024]; int bytesRead = 0; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - bytesRead = in.read(buf, 0, 0); - assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); - in.close(); + try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { + bytesRead = in.read(buf, 0, 0); + assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead); + } } /** @@ -436,14 +432,12 @@ public class Base64InputStreamTest { public void testReadNull() throws Exception { final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE); final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); - try { + try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { in.read(null, 0, 0); fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected } - in.close(); } /** @@ -456,36 +450,36 @@ public class Base64InputStreamTest { final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE); final byte[] buf = new byte[1024]; final ByteArrayInputStream bin = new ByteArrayInputStream(decoded); - final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 }); + try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) { - try { - in.read(buf, -1, 0); - fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, -1, 0); + fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, 0, -1); - fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, 0, -1); + fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, buf.length + 1, 0); - fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected - } + try { + in.read(buf, buf.length + 1, 0); + fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } - try { - in.read(buf, buf.length - 1, 2); - fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException e) { - // Expected + try { + in.read(buf, buf.length - 1, 2); + fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException e) { + // Expected + } } - in.close(); } /** @@ -496,12 +490,12 @@ public class Base64InputStreamTest { @Test public void testSkipBig() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - assertEquals(6, b64stream.skip(Integer.MAX_VALUE)); - // End of stream reached - assertEquals(-1, b64stream.read()); - assertEquals(-1, b64stream.read()); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + assertEquals(6, b64stream.skip(Integer.MAX_VALUE)); + // End of stream reached + assertEquals(-1, b64stream.read()); + assertEquals(-1, b64stream.read()); + } } /** @@ -512,14 +506,14 @@ public class Base64InputStreamTest { @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - final byte[] actualBytes = new byte[6]; - assertEquals(0, b64stream.skip(0)); - b64stream.read(actualBytes, 0, actualBytes.length); - assertArrayEquals(actualBytes, new byte[] { 0, 0, 0, (byte) 255, (byte) 255, (byte) 255 }); - // End of stream reached - assertEquals(-1, b64stream.read()); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + final byte[] actualBytes = new byte[6]; + assertEquals(0, b64stream.skip(0)); + b64stream.read(actualBytes, 0, actualBytes.length); + assertArrayEquals(actualBytes, new byte[] { 0, 0, 0, (byte) 255, (byte) 255, (byte) 255 }); + // End of stream reached + assertEquals(-1, b64stream.read()); + } } /** @@ -530,13 +524,13 @@ public class Base64InputStreamTest { @Test public void testSkipPastEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - // due to CODEC-130, skip now skips correctly decoded characters rather than encoded - assertEquals(6, b64stream.skip(10)); - // End of stream reached - assertEquals(-1, b64stream.read()); - assertEquals(-1, b64stream.read()); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + // due to CODEC-130, skip now skips correctly decoded characters rather than encoded + assertEquals(6, b64stream.skip(10)); + // End of stream reached + assertEquals(-1, b64stream.read()); + assertEquals(-1, b64stream.read()); + } } /** @@ -547,13 +541,13 @@ public class Base64InputStreamTest { @Test public void testSkipToEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - // due to CODEC-130, skip now skips correctly decoded characters rather than encoded - assertEquals(6, b64stream.skip(6)); - // End of stream reached - assertEquals(-1, b64stream.read()); - assertEquals(-1, b64stream.read()); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + // due to CODEC-130, skip now skips correctly decoded characters rather than encoded + assertEquals(6, b64stream.skip(6)); + // End of stream reached + assertEquals(-1, b64stream.read()); + assertEquals(-1, b64stream.read()); + } } /** @@ -564,8 +558,8 @@ public class Base64InputStreamTest { @Test(expected=IllegalArgumentException.class) public void testSkipWrongArgument() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); - final Base64InputStream b64stream = new Base64InputStream(ins); - b64stream.skip(-10); - b64stream.close(); + try (final Base64InputStream b64stream = new Base64InputStream(ins)) { + b64stream.skip(-10); + } } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java Sat Oct 21 18:27:03 2017 @@ -51,15 +51,13 @@ public class Base64OutputStreamTest { final byte[] codec98_1024 = new byte[1024]; System.arraycopy(codec98, 0, codec98_1024, 0, codec98.length); final ByteArrayOutputStream data = new ByteArrayOutputStream(1024); - final Base64OutputStream stream = new Base64OutputStream(data, false); - stream.write(codec98_1024, 0, 1024); - stream.close(); + try (final Base64OutputStream stream = new Base64OutputStream(data, false)) { + stream.write(codec98_1024, 0, 1024); + } final byte[] decodedBytes = data.toByteArray(); final String decoded = StringUtils.newStringUtf8(decodedBytes); - assertEquals( - "codec-98 NPE Base64OutputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded - ); + assertEquals("codec-98 NPE Base64OutputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded); } @@ -295,36 +293,36 @@ public class Base64OutputStreamTest { public void testWriteOutOfBounds() throws Exception { final byte[] buf = new byte[1024]; final ByteArrayOutputStream bout = new ByteArrayOutputStream(); - final Base64OutputStream out = new Base64OutputStream(bout); - - try { - out.write(buf, -1, 1); - fail("Expected Base64OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } - - try { - out.write(buf, 1, -1); - fail("Expected Base64OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } - - try { - out.write(buf, buf.length + 1, 0); - fail("Expected Base64OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected - } + try (final Base64OutputStream out = new Base64OutputStream(bout)) { - try { - out.write(buf, buf.length - 1, 2); - fail("Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException ioobe) { - // Expected + try { + out.write(buf, -1, 1); + fail("Expected Base64OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, 1, -1); + fail("Expected Base64OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, buf.length + 1, 0); + fail("Expected Base64OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } + + try { + out.write(buf, buf.length - 1, 2); + fail("Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException"); + } catch (final IndexOutOfBoundsException ioobe) { + // Expected + } } - out.close(); } /** @@ -336,14 +334,11 @@ public class Base64OutputStreamTest { @Test public void testWriteToNullCoverage() throws Exception { final ByteArrayOutputStream bout = new ByteArrayOutputStream(); - final Base64OutputStream out = new Base64OutputStream(bout); - try { + try (final Base64OutputStream out = new Base64OutputStream(bout)) { out.write(null, 0, 0); fail("Expcted Base64OutputStream.write(null) to throw a NullPointerException"); } catch (final NullPointerException e) { // Expected - } finally { - out.close(); } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java Sat Oct 21 18:27:03 2017 @@ -64,9 +64,9 @@ public class DigestUtilsTest { public void setUp() throws Exception { new Random().nextBytes(testData); testFile = File.createTempFile(DigestUtilsTest.class.getName(), ".dat"); - final FileOutputStream fos = new FileOutputStream(testFile); - fos.write(testData); - fos.close(); + try (final FileOutputStream fos = new FileOutputStream(testFile)) { + fos.write(testData); + } } @After Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/PureJavaCrc32Test.java Sat Oct 21 18:27:03 2017 @@ -184,30 +184,25 @@ public class PureJavaCrc32Test { return b.toString(); } - /** Generate CRC-32 lookup tables */ - public static void main(final String[] args) throws FileNotFoundException { - if (args.length != 1) { - System.err.println("Usage: " + Table.class.getName() + - " <polynomial>"); - System.exit(1); - } - final long polynomial = Long.parseLong(args[0], 16); + /** Generate CRC-32 lookup tables */ + public static void main(final String[] args) throws FileNotFoundException { + if (args.length != 1) { + System.err.println("Usage: " + Table.class.getName() + " <polynomial>"); + System.exit(1); + } + final long polynomial = Long.parseLong(args[0], 16); - final int i = 8; - final Table t = new Table(i, 16, polynomial); - final String s = t.toString(); - System.out.println(s); + final int i = 8; + final Table t = new Table(i, 16, polynomial); + final String s = t.toString(); + System.out.println(s); - //print to a file - final PrintStream out = new PrintStream( - new FileOutputStream("table" + i + ".txt"), true); - try { - out.println(s); - } finally { - out.close(); - } + // print to a file + try (final PrintStream out = new PrintStream(new FileOutputStream("table" + i + ".txt"), true)) { + out.println(s); + } + } } - } /** * Performance tests to compare performance of the Pure Java implementation Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java?rev=1812844&r1=1812843&r2=1812844&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java Sat Oct 21 18:27:03 2017 @@ -69,12 +69,9 @@ public class XXHash32Test { @Test public void verifyChecksum() throws IOException { final XXHash32 h = new XXHash32(); - final FileInputStream s = new FileInputStream(file); - try { + try (final FileInputStream s = new FileInputStream(file)) { final byte[] b = toByteArray(s); h.update(b, 0, b.length); - } finally { - s.close(); } Assert.assertEquals("checksum for " + file.getName(), expectedChecksum, Long.toHexString(h.getValue())); }