diff --git a/src/main/java/com/dampcake/bencode/Bencode.java b/src/main/java/com/dampcake/bencode/Bencode.java index 0591c23..89788bd 100644 --- a/src/main/java/com/dampcake/bencode/Bencode.java +++ b/src/main/java/com/dampcake/bencode/Bencode.java @@ -124,9 +124,7 @@ public Charset getCharset() { public Type type(final byte[] bytes) { if (bytes == null) throw new NullPointerException("bytes cannot be null"); - BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes); - - try { + try (BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes)) { return in.nextType(); } catch (Throwable t) { throw new BencodeException("Exception thrown during type detection", t); @@ -152,9 +150,7 @@ public T decode(final byte[] bytes, final Type type) { if (type == null) throw new NullPointerException("type cannot be null"); if (type == Type.UNKNOWN) throw new IllegalArgumentException("type cannot be UNKNOWN"); - BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes); - - try { + try (BencodeInputStream in = new BencodeInputStream(new ByteArrayInputStream(bytes), charset, useBytes)) { if (type == Type.NUMBER) return (T) in.readNumber(); if (type == Type.LIST) @@ -243,9 +239,8 @@ public byte[] encode(final Map m) { private byte[] encode(final Object o, final Type type) { ByteArrayOutputStream out = new ByteArrayOutputStream(); - BencodeOutputStream bencode = new BencodeOutputStream(out, charset); - try { + try (BencodeOutputStream bencode = new BencodeOutputStream(out, charset)) { if (type == Type.STRING) bencode.writeString((String) o); else if (type == Type.NUMBER) diff --git a/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java b/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java index 605a49f..8cf35ec 100644 --- a/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java +++ b/src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java @@ -37,11 +37,13 @@ private void instantiate(String s, boolean useBytes) { } @Test + @SuppressWarnings("resource") public void testConstructorNullStream() throws Exception { new BencodeInputStream(null); } @Test(expected = NullPointerException.class) + @SuppressWarnings("resource") public void testConstructorNullCharset() throws Exception { new BencodeInputStream(new ByteArrayInputStream(new byte[0]), null); } diff --git a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java index e03db94..76bacfd 100644 --- a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java +++ b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java @@ -31,11 +31,13 @@ public void setUp() { } @Test + @SuppressWarnings("resource") public void testConstructorNullStream() throws Exception { new BencodeOutputStream(null); } @Test(expected = NullPointerException.class) + @SuppressWarnings("resource") public void testConstructorNullCharset() throws Exception { new BencodeOutputStream(out, null); }