![JAR search and dependency download from the Maven repository](/logo.png)
bt.bencoding.Scanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-bencoding Show documentation
Show all versions of bt-bencoding Show documentation
Library for parsing, encoding and validating bencoded documents in Java
package bt.bencoding;
import bt.bencoding.model.BEObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
class Scanner {
private final MemoizingPushbackInputStream source;
Scanner(byte[] bs) {
this(new ByteArrayInputStream(bs));
}
Scanner(InputStream in) {
source = new MemoizingPushbackInputStream(in);
}
T readObject(BEObjectBuilder builder) throws Exception {
source.resetContents();
int c;
while ((c = source.read()) != -1 && builder.accept(c))
;
return builder.build();
}
int peek() {
return source.peek();
}
byte[] getScannedContents() {
return source.getContents();
}
void close() {
try {
source.close();
} catch (IOException e) {
// do nothing
}
}
private static class MemoizingPushbackInputStream extends InputStream {
private PushbackInputStream delegate;
private ByteArrayOutputStream buf;
MemoizingPushbackInputStream(InputStream delegate) {
this.delegate = new PushbackInputStream(delegate);
buf = new ByteArrayOutputStream();
}
@Override
public int read() throws IOException {
int c = delegate.read();
buf.write(c);
return c;
}
public int peek() {
try {
int c = delegate.read();
delegate.unread(c);
return c;
} catch (IOException e) {
throw new RuntimeException("Unexpected I/O exception", e);
}
}
public void resetContents() {
buf = new ByteArrayOutputStream();
}
public byte[] getContents() {
return buf.toByteArray();
}
@Override
public void close() throws IOException {
delegate.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy