mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Don't throw IllegalStateException if BDF input is incomplete.
This commit is contained in:
@@ -4,6 +4,8 @@ import java.io.IOException;
|
|||||||
|
|
||||||
public interface BdfReader {
|
public interface BdfReader {
|
||||||
|
|
||||||
|
int DEFAULT_NESTED_LIMIT = 5;
|
||||||
|
|
||||||
boolean eof() throws IOException;
|
boolean eof() throws IOException;
|
||||||
void close() throws IOException;
|
void close() throws IOException;
|
||||||
|
|
||||||
|
|||||||
@@ -5,4 +5,6 @@ import java.io.InputStream;
|
|||||||
public interface BdfReaderFactory {
|
public interface BdfReaderFactory {
|
||||||
|
|
||||||
BdfReader createReader(InputStream in);
|
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 java.io.InputStream;
|
||||||
|
|
||||||
|
import static org.briarproject.api.data.BdfReader.DEFAULT_NESTED_LIMIT;
|
||||||
|
|
||||||
class BdfReaderFactoryImpl implements BdfReaderFactory {
|
class BdfReaderFactoryImpl implements BdfReaderFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
public BdfReader createReader(InputStream in) {
|
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
|
@NotThreadSafe
|
||||||
class BdfReaderImpl implements BdfReader {
|
class BdfReaderImpl implements BdfReader {
|
||||||
|
|
||||||
final static int DEFAULT_NESTED_LIMIT = 5;
|
private static final byte[] EMPTY_BUFFER = new byte[0];
|
||||||
|
|
||||||
private static final byte[] EMPTY_BUFFER = new byte[] {};
|
|
||||||
|
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
|
private final int nestedLimit;
|
||||||
|
|
||||||
private boolean hasLookahead = false, eof = false;
|
private boolean hasLookahead = false, eof = false;
|
||||||
private byte next;
|
private byte next;
|
||||||
private byte[] buf = new byte[8];
|
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) {
|
BdfReaderImpl(InputStream in, int nestedLimit) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
@@ -54,7 +47,7 @@ class BdfReaderImpl implements BdfReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void readLookahead() throws IOException {
|
private void readLookahead() throws IOException {
|
||||||
if (eof) throw new IllegalStateException();
|
if (eof) return;
|
||||||
if (hasLookahead) throw new IllegalStateException();
|
if (hasLookahead) throw new IllegalStateException();
|
||||||
// Read a lookahead byte
|
// Read a lookahead byte
|
||||||
int i = in.read();
|
int i = in.read();
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ public class BdfReaderImplTest extends BriarTestCase {
|
|||||||
String unicode = "\uFDD0\uFDD1\uFDD2\uFDD3";
|
String unicode = "\uFDD0\uFDD1\uFDD2\uFDD3";
|
||||||
String hex = StringUtils.toHexString(unicode.getBytes("UTF-8"));
|
String hex = StringUtils.toHexString(unicode.getBytes("UTF-8"));
|
||||||
// STRING_8 tag, "foo", the empty string, and the test string
|
// 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("foo", r.readString(Integer.MAX_VALUE));
|
||||||
assertEquals("", r.readString(Integer.MAX_VALUE));
|
assertEquals("", r.readString(Integer.MAX_VALUE));
|
||||||
assertEquals(unicode, r.readString(Integer.MAX_VALUE));
|
assertEquals(unicode, r.readString(Integer.MAX_VALUE));
|
||||||
|
|||||||
Reference in New Issue
Block a user