Skip to content

Commit f13d80e

Browse files
committed
Use try-with-resources
1 parent db5e1e7 commit f13d80e

2 files changed

Lines changed: 29 additions & 33 deletions

File tree

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,17 @@ public void testBase64InputStreamByteByByte() throws Exception {
295295
* Usually signifies a bug in the Base64 commons-codec implementation.
296296
*/
297297
private void testByChunk(final byte[] encoded, final byte[] decoded, final int chunkSize, final byte[] separator) throws Exception {
298-
299298
// Start with encode.
300-
InputStream in;
301-
in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator);
302-
byte[] output = BaseNTestData.streamToBytes(in);
303-
304-
assertEquals(-1, in.read(), "EOF");
305-
assertEquals(-1, in.read(), "Still EOF");
306-
assertArrayEquals(encoded, output, "Streaming base64 encode");
307-
308-
in.close();
299+
try (InputStream in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, separator)) {
300+
byte[] output = BaseNTestData.streamToBytes(in);
301+
assertEquals(-1, in.read(), "EOF");
302+
assertEquals(-1, in.read(), "Still EOF");
303+
assertArrayEquals(encoded, output, "Streaming base64 encode");
304+
}
309305

310306
// Now let's try to decode.
311-
in = new Base64InputStream(new ByteArrayInputStream(encoded));
312-
output = BaseNTestData.streamToBytes(in);
307+
InputStream in = new Base64InputStream(new ByteArrayInputStream(encoded));
308+
byte[] output = BaseNTestData.streamToBytes(in);
313309

314310
assertEquals(-1, in.read(), "EOF");
315311
assertEquals(-1, in.read(), "Still EOF");

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)