Skip to content

Commit 6673c58

Browse files
authored
Fix resource leaks in stream usage with try-with-resources (#21)
1 parent 1285a8d commit 6673c58

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

src/main/java/com/dampcake/bencode/Bencode.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ public Charset getCharset() {
124124
public Type type(final byte[] bytes) {
125125
if (bytes == null) throw new NullPointerException("bytes cannot be null");
126126

127-
BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes);
128-
129-
try {
127+
try (BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes)) {
130128
return in.nextType();
131129
} catch (Throwable t) {
132130
throw new BencodeException("Exception thrown during type detection", t);
@@ -152,9 +150,7 @@ public <T> T decode(final byte[] bytes, final Type<T> type) {
152150
if (type == null) throw new NullPointerException("type cannot be null");
153151
if (type == Type.UNKNOWN) throw new IllegalArgumentException("type cannot be UNKNOWN");
154152

155-
BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes);
156-
157-
try {
153+
try (BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes)) {
158154
if (type == Type.NUMBER)
159155
return (T) in.readNumber();
160156
if (type == Type.LIST)
@@ -243,9 +239,8 @@ public byte[] encode(final Map<?, ?> m) {
243239

244240
private byte[] encode(final Object o, final Type type) {
245241
ByteArrayOutputStream out = new ByteArrayOutputStream();
246-
BencodeOutputStream bencode = new BencodeOutputStream(out, charset);
247242

248-
try {
243+
try (BencodeOutputStream bencode = new BencodeOutputStream(out, charset)) {
249244
if (type == Type.STRING)
250245
bencode.writeString((String) o);
251246
else if (type == Type.NUMBER)

src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ private void instantiate(String s, boolean useBytes) {
3737
}
3838

3939
@Test
40+
@SuppressWarnings("resource")
4041
public void testConstructorNullStream() throws Exception {
4142
new BencodeInputStream(null);
4243
}
4344

4445
@Test(expected = NullPointerException.class)
46+
@SuppressWarnings("resource")
4547
public void testConstructorNullCharset() throws Exception {
4648
new BencodeInputStream(new ByteArrayInputStream(new byte[0]), null);
4749
}

src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public void setUp() {
3131
}
3232

3333
@Test
34+
@SuppressWarnings("resource")
3435
public void testConstructorNullStream() throws Exception {
3536
new BencodeOutputStream(null);
3637
}
3738

3839
@Test(expected = NullPointerException.class)
40+
@SuppressWarnings("resource")
3941
public void testConstructorNullCharset() throws Exception {
4042
new BencodeOutputStream(out, null);
4143
}

0 commit comments

Comments
 (0)