Don't throw IllegalStateException if BDF input is incomplete.

This commit is contained in:
akwizgran
2016-11-01 12:16:03 +00:00
parent 2650f3114e
commit 4d8a84a48d
5 changed files with 17 additions and 12 deletions

View File

@@ -4,6 +4,8 @@ import java.io.IOException;
public interface BdfReader {
int DEFAULT_NESTED_LIMIT = 5;
boolean eof() throws IOException;
void close() throws IOException;

View File

@@ -5,4 +5,6 @@ import java.io.InputStream;
public interface BdfReaderFactory {
BdfReader createReader(InputStream in);
BdfReader createReader(InputStream in, int nestedLimit);
}

View File

@@ -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);
}
}

View File

@@ -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();

View File

@@ -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));