Skip to content

Commit 559eed1

Browse files
committed
2 parents 70eaa82 + c5c8326 commit 559eed1

4 files changed

Lines changed: 55 additions & 65 deletions

File tree

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

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

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);

src/test/java/org/apache/commons/codec/digest/MessageDigestAlgorithmsTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,11 @@ public void testNonBlockingDigestRandomAccessFile(final String messageDigestAlgo
209209

210210
final byte[] expected = digestTestData(messageDigestAlgorithm);
211211

212-
assertArrayEquals(expected,
213-
DigestUtils.digest(
214-
DigestUtils.getDigest(messageDigestAlgorithm), getTestRandomAccessFile()
215-
)
216-
);
217-
getTestRandomAccessFile().seek(0);
218-
assertArrayEquals(expected,
219-
DigestUtils.digest(
220-
DigestUtils.getDigest(messageDigestAlgorithm), getTestRandomAccessFile()
221-
)
222-
);
212+
@SuppressWarnings("resource") // test manages RAF
213+
final RandomAccessFile randomAccessFile = getTestRandomAccessFile();
214+
assertArrayEquals(expected, DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), randomAccessFile));
215+
randomAccessFile.seek(0);
216+
assertArrayEquals(expected, DigestUtils.digest(DigestUtils.getDigest(messageDigestAlgorithm), randomAccessFile));
223217
}
224218

225219
}

0 commit comments

Comments
 (0)