mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 04:18:53 +01:00
Separate the sync layer from its clients. #112
This commit is contained in:
@@ -2,12 +2,9 @@ package org.briarproject.data;
|
||||
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.data.BdfReader;
|
||||
import org.briarproject.api.data.Consumer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.briarproject.data.Types.DICTIONARY;
|
||||
import static org.briarproject.data.Types.END;
|
||||
@@ -33,7 +30,6 @@ class BdfReaderImpl implements BdfReader {
|
||||
private static final byte[] EMPTY_BUFFER = new byte[] {};
|
||||
|
||||
private final InputStream in;
|
||||
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
|
||||
|
||||
private boolean hasLookahead = false, eof = false;
|
||||
private byte next;
|
||||
@@ -44,8 +40,8 @@ class BdfReaderImpl implements BdfReader {
|
||||
}
|
||||
|
||||
private void readLookahead() throws IOException {
|
||||
assert !eof;
|
||||
assert !hasLookahead;
|
||||
if (eof) throw new IllegalStateException();
|
||||
if (hasLookahead) throw new IllegalStateException();
|
||||
// Read a lookahead byte
|
||||
int i = in.read();
|
||||
if (i == -1) {
|
||||
@@ -56,27 +52,18 @@ class BdfReaderImpl implements BdfReader {
|
||||
hasLookahead = true;
|
||||
}
|
||||
|
||||
private void consumeLookahead() throws IOException {
|
||||
assert hasLookahead;
|
||||
for (Consumer c : consumers) c.write(next);
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
private void readIntoBuffer(byte[] b, int length, boolean consume)
|
||||
throws IOException {
|
||||
private void readIntoBuffer(byte[] b, int length) throws IOException {
|
||||
int offset = 0;
|
||||
while (offset < length) {
|
||||
int read = in.read(b, offset, length - offset);
|
||||
if (read == -1) throw new FormatException();
|
||||
offset += read;
|
||||
}
|
||||
if (consume) for (Consumer c : consumers) c.write(b, 0, length);
|
||||
}
|
||||
|
||||
private void readIntoBuffer(int length, boolean consume)
|
||||
throws IOException {
|
||||
private void readIntoBuffer(int length) throws IOException {
|
||||
if (buf.length < length) buf = new byte[length];
|
||||
readIntoBuffer(buf, length, consume);
|
||||
readIntoBuffer(buf, length);
|
||||
}
|
||||
|
||||
private void skip(int length) throws IOException {
|
||||
@@ -108,14 +95,6 @@ class BdfReaderImpl implements BdfReader {
|
||||
in.close();
|
||||
}
|
||||
|
||||
public void addConsumer(Consumer c) {
|
||||
consumers.add(c);
|
||||
}
|
||||
|
||||
public void removeConsumer(Consumer c) {
|
||||
if (!consumers.remove(c)) throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public boolean hasNull() throws IOException {
|
||||
if (!hasLookahead) readLookahead();
|
||||
if (eof) return false;
|
||||
@@ -124,7 +103,7 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public void readNull() throws IOException {
|
||||
if (!hasNull()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
public void skipNull() throws IOException {
|
||||
@@ -141,7 +120,7 @@ class BdfReaderImpl implements BdfReader {
|
||||
public boolean readBoolean() throws IOException {
|
||||
if (!hasBoolean()) throw new FormatException();
|
||||
boolean bool = next == TRUE;
|
||||
consumeLookahead();
|
||||
hasLookahead = false;
|
||||
return bool;
|
||||
}
|
||||
|
||||
@@ -159,32 +138,32 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public long readInteger() throws IOException {
|
||||
if (!hasInteger()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
if (next == INT_8) return readInt8(true);
|
||||
if (next == INT_16) return readInt16(true);
|
||||
if (next == INT_32) return readInt32(true);
|
||||
return readInt64(true);
|
||||
hasLookahead = false;
|
||||
if (next == INT_8) return readInt8();
|
||||
if (next == INT_16) return readInt16();
|
||||
if (next == INT_32) return readInt32();
|
||||
return readInt64();
|
||||
}
|
||||
|
||||
private int readInt8(boolean consume) throws IOException {
|
||||
readIntoBuffer(1, consume);
|
||||
private int readInt8() throws IOException {
|
||||
readIntoBuffer(1);
|
||||
return buf[0];
|
||||
}
|
||||
|
||||
private short readInt16(boolean consume) throws IOException {
|
||||
readIntoBuffer(2, consume);
|
||||
private short readInt16() throws IOException {
|
||||
readIntoBuffer(2);
|
||||
return (short) (((buf[0] & 0xFF) << 8) + (buf[1] & 0xFF));
|
||||
}
|
||||
|
||||
private int readInt32(boolean consume) throws IOException {
|
||||
readIntoBuffer(4, consume);
|
||||
private int readInt32() throws IOException {
|
||||
readIntoBuffer(4);
|
||||
int value = 0;
|
||||
for (int i = 0; i < 4; i++) value |= (buf[i] & 0xFF) << (24 - i * 8);
|
||||
return value;
|
||||
}
|
||||
|
||||
private long readInt64(boolean consume) throws IOException {
|
||||
readIntoBuffer(8, consume);
|
||||
private long readInt64() throws IOException {
|
||||
readIntoBuffer(8);
|
||||
long value = 0;
|
||||
for (int i = 0; i < 8; i++) value |= (buf[i] & 0xFFL) << (56 - i * 8);
|
||||
return value;
|
||||
@@ -207,8 +186,8 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public double readFloat() throws IOException {
|
||||
if (!hasFloat()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
readIntoBuffer(8, true);
|
||||
hasLookahead = false;
|
||||
readIntoBuffer(8);
|
||||
long value = 0;
|
||||
for (int i = 0; i < 8; i++) value |= (buf[i] & 0xFFL) << (56 - i * 8);
|
||||
return Double.longBitsToDouble(value);
|
||||
@@ -228,24 +207,24 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public String readString(int maxLength) throws IOException {
|
||||
if (!hasString()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
int length = readStringLength(true);
|
||||
hasLookahead = false;
|
||||
int length = readStringLength();
|
||||
if (length < 0 || length > maxLength) throw new FormatException();
|
||||
if (length == 0) return "";
|
||||
readIntoBuffer(length, true);
|
||||
readIntoBuffer(length);
|
||||
return new String(buf, 0, length, "UTF-8");
|
||||
}
|
||||
|
||||
private int readStringLength(boolean consume) throws IOException {
|
||||
if (next == STRING_8) return readInt8(consume);
|
||||
if (next == STRING_16) return readInt16(consume);
|
||||
if (next == STRING_32) return readInt32(consume);
|
||||
private int readStringLength() throws IOException {
|
||||
if (next == STRING_8) return readInt8();
|
||||
if (next == STRING_16) return readInt16();
|
||||
if (next == STRING_32) return readInt32();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public void skipString() throws IOException {
|
||||
if (!hasString()) throw new FormatException();
|
||||
int length = readStringLength(false);
|
||||
int length = readStringLength();
|
||||
if (length < 0) throw new FormatException();
|
||||
skip(length);
|
||||
hasLookahead = false;
|
||||
@@ -259,25 +238,25 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public byte[] readRaw(int maxLength) throws IOException {
|
||||
if (!hasRaw()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
int length = readRawLength(true);
|
||||
hasLookahead = false;
|
||||
int length = readRawLength();
|
||||
if (length < 0 || length > maxLength) throw new FormatException();
|
||||
if (length == 0) return EMPTY_BUFFER;
|
||||
byte[] b = new byte[length];
|
||||
readIntoBuffer(b, length, true);
|
||||
readIntoBuffer(b, length);
|
||||
return b;
|
||||
}
|
||||
|
||||
private int readRawLength(boolean consume) throws IOException {
|
||||
if (next == RAW_8) return readInt8(consume);
|
||||
if (next == RAW_16) return readInt16(consume);
|
||||
if (next == RAW_32) return readInt32(consume);
|
||||
private int readRawLength() throws IOException {
|
||||
if (next == RAW_8) return readInt8();
|
||||
if (next == RAW_16) return readInt16();
|
||||
if (next == RAW_32) return readInt32();
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
public void skipRaw() throws IOException {
|
||||
if (!hasRaw()) throw new FormatException();
|
||||
int length = readRawLength(false);
|
||||
int length = readRawLength();
|
||||
if (length < 0) throw new FormatException();
|
||||
skip(length);
|
||||
hasLookahead = false;
|
||||
@@ -291,7 +270,7 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public void readListStart() throws IOException {
|
||||
if (!hasList()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
public boolean hasListEnd() throws IOException {
|
||||
@@ -310,7 +289,7 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
private void readEnd() throws IOException {
|
||||
if (!hasEnd()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
public void skipList() throws IOException {
|
||||
@@ -328,7 +307,7 @@ class BdfReaderImpl implements BdfReader {
|
||||
|
||||
public void readDictionaryStart() throws IOException {
|
||||
if (!hasDictionary()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
hasLookahead = false;
|
||||
}
|
||||
|
||||
public boolean hasDictionaryEnd() throws IOException {
|
||||
|
||||
@@ -33,13 +33,13 @@ class MetadataParserImpl implements MetadataParser {
|
||||
|
||||
@Override
|
||||
public BdfDictionary parse(Metadata m) throws FormatException {
|
||||
BdfDictionary dict = new BdfDictionary();
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
for (Entry<String, byte[]> e : m.entrySet())
|
||||
dict.put(e.getKey(), parseObject(e.getValue()));
|
||||
return dict;
|
||||
d.put(e.getKey(), parseValue(e.getValue()));
|
||||
return d;
|
||||
}
|
||||
|
||||
private Object parseObject(byte[] b) throws FormatException {
|
||||
private Object parseValue(byte[] b) throws FormatException {
|
||||
if (b == REMOVE) return null;
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Object o = parseObject(in);
|
||||
|
||||
Reference in New Issue
Block a user