Skip to content

Commit

Permalink
Support encoding of byte arrays (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
aruthane committed Jul 8, 2023
1 parent 85050a6 commit 8823b21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/dampcake/bencode/BencodeOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ public void writeString(final ByteBuffer buff) throws IOException {
write(encode(buff.array()));
}

/**
* Writes the passed byte[] to the stream.
*
* @param array the byte[] to write to the stream
*
* @throws NullPointerException if the byte[] is null
* @throws IOException if the underlying stream throws
*
* @since 1.4.1
*/
public void writeString(final byte[] array) throws IOException {
write(encode(array));
}

/**
* Writes the passed {@link Number} to the stream.
* <p>
Expand Down Expand Up @@ -218,6 +232,8 @@ private byte[] encodeObject(final Object o) throws IOException {
return encode((Map<?, ?>) o);
if (o instanceof ByteBuffer)
return encode(((ByteBuffer) o).array());
if (o instanceof byte[])
return encode((byte[]) o);

return encode(o.toString());
}
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,33 @@ public void run() throws Exception {
}

@Test
public void testWriteStringByteArray() throws Exception {
public void testWriteStringByteBuffer() throws Exception {
instance.writeString(ByteBuffer.wrap("Hello World!".getBytes()));

assertEquals("12:Hello World!", new String(out.toByteArray(), instance.getCharset()));
}

@Test
public void testWriteStringEmptyByteArray() throws Exception {
public void testWriteStringEmptyByteBuffer() throws Exception {
instance.writeString(ByteBuffer.wrap(new byte[0]));

assertEquals("0:", new String(out.toByteArray(), instance.getCharset()));
}

@Test
public void testWriteStringByteArray() throws Exception {
instance.writeString("Hello World!".getBytes());

assertEquals("12:Hello World!", new String(out.toByteArray(), instance.getCharset()));
}

@Test
public void testWriteStringEmptyByteArray() throws Exception {
instance.writeString(new byte[0]);

assertEquals("0:", new String(out.toByteArray(), instance.getCharset()));
}

@Test
public void testWriteStringNullByteArray() throws Exception {
assertThrows(NullPointerException.class, new Runnable() {
Expand Down Expand Up @@ -124,9 +138,10 @@ public void testWriteList() throws Exception {
add(123);
add(456);
}});
add("Foo".getBytes());
}});

assertEquals("l5:Hello6:World!li123ei456eee", new String(out.toByteArray(), instance.getCharset()));
assertEquals("l5:Hello6:World!li123ei456ee3:Fooe", new String(out.toByteArray(), instance.getCharset()));
}

@Test
Expand Down

0 comments on commit 8823b21

Please sign in to comment.