Skip to content

Commit 208d3db

Browse files
committed
Use try-with-resources.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1812844 13f79535-47bb-0310-9956-ffa450edef68
1 parent 28910ee commit 208d3db

13 files changed

Lines changed: 261 additions & 312 deletions

File tree

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,8 @@ public static MessageDigest updateDigest(final MessageDigest messageDigest, fina
896896
* @since 1.11
897897
*/
898898
public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException {
899-
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data));
900-
try {
899+
try (final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data))) {
901900
return updateDigest(digest, stream);
902-
} finally {
903-
stream.close();
904901
}
905902
}
906903

src/main/java/org/apache/commons/codec/digest/HmacUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,11 +1064,8 @@ public String hmacHex(final InputStream valueToDigest) throws IOException {
10641064
* @since 1.11
10651065
*/
10661066
public byte[] hmac(final File valueToDigest) throws IOException {
1067-
final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest));
1068-
try {
1067+
try (final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest))) {
10691068
return hmac(stream);
1070-
} finally {
1071-
stream.close();
10721069
}
10731070
}
10741071

src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,8 @@ public String toString() {
231231
throw new IllegalArgumentException("Unable to load resource: " + RESOURCE_FILE);
232232
}
233233

234-
final Scanner scanner = new Scanner(rulesIS, CharEncoding.UTF_8);
235-
try {
234+
try (final Scanner scanner = new Scanner(rulesIS, CharEncoding.UTF_8)) {
236235
parseRules(scanner, RESOURCE_FILE, RULES, FOLDINGS);
237-
} finally {
238-
scanner.close();
239236
}
240237

241238
// sort RULES by pattern length in descending order

src/main/java/org/apache/commons/codec/language/bm/Lang.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ public static Lang loadFromResource(final String languageRulesResourceName, fina
134134
throw new IllegalStateException("Unable to resolve required resource:" + LANGUAGE_RULES_RN);
135135
}
136136

137-
final Scanner scanner = new Scanner(lRulesIS, ResourceConstants.ENCODING);
138-
try {
137+
try (final Scanner scanner = new Scanner(lRulesIS, ResourceConstants.ENCODING)) {
139138
boolean inExtendedComment = false;
140139
while (scanner.hasNextLine()) {
141140
final String rawLine = scanner.nextLine();
@@ -178,8 +177,6 @@ public static Lang loadFromResource(final String languageRulesResourceName, fina
178177
}
179178
}
180179
}
181-
} finally {
182-
scanner.close();
183180
}
184181
return new Lang(rules, languages);
185182
}

src/main/java/org/apache/commons/codec/language/bm/Languages.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ public static Languages getInstance(final String languagesResourceName) {
175175
throw new IllegalArgumentException("Unable to resolve required resource: " + languagesResourceName);
176176
}
177177

178-
final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING);
179-
try {
178+
try (final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING)) {
180179
boolean inExtendedComment = false;
181180
while (lsScanner.hasNextLine()) {
182181
final String line = lsScanner.nextLine().trim();
@@ -192,8 +191,6 @@ public static Languages getInstance(final String languagesResourceName) {
192191
}
193192
}
194193
}
195-
} finally {
196-
lsScanner.close();
197194
}
198195

199196
return new Languages(Collections.unmodifiableSet(ls));

src/main/java/org/apache/commons/codec/language/bm/Rule.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,21 +219,15 @@ public boolean isMatch(final CharSequence input) {
219219

220220
final Languages ls = Languages.getInstance(s);
221221
for (final String l : ls.getLanguages()) {
222-
final Scanner scanner = createScanner(s, rt, l);
223-
try {
222+
try (final Scanner scanner = createScanner(s, rt, l)) {
224223
rs.put(l, parseRules(scanner, createResourceName(s, rt, l)));
225224
} catch (final IllegalStateException e) {
226225
throw new IllegalStateException("Problem processing " + createResourceName(s, rt, l), e);
227-
} finally {
228-
scanner.close();
229226
}
230227
}
231228
if (!rt.equals(RuleType.RULES)) {
232-
final Scanner scanner = createScanner(s, rt, "common");
233-
try {
229+
try (final Scanner scanner = createScanner(s, rt, "common")) {
234230
rs.put("common", parseRules(scanner, createResourceName(s, rt, "common")));
235-
} finally {
236-
scanner.close();
237231
}
238232
}
239233

@@ -443,11 +437,8 @@ private static Map<String, List<Rule>> parseRules(final Scanner scanner, final S
443437
throw new IllegalArgumentException("Malformed import statement '" + rawLine + "' in " +
444438
location);
445439
}
446-
final Scanner hashIncludeScanner = createScanner(incl);
447-
try {
440+
try (final Scanner hashIncludeScanner = createScanner(incl)) {
448441
lines.putAll(parseRules(hashIncludeScanner, location + "->" + incl));
449-
} finally {
450-
hashIncludeScanner.close();
451442
}
452443
} else {
453444
// rule

src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java

Lines changed: 78 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public class Base32InputStreamTest {
4747
@Test
4848
public void testCodec130() throws IOException {
4949
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
50-
final Base32OutputStream base32os = new Base32OutputStream(bos);
51-
52-
base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE));
53-
base32os.close();
50+
try (final Base32OutputStream base32os = new Base32OutputStream(bos)) {
51+
base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE));
52+
}
5453

5554
final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
5655
final Base32InputStream ins = new Base32InputStream(bis);
@@ -68,13 +67,10 @@ public void testCodec130() throws IOException {
6867
*/
6968
@Test
7069
public void testCodec105() throws IOException {
71-
final Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null);
72-
try {
70+
try (final Base32InputStream in = new Base32InputStream(new Codec105ErrorInputStream(), true, 0, null)) {
7371
for (int i = 0; i < 5; i++) {
7472
in.read();
7573
}
76-
} finally {
77-
in.close();
7874
}
7975
}
8076

@@ -152,15 +148,15 @@ public void testCodec105() throws IOException {
152148
@Test
153149
public void testAvailable() throws Throwable {
154150
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
155-
final Base32InputStream b32stream = new Base32InputStream(ins);
156-
assertEquals(1, b32stream.available());
157-
assertEquals(3, b32stream.skip(10));
158-
// End of stream reached
159-
assertEquals(0, b32stream.available());
160-
assertEquals(-1, b32stream.read());
161-
assertEquals(-1, b32stream.read());
162-
assertEquals(0, b32stream.available());
163-
b32stream.close();
151+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
152+
assertEquals(1, b32stream.available());
153+
assertEquals(3, b32stream.skip(10));
154+
// End of stream reached
155+
assertEquals(0, b32stream.available());
156+
assertEquals(-1, b32stream.read());
157+
assertEquals(-1, b32stream.read());
158+
assertEquals(0, b32stream.available());
159+
}
164160
}
165161

166162
/**
@@ -389,10 +385,10 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
389385
public void testMarkSupported() throws Exception {
390386
final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
391387
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
392-
final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
393-
// Always returns false for now.
394-
assertFalse("Base32InputStream.markSupported() is false", in.markSupported());
395-
in.close();
388+
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
389+
// Always returns false for now.
390+
assertFalse("Base32InputStream.markSupported() is false", in.markSupported());
391+
}
396392
}
397393

398394
/**
@@ -406,10 +402,10 @@ public void testRead0() throws Exception {
406402
final byte[] buf = new byte[1024];
407403
int bytesRead = 0;
408404
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
409-
final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
410-
bytesRead = in.read(buf, 0, 0);
411-
assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
412-
in.close();
405+
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
406+
bytesRead = in.read(buf, 0, 0);
407+
assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
408+
}
413409
}
414410

415411
/**
@@ -422,14 +418,12 @@ public void testRead0() throws Exception {
422418
public void testReadNull() throws Exception {
423419
final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
424420
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
425-
final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
426-
try {
421+
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
427422
in.read(null, 0, 0);
428423
fail("Base32InputStream.read(null, 0, 0) to throw a NullPointerException");
429424
} catch (final NullPointerException e) {
430425
// Expected
431426
}
432-
in.close();
433427
}
434428

435429
/**
@@ -442,36 +436,36 @@ public void testReadOutOfBounds() throws Exception {
442436
final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
443437
final byte[] buf = new byte[1024];
444438
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
445-
final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
439+
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
446440

447-
try {
448-
in.read(buf, -1, 0);
449-
fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
450-
} catch (final IndexOutOfBoundsException e) {
451-
// Expected
452-
}
441+
try {
442+
in.read(buf, -1, 0);
443+
fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
444+
} catch (final IndexOutOfBoundsException e) {
445+
// Expected
446+
}
453447

454-
try {
455-
in.read(buf, 0, -1);
456-
fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
457-
} catch (final IndexOutOfBoundsException e) {
458-
// Expected
459-
}
448+
try {
449+
in.read(buf, 0, -1);
450+
fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
451+
} catch (final IndexOutOfBoundsException e) {
452+
// Expected
453+
}
460454

461-
try {
462-
in.read(buf, buf.length + 1, 0);
463-
fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
464-
} catch (final IndexOutOfBoundsException e) {
465-
// Expected
466-
}
455+
try {
456+
in.read(buf, buf.length + 1, 0);
457+
fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
458+
} catch (final IndexOutOfBoundsException e) {
459+
// Expected
460+
}
467461

468-
try {
469-
in.read(buf, buf.length - 1, 2);
470-
fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
471-
} catch (final IndexOutOfBoundsException e) {
472-
// Expected
462+
try {
463+
in.read(buf, buf.length - 1, 2);
464+
fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
465+
} catch (final IndexOutOfBoundsException e) {
466+
// Expected
467+
}
473468
}
474-
in.close();
475469
}
476470

477471
/**
@@ -482,14 +476,14 @@ public void testReadOutOfBounds() throws Exception {
482476
@Test
483477
public void testSkipNone() throws Throwable {
484478
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
485-
final Base32InputStream b32stream = new Base32InputStream(ins);
486-
final byte[] actualBytes = new byte[6];
487-
assertEquals(0, b32stream.skip(0));
488-
b32stream.read(actualBytes, 0, actualBytes.length);
489-
assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 });
490-
// End of stream reached
491-
assertEquals(-1, b32stream.read());
492-
b32stream.close();
479+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
480+
final byte[] actualBytes = new byte[6];
481+
assertEquals(0, b32stream.skip(0));
482+
b32stream.read(actualBytes, 0, actualBytes.length);
483+
assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 });
484+
// End of stream reached
485+
assertEquals(-1, b32stream.read());
486+
}
493487
}
494488

495489
/**
@@ -500,12 +494,12 @@ public void testSkipNone() throws Throwable {
500494
@Test
501495
public void testSkipBig() throws Throwable {
502496
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
503-
final Base32InputStream b32stream = new Base32InputStream(ins);
504-
assertEquals(3, b32stream.skip(1024));
505-
// End of stream reached
506-
assertEquals(-1, b32stream.read());
507-
assertEquals(-1, b32stream.read());
508-
b32stream.close();
497+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
498+
assertEquals(3, b32stream.skip(1024));
499+
// End of stream reached
500+
assertEquals(-1, b32stream.read());
501+
assertEquals(-1, b32stream.read());
502+
}
509503
}
510504

511505
/**
@@ -516,13 +510,13 @@ public void testSkipBig() throws Throwable {
516510
@Test
517511
public void testSkipPastEnd() throws Throwable {
518512
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
519-
final Base32InputStream b32stream = new Base32InputStream(ins);
520-
// due to CODEC-130, skip now skips correctly decoded characters rather than encoded
521-
assertEquals(3, b32stream.skip(10));
522-
// End of stream reached
523-
assertEquals(-1, b32stream.read());
524-
assertEquals(-1, b32stream.read());
525-
b32stream.close();
513+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
514+
// due to CODEC-130, skip now skips correctly decoded characters rather than encoded
515+
assertEquals(3, b32stream.skip(10));
516+
// End of stream reached
517+
assertEquals(-1, b32stream.read());
518+
assertEquals(-1, b32stream.read());
519+
}
526520
}
527521

528522
/**
@@ -533,13 +527,13 @@ public void testSkipPastEnd() throws Throwable {
533527
@Test
534528
public void testSkipToEnd() throws Throwable {
535529
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
536-
final Base32InputStream b32stream = new Base32InputStream(ins);
537-
// due to CODEC-130, skip now skips correctly decoded characters rather than encoded
538-
assertEquals(3, b32stream.skip(3));
539-
// End of stream reached
540-
assertEquals(-1, b32stream.read());
541-
assertEquals(-1, b32stream.read());
542-
b32stream.close();
530+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
531+
// due to CODEC-130, skip now skips correctly decoded characters rather than encoded
532+
assertEquals(3, b32stream.skip(3));
533+
// End of stream reached
534+
assertEquals(-1, b32stream.read());
535+
assertEquals(-1, b32stream.read());
536+
}
543537
}
544538

545539
/**
@@ -550,8 +544,8 @@ public void testSkipToEnd() throws Throwable {
550544
@Test(expected=IllegalArgumentException.class)
551545
public void testSkipWrongArgument() throws Throwable {
552546
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
553-
final Base32InputStream b32stream = new Base32InputStream(ins);
554-
b32stream.skip(-10);
555-
b32stream.close();
547+
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
548+
b32stream.skip(-10);
549+
}
556550
}
557551
}

0 commit comments

Comments
 (0)