Skip to content

Commit 8823b21

Browse files
authored
Support encoding of byte arrays (#14)
1 parent 85050a6 commit 8823b21

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ public void writeString(final ByteBuffer buff) throws IOException {
9393
write(encode(buff.array()));
9494
}
9595

96+
/**
97+
* Writes the passed byte[] to the stream.
98+
*
99+
* @param array the byte[] to write to the stream
100+
*
101+
* @throws NullPointerException if the byte[] is null
102+
* @throws IOException if the underlying stream throws
103+
*
104+
* @since 1.4.1
105+
*/
106+
public void writeString(final byte[] array) throws IOException {
107+
write(encode(array));
108+
}
109+
96110
/**
97111
* Writes the passed {@link Number} to the stream.
98112
* <p>
@@ -218,6 +232,8 @@ private byte[] encodeObject(final Object o) throws IOException {
218232
return encode((Map<?, ?>) o);
219233
if (o instanceof ByteBuffer)
220234
return encode(((ByteBuffer) o).array());
235+
if (o instanceof byte[])
236+
return encode((byte[]) o);
221237

222238
return encode(o.toString());
223239
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,33 @@ public void run() throws Exception {
6666
}
6767

6868
@Test
69-
public void testWriteStringByteArray() throws Exception {
69+
public void testWriteStringByteBuffer() throws Exception {
7070
instance.writeString(ByteBuffer.wrap("Hello World!".getBytes()));
7171

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

7575
@Test
76-
public void testWriteStringEmptyByteArray() throws Exception {
76+
public void testWriteStringEmptyByteBuffer() throws Exception {
7777
instance.writeString(ByteBuffer.wrap(new byte[0]));
7878

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

82+
@Test
83+
public void testWriteStringByteArray() throws Exception {
84+
instance.writeString("Hello World!".getBytes());
85+
86+
assertEquals("12:Hello World!", new String(out.toByteArray(), instance.getCharset()));
87+
}
88+
89+
@Test
90+
public void testWriteStringEmptyByteArray() throws Exception {
91+
instance.writeString(new byte[0]);
92+
93+
assertEquals("0:", new String(out.toByteArray(), instance.getCharset()));
94+
}
95+
8296
@Test
8397
public void testWriteStringNullByteArray() throws Exception {
8498
assertThrows(NullPointerException.class, new Runnable() {
@@ -124,9 +138,10 @@ public void testWriteList() throws Exception {
124138
add(123);
125139
add(456);
126140
}});
141+
add("Foo".getBytes());
127142
}});
128143

129-
assertEquals("l5:Hello6:World!li123ei456eee", new String(out.toByteArray(), instance.getCharset()));
144+
assertEquals("l5:Hello6:World!li123ei456ee3:Fooe", new String(out.toByteArray(), instance.getCharset()));
130145
}
131146

132147
@Test

0 commit comments

Comments
 (0)