@@ -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