Skip to content
Snippets Groups Projects
Unverified Commit 4d8a84a4 authored by akwizgran's avatar akwizgran
Browse files

Don't throw IllegalStateException if BDF input is incomplete.

parent 2650f311
No related branches found
No related tags found
1 merge request!378Don't throw IllegalStateException if BDF input is incomplete
......@@ -4,6 +4,8 @@ import java.io.IOException;
public interface BdfReader {
int DEFAULT_NESTED_LIMIT = 5;
boolean eof() throws IOException;
void close() throws IOException;
......
......@@ -5,4 +5,6 @@ import java.io.InputStream;
public interface BdfReaderFactory {
BdfReader createReader(InputStream in);
BdfReader createReader(InputStream in, int nestedLimit);
}
......@@ -5,9 +5,17 @@ import org.briarproject.api.data.BdfReaderFactory;
import java.io.InputStream;
import static org.briarproject.api.data.BdfReader.DEFAULT_NESTED_LIMIT;
class BdfReaderFactoryImpl implements BdfReaderFactory {
@Override
public BdfReader createReader(InputStream in) {
return new BdfReaderImpl(in);
return new BdfReaderImpl(in, DEFAULT_NESTED_LIMIT);
}
@Override
public BdfReader createReader(InputStream in, int nestedLimit) {
return new BdfReaderImpl(in, nestedLimit);
}
}
......@@ -32,21 +32,14 @@ import static org.briarproject.data.Types.TRUE;
@NotThreadSafe
class BdfReaderImpl implements BdfReader {
final static int DEFAULT_NESTED_LIMIT = 5;
private static final byte[] EMPTY_BUFFER = new byte[] {};
private static final byte[] EMPTY_BUFFER = new byte[0];
private final InputStream in;
private final int nestedLimit;
private boolean hasLookahead = false, eof = false;
private byte next;
private byte[] buf = new byte[8];
private final int nestedLimit;
BdfReaderImpl(InputStream in) {
this.in = in;
this.nestedLimit = DEFAULT_NESTED_LIMIT;
}
BdfReaderImpl(InputStream in, int nestedLimit) {
this.in = in;
......@@ -54,7 +47,7 @@ class BdfReaderImpl implements BdfReader {
}
private void readLookahead() throws IOException {
if (eof) throw new IllegalStateException();
if (eof) return;
if (hasLookahead) throw new IllegalStateException();
// Read a lookahead byte
int i = in.read();
......
......@@ -268,7 +268,7 @@ public class BdfReaderImplTest extends BriarTestCase {
String unicode = "\uFDD0\uFDD1\uFDD2\uFDD3";
String hex = StringUtils.toHexString(unicode.getBytes("UTF-8"));
// STRING_8 tag, "foo", the empty string, and the test string
setContents("41" + "03" + "666F6F" +"41" + "00" + "41" + "0C" + hex);
setContents("41" + "03" + "666F6F" + "41" + "00" + "41" + "0C" + hex);
assertEquals("foo", r.readString(Integer.MAX_VALUE));
assertEquals("", r.readString(Integer.MAX_VALUE));
assertEquals(unicode, r.readString(Integer.MAX_VALUE));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment