@@ -181,23 +181,23 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
181181
182182 // Start with encode.
183183 ByteArrayOutputStream byteOut = new ByteArrayOutputStream ();
184- OutputStream out = new Base32OutputStream (byteOut , true , chunkSize , separator );
185- out .write (decoded );
186- out . close ();
184+ try ( OutputStream out = new Base32OutputStream (byteOut , true , chunkSize , separator )) {
185+ out .write (decoded );
186+ }
187187 byte [] output = byteOut .toByteArray ();
188188 assertArrayEquals (encoded , output , "Streaming chunked Base32 encode" );
189189
190190 // Now let's try to decode.
191191 byteOut = new ByteArrayOutputStream ();
192- out = new Base32OutputStream (byteOut , false );
193- out .write (encoded );
194- out . close ();
192+ try ( OutputStream out = new Base32OutputStream (byteOut , false )) {
193+ out .write (encoded );
194+ }
195195 output = byteOut .toByteArray ();
196196 assertArrayEquals (decoded , output , "Streaming chunked Base32 decode" );
197197
198198 // I always wanted to do this! (wrap encoder with decoder etc.).
199199 byteOut = new ByteArrayOutputStream ();
200- out = byteOut ;
200+ OutputStream out = byteOut ;
201201 for (int i = 0 ; i < 10 ; i ++) {
202202 out = new Base32OutputStream (out , false );
203203 out = new Base32OutputStream (out , true , chunkSize , separator );
@@ -231,38 +231,38 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
231231
232232 // Start with encode.
233233 ByteArrayOutputStream byteOut = new ByteArrayOutputStream ();
234- OutputStream out = new Base32OutputStream (byteOut , true , chunkSize , separator );
235- for (final byte element : decoded ) {
236- out .write (element );
234+ try (OutputStream out = new Base32OutputStream (byteOut , true , chunkSize , separator )) {
235+ for (final byte element : decoded ) {
236+ out .write (element );
237+ }
237238 }
238- out .close ();
239239 byte [] output = byteOut .toByteArray ();
240240 assertArrayEquals (encoded , output , "Streaming byte-by-byte Base32 encode" );
241241
242242 // Now let's try to decode.
243243 byteOut = new ByteArrayOutputStream ();
244- out = new Base32OutputStream (byteOut , false );
245- for (final byte element : encoded ) {
246- out .write (element );
244+ try (OutputStream out = new Base32OutputStream (byteOut , false )) {
245+ for (final byte element : encoded ) {
246+ out .write (element );
247+ }
247248 }
248- out .close ();
249249 output = byteOut .toByteArray ();
250250 assertArrayEquals (decoded , output , "Streaming byte-by-byte Base32 decode" );
251251
252252 // Now let's try to decode with tonnes of flushes.
253253 byteOut = new ByteArrayOutputStream ();
254- out = new Base32OutputStream (byteOut , false );
255- for (final byte element : encoded ) {
256- out .write (element );
257- out .flush ();
254+ try (OutputStream out = new Base32OutputStream (byteOut , false )) {
255+ for (final byte element : encoded ) {
256+ out .write (element );
257+ out .flush ();
258+ }
258259 }
259- out .close ();
260260 output = byteOut .toByteArray ();
261261 assertArrayEquals (decoded , output , "Streaming byte-by-byte flush() Base32 decode" );
262262
263263 // I always wanted to do this! (wrap encoder with decoder etc.).
264264 byteOut = new ByteArrayOutputStream ();
265- out = byteOut ;
265+ OutputStream out = byteOut ;
266266 for (int i = 0 ; i < 10 ; i ++) {
267267 out = new Base32OutputStream (out , false );
268268 out = new Base32OutputStream (out , true , chunkSize , separator );
0 commit comments