Skip to content

Commit 717ff5c

Browse files
committed
Eclipse warns that files are not closed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1379025 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0951540 commit 717ff5c

2 files changed

Lines changed: 48 additions & 17 deletions

File tree

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public void testAvailable() throws Throwable {
160160
assertEquals(-1, b32stream.read());
161161
assertEquals(-1, b32stream.read());
162162
assertEquals(0, b32stream.available());
163+
b32stream.close();
163164
}
164165

165166
/**
@@ -285,17 +286,17 @@ public void testBase32InputStreamByteByByte() throws Exception {
285286
private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
286287

287288
// Start with encode.
288-
InputStream in = new ByteArrayInputStream(decoded);
289-
in = new Base32InputStream(in, true, chunkSize, seperator);
289+
InputStream in;
290+
291+
in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, seperator);
290292
byte[] output = Base32TestData.streamToBytes(in);
291293

292294
assertEquals("EOF", -1, in.read());
293295
assertEquals("Still EOF", -1, in.read());
294296
assertTrue("Streaming base32 encode", Arrays.equals(output, encoded));
295297

296298
// Now let's try decode.
297-
in = new ByteArrayInputStream(encoded);
298-
in = new Base32InputStream(in);
299+
in = new Base32InputStream(new ByteArrayInputStream(encoded));
299300
output = Base32TestData.streamToBytes(in);
300301

301302
assertEquals("EOF", -1, in.read());
@@ -313,6 +314,7 @@ private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] s
313314
assertEquals("EOF", -1, in.read());
314315
assertEquals("Still EOF", -1, in.read());
315316
assertTrue("Streaming base32 wrap-wrap-wrap!", Arrays.equals(output, decoded));
317+
in.close();
316318
}
317319

318320
/**
@@ -336,8 +338,8 @@ private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] s
336338
private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
337339

338340
// Start with encode.
339-
InputStream in = new ByteArrayInputStream(decoded);
340-
in = new Base32InputStream(in, true, chunkSize, seperator);
341+
InputStream in;
342+
in = new Base32InputStream(new ByteArrayInputStream(decoded), true, chunkSize, seperator);
341343
byte[] output = new byte[encoded.length];
342344
for (int i = 0; i < output.length; i++) {
343345
output[i] = (byte) in.read();
@@ -347,9 +349,10 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
347349
assertEquals("Still EOF", -1, in.read());
348350
assertTrue("Streaming base32 encode", Arrays.equals(output, encoded));
349351

352+
in.close();
353+
350354
// Now let's try decode.
351-
in = new ByteArrayInputStream(encoded);
352-
in = new Base32InputStream(in);
355+
in = new Base32InputStream(new ByteArrayInputStream(encoded));
353356
output = new byte[decoded.length];
354357
for (int i = 0; i < output.length; i++) {
355358
output[i] = (byte) in.read();
@@ -359,6 +362,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
359362
assertEquals("Still EOF", -1, in.read());
360363
assertTrue("Streaming base32 decode", Arrays.equals(output, decoded));
361364

365+
in.close();
366+
362367
// I always wanted to do this! (wrap encoder with decoder etc etc).
363368
in = new ByteArrayInputStream(decoded);
364369
for (int i = 0; i < 10; i++) {
@@ -387,6 +392,7 @@ public void testMarkSupported() throws Exception {
387392
Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
388393
// Always returns false for now.
389394
assertFalse("Base32InputStream.markSupported() is false", in.markSupported());
395+
in.close();
390396
}
391397

392398
/**
@@ -403,6 +409,7 @@ public void testRead0() throws Exception {
403409
Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
404410
bytesRead = in.read(buf, 0, 0);
405411
assertEquals("Base32InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
412+
in.close();
406413
}
407414

408415
/**
@@ -422,6 +429,7 @@ public void testReadNull() throws Exception {
422429
} catch (NullPointerException e) {
423430
// Expected
424431
}
432+
in.close();
425433
}
426434

427435
/**
@@ -463,6 +471,7 @@ public void testReadOutOfBounds() throws Exception {
463471
} catch (IndexOutOfBoundsException e) {
464472
// Expected
465473
}
474+
in.close();
466475
}
467476

468477
/**
@@ -480,6 +489,7 @@ public void testSkipNone() throws Throwable {
480489
assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 });
481490
// End of stream reached
482491
assertEquals(-1, b32stream.read());
492+
b32stream.close();
483493
}
484494

485495
/**
@@ -495,6 +505,7 @@ public void testSkipBig() throws Throwable {
495505
// End of stream reached
496506
assertEquals(-1, b32stream.read());
497507
assertEquals(-1, b32stream.read());
508+
b32stream.close();
498509
}
499510

500511
/**
@@ -511,7 +522,8 @@ public void testSkipPastEnd() throws Throwable {
511522
// End of stream reached
512523
assertEquals(-1, b32stream.read());
513524
assertEquals(-1, b32stream.read());
514-
}
525+
b32stream.close();
526+
}
515527

516528
/**
517529
* Tests skipping to the end of a stream.
@@ -527,6 +539,7 @@ public void testSkipToEnd() throws Throwable {
527539
// End of stream reached
528540
assertEquals(-1, b32stream.read());
529541
assertEquals(-1, b32stream.read());
542+
b32stream.close();
530543
}
531544

532545
/**
@@ -539,5 +552,6 @@ public void testSkipWrongArgument() throws Throwable {
539552
InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
540553
Base32InputStream b32stream = new Base32InputStream(ins);
541554
b32stream.skip(-10);
555+
b32stream.close();
542556
}
543557
}

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public void testCodec101() throws Exception {
105105

106106
c = in.read(result);
107107
assertTrue("Codec101: Second read should report end-of-stream [c=" + c + "]", c < 0);
108+
in.close();
108109
}
109110

110111
/**
@@ -131,6 +132,7 @@ public void testInputStreamReader() throws Exception {
131132
BufferedReader br = new BufferedReader(isr);
132133
String line = br.readLine();
133134
assertNotNull("Codec101: InputStreamReader works!", line);
135+
br.close();
134136
}
135137

136138
/**
@@ -168,6 +170,7 @@ public void testAvailable() throws Throwable {
168170
assertEquals(-1, b64stream.read());
169171
assertEquals(-1, b64stream.read());
170172
assertEquals(0, b64stream.available());
173+
b64stream.close();
171174
}
172175

173176
/**
@@ -296,17 +299,18 @@ public void testBase64InputStreamByteByByte() throws Exception {
296299
private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
297300

298301
// Start with encode.
299-
InputStream in = new ByteArrayInputStream(decoded);
300-
in = new Base64InputStream(in, true, chunkSize, seperator);
302+
InputStream in;
303+
in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, seperator);
301304
byte[] output = Base64TestData.streamToBytes(in);
302305

303306
assertEquals("EOF", -1, in.read());
304307
assertEquals("Still EOF", -1, in.read());
305308
assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
306309

310+
in.close();
311+
307312
// Now let's try decode.
308-
in = new ByteArrayInputStream(encoded);
309-
in = new Base64InputStream(in);
313+
in = new Base64InputStream(new ByteArrayInputStream(encoded));
310314
output = Base64TestData.streamToBytes(in);
311315

312316
assertEquals("EOF", -1, in.read());
@@ -324,6 +328,7 @@ private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] s
324328
assertEquals("EOF", -1, in.read());
325329
assertEquals("Still EOF", -1, in.read());
326330
assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
331+
in.close();
327332
}
328333

329334
/**
@@ -347,8 +352,8 @@ private void testByChunk(byte[] encoded, byte[] decoded, int chunkSize, byte[] s
347352
private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[] seperator) throws Exception {
348353

349354
// Start with encode.
350-
InputStream in = new ByteArrayInputStream(decoded);
351-
in = new Base64InputStream(in, true, chunkSize, seperator);
355+
InputStream in;
356+
in = new Base64InputStream(new ByteArrayInputStream(decoded), true, chunkSize, seperator);
352357
byte[] output = new byte[encoded.length];
353358
for (int i = 0; i < output.length; i++) {
354359
output[i] = (byte) in.read();
@@ -358,9 +363,9 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
358363
assertEquals("Still EOF", -1, in.read());
359364
assertTrue("Streaming base64 encode", Arrays.equals(output, encoded));
360365

366+
in.close();
361367
// Now let's try decode.
362-
in = new ByteArrayInputStream(encoded);
363-
in = new Base64InputStream(in);
368+
in = new Base64InputStream(new ByteArrayInputStream(encoded));
364369
output = new byte[decoded.length];
365370
for (int i = 0; i < output.length; i++) {
366371
output[i] = (byte) in.read();
@@ -370,6 +375,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
370375
assertEquals("Still EOF", -1, in.read());
371376
assertTrue("Streaming base64 decode", Arrays.equals(output, decoded));
372377

378+
in.close();
379+
373380
// I always wanted to do this! (wrap encoder with decoder etc etc).
374381
in = new ByteArrayInputStream(decoded);
375382
for (int i = 0; i < 10; i++) {
@@ -384,6 +391,7 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
384391
assertEquals("EOF", -1, in.read());
385392
assertEquals("Still EOF", -1, in.read());
386393
assertTrue("Streaming base64 wrap-wrap-wrap!", Arrays.equals(output, decoded));
394+
in.close();
387395
}
388396

389397
/**
@@ -398,6 +406,7 @@ public void testMarkSupported() throws Exception {
398406
Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
399407
// Always returns false for now.
400408
assertFalse("Base64InputStream.markSupported() is false", in.markSupported());
409+
in.close();
401410
}
402411

403412
/**
@@ -414,6 +423,7 @@ public void testRead0() throws Exception {
414423
Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 });
415424
bytesRead = in.read(buf, 0, 0);
416425
assertEquals("Base64InputStream.read(buf, 0, 0) returns 0", 0, bytesRead);
426+
in.close();
417427
}
418428

419429
/**
@@ -433,6 +443,7 @@ public void testReadNull() throws Exception {
433443
} catch (NullPointerException e) {
434444
// Expected
435445
}
446+
in.close();
436447
}
437448

438449
/**
@@ -474,6 +485,7 @@ public void testReadOutOfBounds() throws Exception {
474485
} catch (IndexOutOfBoundsException e) {
475486
// Expected
476487
}
488+
in.close();
477489
}
478490

479491
/**
@@ -489,6 +501,7 @@ public void testSkipBig() throws Throwable {
489501
// End of stream reached
490502
assertEquals(-1, b64stream.read());
491503
assertEquals(-1, b64stream.read());
504+
b64stream.close();
492505
}
493506

494507
/**
@@ -506,6 +519,7 @@ public void testSkipNone() throws Throwable {
506519
assertArrayEquals(actualBytes, new byte[] { 0, 0, 0, (byte) 255, (byte) 255, (byte) 255 });
507520
// End of stream reached
508521
assertEquals(-1, b64stream.read());
522+
b64stream.close();
509523
}
510524

511525
/**
@@ -522,6 +536,7 @@ public void testSkipPastEnd() throws Throwable {
522536
// End of stream reached
523537
assertEquals(-1, b64stream.read());
524538
assertEquals(-1, b64stream.read());
539+
b64stream.close();
525540
}
526541

527542
/**
@@ -538,6 +553,7 @@ public void testSkipToEnd() throws Throwable {
538553
// End of stream reached
539554
assertEquals(-1, b64stream.read());
540555
assertEquals(-1, b64stream.read());
556+
b64stream.close();
541557
}
542558

543559
/**
@@ -550,5 +566,6 @@ public void testSkipWrongArgument() throws Throwable {
550566
InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64));
551567
Base64InputStream b64stream = new Base64InputStream(ins);
552568
b64stream.skip(-10);
569+
b64stream.close();
553570
}
554571
}

0 commit comments

Comments
 (0)