Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputStream does not need support mark/reset #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 33 additions & 46 deletions src/main/java/org/bitlet/wetorrent/bencode/Bencode.java
Expand Up @@ -40,10 +40,7 @@ public class Bencode {
* This creates and parse a bencoded InputStream
*/
public Bencode(InputStream is) throws IOException {
if (!is.markSupported()) {
throw new IOException("is.markSupported should be true");
}
rootElement = parse(is);
rootElement = parse(readHead(is), is);
}

/**
Expand Down Expand Up @@ -100,16 +97,18 @@ private void print(Object object, OutputStream os) throws IOException {
}
}

private Object parse(InputStream is) throws IOException {
is.mark(0);
int readChar = is.read();
switch (readChar) {
private int readHead(InputStream is) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid this method and just call is.read in every place instead of readHead(is).

return is.read();
}

private Object parse(int head, InputStream tail) throws IOException {
switch (head) {
case 'i':
return parseInteger(is);
return parseInteger(readHead(tail), tail);
case 'l':
return parseList(is);
return parseList(readHead(tail), tail);
case 'd':
return parseDictionary(is);
return parseDictionary(readHead(tail), tail);
case '0':
case '1':
case '2':
Expand All @@ -120,8 +119,7 @@ private Object parse(InputStream is) throws IOException {
case '7':
case '8':
case '9':
is.reset();
return parseByteString(is);
return parseByteString(head, tail);
default:
throw new IOException("Problem parsing bencoded file");
}
Expand All @@ -135,75 +133,64 @@ public void setRootElement(Object rootElement) {
this.rootElement = rootElement;
}

private Long parseInteger(InputStream is) throws IOException {

int readChar = is.read();
private Long parseInteger(int head, InputStream tail) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need the head parameter?


StringBuffer buff = new StringBuffer();
do {
if (readChar < 0) {
if (head < 0) {
throw new IOException("Unexpected EOF found");
}
buff.append((char) readChar);
readChar = is.read();
} while (readChar != 'e');
buff.append((char) head);
head = readHead(tail);
} while (head != 'e');

// System.out.println("Loaded int: " + buff);
return Long.parseLong(buff.toString());
}

private List<Object> parseList(InputStream is) throws IOException {
private List<Object> parseList(int head, InputStream tail) throws IOException {

List<Object> list = new LinkedList<Object>();
is.mark(0);
int readChar = is.read();
while (readChar != 'e') {
if (readChar < 0) {
while (head != 'e') {
if (head < 0) {
throw new IOException("Unexpected EOF found");
}
is.reset();
list.add(parse(is));
is.mark(0);
readChar = is.read();
list.add(parse(head, tail));
head = readHead(tail);
}

return list;
}

private SortedMap parseDictionary(InputStream is) throws IOException {
private SortedMap parseDictionary(int head, InputStream tail) throws IOException {
SortedMap<ByteBuffer, Object> map = new TreeMap<ByteBuffer, Object>(new DictionaryComparator());
is.mark(0);
int readChar = is.read();
while (readChar != 'e') {
if (readChar < 0) {
while (head != 'e') {
if (head < 0) {
throw new IOException("Unexpected EOF found");
}
is.reset();
map.put(parseByteString(is), parse(is));
is.mark(0);
readChar = is.read();
ByteBuffer key = parseByteString(head, tail);
map.put(key, parse(readHead(tail), tail));
head = readHead(tail);
}

return map;
}

private ByteBuffer parseByteString(InputStream is) throws IOException {

int readChar = is.read();
private ByteBuffer parseByteString(int head, InputStream tail) throws IOException {

StringBuffer buff = new StringBuffer();
do {
if (readChar < 0) {
if (head < 0) {
throw new IOException("Unexpected EOF found");
}
buff.append((char) readChar);
readChar = is.read();
} while (readChar != ':');
buff.append((char) head);
head = readHead(tail);
} while (head != ':');
Integer length = Integer.parseInt(buff.toString());

byte[] byteString = new byte[length];
for (int i = 0; i < byteString.length; i++) {
byteString[i] = (byte) is.read();
byteString[i] = (byte) readHead(tail);
// System.out.println("Loaded string: " + new String(byteString));
}
return ByteBuffer.wrap(byteString);
Expand Down