Skip to content

Commit

Permalink
Added some tests and fixed String decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jksiezni committed May 1, 2015
1 parent 31001b7 commit c9bb3c8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main/java/org/ardverk/coding/BencodingInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object read(BencodingInputStream in) throws IOException {
} else if (token == BencodingUtils.NUMBER) {
return in.readNumber();
} else if (isDigit(token)) {
byte[] data = in.raw();
byte[] data = in.readBytes();
return in.decodeAsString ? new String(data, in.charset) : data;
} else {
return in.readCustom();
Expand Down Expand Up @@ -130,10 +130,6 @@ protected int peek() throws IOException {
return value;
}

private byte[] raw() throws IOException {
return raw(pop());
}

private byte[] raw(int length) throws IOException {
byte[] data = new byte[length];
readFully(data);
Expand All @@ -148,14 +144,24 @@ private void check(int expected) throws IOException {
}

public byte[] readBytes() throws IOException {
StringBuilder sb = new StringBuilder();

int value = -1;
while ((value = pop()) != BencodingUtils.LENGTH_DELIMITER) {
sb.append((char)value);
}

return raw(Integer.parseInt(sb.toString()));
int token = pop();
if (token == -1) {
throw new EOFException();
}

return readBytes(token);
}

private byte[] readBytes(int token) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append((char)token);

int value = -1;
while ((value = pop()) != BencodingUtils.LENGTH_DELIMITER) {
sb.append((char)value);
}

return raw(Integer.parseInt(sb.toString()));
}

/**
Expand Down Expand Up @@ -312,7 +318,7 @@ public <T> Map<String, T> readMap(Map<String, T> dst,
check(BencodingUtils.DICTIONARY);

while (peek() != BencodingUtils.EOF) {
String key = new String(raw(), charset);
String key = readString();
T value = readObject(factory);
dst.put(key, value);
}
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/org/ardverk/coding/BencodingTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
package org.ardverk.coding;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class BencodingTest {

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
BencodingOutputStream bEncoder = new BencodingOutputStream(byteStream);

private BencodingInputStream decoder() {
return decoder(false);
}

private BencodingInputStream decoder(boolean decodeAsString) {
final byte[] bytes = byteStream.toByteArray();
System.out.println(new String(bytes));
return new BencodingInputStream(new ByteArrayInputStream(bytes), decodeAsString);
}

@Before
public void setup() {
byteStream = new ByteArrayOutputStream();
bEncoder = new BencodingOutputStream(byteStream);
}

@Test
public void testString() throws IOException {
bEncoder.writeString("test");
String result = decoder().readString();
assertEquals("test", result);
}

@Test
public void testInt() throws IOException {
bEncoder.writeInt(1);
int result = decoder().readInt();
assertEquals(1, result);
}

@Test
public void testCollectionsDecoding() throws IOException {
ArrayList<Object> list = new ArrayList<Object>();
list.add("testA");
list.add("testB");
bEncoder.writeCollection(list);

List<Object> result = decoder(true).readList();
assertEquals(list, result);
}

@Test
public void testMapDecoding() throws IOException {
Map<String, String> map = new TreeMap<String, String>();
map.put("keyA", "valueA");
map.put("keyB", "valueB");
bEncoder.writeMap(map);

Map<String, Object> result = decoder(true).readMap();
assertEquals(map, result);
}
}

0 comments on commit c9bb3c8

Please sign in to comment.