diff --git a/src/main/java/com/dampcake/bencode/BencodeOutputStream.java b/src/main/java/com/dampcake/bencode/BencodeOutputStream.java index 8253158..0bb9a47 100644 --- a/src/main/java/com/dampcake/bencode/BencodeOutputStream.java +++ b/src/main/java/com/dampcake/bencode/BencodeOutputStream.java @@ -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. *
@@ -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()); } diff --git a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java index 60920b0..e03db94 100644 --- a/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java +++ b/src/test/java/com/dampcake/bencode/BencodeOutputStreamTest.java @@ -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() { @@ -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