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
saper
wants to merge
1
commit into
bitletorg:master
Choose a base branch
from
saper:nomark
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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 { | ||
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': | ||
|
@@ -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"); | ||
} | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).