Skip to content

Commit 7943c4a

Browse files
authored
Merge pull request #63 from Marcono1234/test-try-with-resources
Use try-with-resources for TestFileStreams
2 parents 41ec802 + 854b202 commit 7943c4a

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/test/java/com/ning/compress/lzf/TestLZFInputStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void setUp() throws Exception
3131
System.arraycopy(bytes, 0, bytesToWrite, cursor, (bytes.length+cursor < bytesToWrite.length)?bytes.length:bytesToWrite.length-cursor);
3232
cursor += bytes.length;
3333
}
34-
ByteArrayOutputStream nonCompressed = new ByteArrayOutputStream();
34+
ByteArrayOutputStream nonCompressed = new ByteArrayOutputStream();
3535
OutputStream os = new LZFOutputStream(nonCompressed);
3636
os.write(nonEncodableBytesToWrite);
3737
os.close();
@@ -88,7 +88,7 @@ public void testAvailable() throws IOException
8888
assertSame(bis, is.getUnderlyingInputStream());
8989
assertEquals(0, is.available());
9090
// read one byte; should decode bunch more, make available
91-
is.read();
91+
assertNotEquals(-1, is.read());
9292
int total = 1; // since we read one byte already
9393
assertEquals(65534, is.available());
9494
// and after we skip through all of it, end with -1 for EOF

src/test/java/com/ning/compress/lzf/util/TestFileStreams.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public void testStreams() throws Exception
2424
// First, write encoded stuff (won't compress, but produces something)
2525
byte[] input = "Whatever stuff...".getBytes(StandardCharsets.UTF_8);
2626

27-
LZFFileOutputStream out = new LZFFileOutputStream(f);
28-
out.write(input);
29-
out.close();
27+
try (LZFFileOutputStream out = new LZFFileOutputStream(f)) {
28+
out.write(input);
29+
}
3030

3131
long len = f.length();
3232
// happens to be 22; 17 bytes uncompressed, with 5 byte header
3333
assertEquals(22L, len);
3434

35-
LZFFileInputStream in = new LZFFileInputStream(f);
36-
for (byte b : input) {
37-
assertEquals(b & 0xFF, in.read());
35+
try (LZFFileInputStream in = new LZFFileInputStream(f)) {
36+
for (byte b : input) {
37+
assertEquals(b & 0xFF, in.read());
38+
}
39+
assertEquals(-1, in.read());
3840
}
39-
assertEquals(-1, in.read());
40-
in.close();
4141
}
4242

4343
@Test
@@ -46,14 +46,14 @@ public void testReadAndWrite() throws Exception
4646
File f = tempDir.resolve("lzf-test.lzf").toFile();
4747

4848
byte[] fluff = constructFluff(132000);
49-
LZFFileOutputStream fout = new LZFFileOutputStream(f);
50-
fout.write(fluff);
51-
fout.close();
52-
53-
LZFFileInputStream in = new LZFFileInputStream(f);
49+
try (LZFFileOutputStream fout = new LZFFileOutputStream(f)) {
50+
fout.write(fluff);
51+
}
52+
5453
ByteArrayOutputStream bytes = new ByteArrayOutputStream(fluff.length);
55-
in.readAndWrite(bytes);
56-
in.close();
54+
try (LZFFileInputStream in = new LZFFileInputStream(f)) {
55+
in.readAndWrite(bytes);
56+
}
5757
byte[] actual = bytes.toByteArray();
5858
assertArrayEquals(fluff, actual);
5959
}

0 commit comments

Comments
 (0)