Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/main/java/com/dampcake/bencode/Bencode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -152,9 +150,7 @@ public <T> T decode(final byte[] bytes, final Type<T> 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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down