@@ -191,23 +191,23 @@ private void testByChunk(final byte[] encoded, final byte[] decoded, final int c
191191
192192 // Start with encode.
193193 ByteArrayOutputStream byteOut = new ByteArrayOutputStream ();
194- OutputStream out = new Base64OutputStream (byteOut , true , chunkSize , separator );
195- out .write (decoded );
196- out . close ();
194+ try ( OutputStream out = new Base64OutputStream (byteOut , true , chunkSize , separator )) {
195+ out .write (decoded );
196+ }
197197 byte [] output = byteOut .toByteArray ();
198198 assertArrayEquals (encoded , output , "Streaming chunked base64 encode" );
199199
200200 // Now let's try to decode.
201201 byteOut = new ByteArrayOutputStream ();
202- out = new Base64OutputStream (byteOut , false );
203- out .write (encoded );
204- out . close ();
202+ try ( OutputStream out = new Base64OutputStream (byteOut , false )) {
203+ out .write (encoded );
204+ }
205205 output = byteOut .toByteArray ();
206206 assertArrayEquals (decoded , output , "Streaming chunked base64 decode" );
207207
208208 // I always wanted to do this! (wrap encoder with decoder etc.).
209209 byteOut = new ByteArrayOutputStream ();
210- out = byteOut ;
210+ OutputStream out = byteOut ;
211211 for (int i = 0 ; i < 10 ; i ++) {
212212 out = new Base64OutputStream (out , false );
213213 out = new Base64OutputStream (out , true , chunkSize , separator );
@@ -241,38 +241,38 @@ private void testByteByByte(final byte[] encoded, final byte[] decoded, final in
241241
242242 // Start with encode.
243243 ByteArrayOutputStream byteOut = new ByteArrayOutputStream ();
244- OutputStream out = new Base64OutputStream (byteOut , true , chunkSize , separator );
245- for (final byte element : decoded ) {
246- out .write (element );
244+ try (OutputStream out = new Base64OutputStream (byteOut , true , chunkSize , separator )) {
245+ for (final byte element : decoded ) {
246+ out .write (element );
247+ }
247248 }
248- out .close ();
249249 byte [] output = byteOut .toByteArray ();
250250 assertArrayEquals (encoded , output , "Streaming byte-by-byte base64 encode" );
251251
252252 // Now let's try to decode.
253253 byteOut = new ByteArrayOutputStream ();
254- out = new Base64OutputStream (byteOut , false );
255- for (final byte element : encoded ) {
256- out .write (element );
254+ try (OutputStream out = new Base64OutputStream (byteOut , false )) {
255+ for (final byte element : encoded ) {
256+ out .write (element );
257+ }
257258 }
258- out .close ();
259259 output = byteOut .toByteArray ();
260260 assertArrayEquals (decoded , output , "Streaming byte-by-byte base64 decode" );
261261
262262 // Now let's try to decode with tonnes of flushes.
263263 byteOut = new ByteArrayOutputStream ();
264- out = new Base64OutputStream (byteOut , false );
265- for (final byte element : encoded ) {
266- out .write (element );
267- out .flush ();
264+ try (OutputStream out = new Base64OutputStream (byteOut , false )) {
265+ for (final byte element : encoded ) {
266+ out .write (element );
267+ out .flush ();
268+ }
268269 }
269- out .close ();
270270 output = byteOut .toByteArray ();
271271 assertArrayEquals (decoded , output , "Streaming byte-by-byte flush() base64 decode" );
272272
273273 // I always wanted to do this! (wrap encoder with decoder etc.).
274274 byteOut = new ByteArrayOutputStream ();
275- out = byteOut ;
275+ OutputStream out = byteOut ;
276276 for (int i = 0 ; i < 10 ; i ++) {
277277 out = new Base64OutputStream (out , false );
278278 out = new Base64OutputStream (out , true , chunkSize , separator );
0 commit comments