mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Minor changes to serialisation library (mostly renaming).
This commit is contained in:
@@ -16,10 +16,10 @@ import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class AckReader implements ObjectReader<Ack> {
|
||||
class AckReader implements StructReader<Ack> {
|
||||
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
@@ -27,7 +27,7 @@ class AckReader implements ObjectReader<Ack> {
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public Ack readObject(Reader r) throws IOException {
|
||||
public Ack readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
|
||||
@@ -10,10 +10,10 @@ import net.sf.briar.api.protocol.AuthorId;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.serial.DigestingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class AuthorReader implements ObjectReader<Author> {
|
||||
class AuthorReader implements StructReader<Author> {
|
||||
|
||||
private final MessageDigest messageDigest;
|
||||
private final AuthorFactory authorFactory;
|
||||
@@ -23,7 +23,7 @@ class AuthorReader implements ObjectReader<Author> {
|
||||
this.authorFactory = authorFactory;
|
||||
}
|
||||
|
||||
public Author readObject(Reader r) throws IOException {
|
||||
public Author readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
|
||||
// Read and digest the data
|
||||
|
||||
@@ -10,29 +10,29 @@ import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class BatchReader implements ObjectReader<UnverifiedBatch> {
|
||||
class BatchReader implements StructReader<UnverifiedBatch> {
|
||||
|
||||
private final ObjectReader<UnverifiedMessage> messageReader;
|
||||
private final StructReader<UnverifiedMessage> messageReader;
|
||||
private final UnverifiedBatchFactory batchFactory;
|
||||
|
||||
BatchReader(ObjectReader<UnverifiedMessage> messageReader,
|
||||
BatchReader(StructReader<UnverifiedMessage> messageReader,
|
||||
UnverifiedBatchFactory batchFactory) {
|
||||
this.messageReader = messageReader;
|
||||
this.batchFactory = batchFactory;
|
||||
}
|
||||
|
||||
public UnverifiedBatch readObject(Reader r) throws IOException {
|
||||
public UnverifiedBatch readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readStructId(Types.BATCH);
|
||||
r.addObjectReader(Types.MESSAGE, messageReader);
|
||||
r.addStructReader(Types.MESSAGE, messageReader);
|
||||
List<UnverifiedMessage> messages = r.readList(UnverifiedMessage.class);
|
||||
r.removeObjectReader(Types.MESSAGE);
|
||||
r.removeStructReader(Types.MESSAGE);
|
||||
r.removeConsumer(counting);
|
||||
if(messages.isEmpty()) throw new FormatException();
|
||||
// Build and return the batch
|
||||
|
||||
@@ -10,10 +10,10 @@ import net.sf.briar.api.protocol.GroupId;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.serial.DigestingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class GroupReader implements ObjectReader<Group> {
|
||||
class GroupReader implements StructReader<Group> {
|
||||
|
||||
private final MessageDigest messageDigest;
|
||||
private final GroupFactory groupFactory;
|
||||
@@ -23,7 +23,7 @@ class GroupReader implements ObjectReader<Group> {
|
||||
this.groupFactory = groupFactory;
|
||||
}
|
||||
|
||||
public Group readObject(Reader r) throws IOException {
|
||||
public Group readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
|
||||
// Read and digest the data
|
||||
|
||||
@@ -16,21 +16,21 @@ import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.CopyingConsumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class MessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
class MessageReader implements StructReader<UnverifiedMessage> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
private final ObjectReader<Author> authorReader;
|
||||
private final StructReader<Group> groupReader;
|
||||
private final StructReader<Author> authorReader;
|
||||
|
||||
MessageReader(ObjectReader<Group> groupReader,
|
||||
ObjectReader<Author> authorReader) {
|
||||
MessageReader(StructReader<Group> groupReader,
|
||||
StructReader<Author> authorReader) {
|
||||
this.groupReader = groupReader;
|
||||
this.authorReader = authorReader;
|
||||
}
|
||||
|
||||
public UnverifiedMessage readObject(Reader r) throws IOException {
|
||||
public UnverifiedMessage readStruct(Reader r) throws IOException {
|
||||
CopyingConsumer copying = new CopyingConsumer();
|
||||
CountingConsumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
r.addConsumer(copying);
|
||||
@@ -51,18 +51,18 @@ class MessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
if(r.hasNull()) {
|
||||
r.readNull();
|
||||
} else {
|
||||
r.addObjectReader(Types.GROUP, groupReader);
|
||||
r.addStructReader(Types.GROUP, groupReader);
|
||||
group = r.readStruct(Types.GROUP, Group.class);
|
||||
r.removeObjectReader(Types.GROUP);
|
||||
r.removeStructReader(Types.GROUP);
|
||||
}
|
||||
// Read the author, if there is one
|
||||
Author author = null;
|
||||
if(r.hasNull()) {
|
||||
r.readNull();
|
||||
} else {
|
||||
r.addObjectReader(Types.AUTHOR, authorReader);
|
||||
r.addStructReader(Types.AUTHOR, authorReader);
|
||||
author = r.readStruct(Types.AUTHOR, Author.class);
|
||||
r.removeObjectReader(Types.AUTHOR);
|
||||
r.removeStructReader(Types.AUTHOR);
|
||||
}
|
||||
// Read the subject
|
||||
String subject = r.readString(MAX_SUBJECT_LENGTH);
|
||||
|
||||
@@ -16,10 +16,10 @@ import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class OfferReader implements ObjectReader<Offer> {
|
||||
class OfferReader implements StructReader<Offer> {
|
||||
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
@@ -27,7 +27,7 @@ class OfferReader implements ObjectReader<Offer> {
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public Offer readObject(Reader r) throws IOException {
|
||||
public Offer readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
|
||||
@@ -18,7 +18,7 @@ import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.protocol.VerificationExecutor;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.util.BoundedExecutor;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
@@ -58,54 +58,54 @@ public class ProtocolModule extends AbstractModule {
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Ack> getAckReader(PacketFactory ackFactory) {
|
||||
StructReader<Ack> getAckReader(PacketFactory ackFactory) {
|
||||
return new AckReader(ackFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Author> getAuthorReader(CryptoComponent crypto,
|
||||
StructReader<Author> getAuthorReader(CryptoComponent crypto,
|
||||
AuthorFactory authorFactory) {
|
||||
return new AuthorReader(crypto, authorFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<UnverifiedBatch> getBatchReader(
|
||||
ObjectReader<UnverifiedMessage> messageReader,
|
||||
StructReader<UnverifiedBatch> getBatchReader(
|
||||
StructReader<UnverifiedMessage> messageReader,
|
||||
UnverifiedBatchFactory batchFactory) {
|
||||
return new BatchReader(messageReader, batchFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Group> getGroupReader(CryptoComponent crypto,
|
||||
StructReader<Group> getGroupReader(CryptoComponent crypto,
|
||||
GroupFactory groupFactory) {
|
||||
return new GroupReader(crypto, groupFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<UnverifiedMessage> getMessageReader(
|
||||
ObjectReader<Group> groupReader,
|
||||
ObjectReader<Author> authorReader) {
|
||||
StructReader<UnverifiedMessage> getMessageReader(
|
||||
StructReader<Group> groupReader,
|
||||
StructReader<Author> authorReader) {
|
||||
return new MessageReader(groupReader, authorReader);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Offer> getOfferReader(PacketFactory packetFactory) {
|
||||
StructReader<Offer> getOfferReader(PacketFactory packetFactory) {
|
||||
return new OfferReader(packetFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<Request> getRequestReader(PacketFactory packetFactory) {
|
||||
StructReader<Request> getRequestReader(PacketFactory packetFactory) {
|
||||
return new RequestReader(packetFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<SubscriptionUpdate> getSubscriptionReader(
|
||||
ObjectReader<Group> groupReader, PacketFactory packetFactory) {
|
||||
StructReader<SubscriptionUpdate> getSubscriptionReader(
|
||||
StructReader<Group> groupReader, PacketFactory packetFactory) {
|
||||
return new SubscriptionUpdateReader(groupReader, packetFactory);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ObjectReader<TransportUpdate> getTransportReader(
|
||||
StructReader<TransportUpdate> getTransportReader(
|
||||
PacketFactory packetFactory) {
|
||||
return new TransportUpdateReader(packetFactory);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
@@ -19,21 +19,21 @@ import com.google.inject.Provider;
|
||||
class ProtocolReaderFactoryImpl implements ProtocolReaderFactory {
|
||||
|
||||
private final ReaderFactory readerFactory;
|
||||
private final Provider<ObjectReader<Ack>> ackProvider;
|
||||
private final Provider<ObjectReader<UnverifiedBatch>> batchProvider;
|
||||
private final Provider<ObjectReader<Offer>> offerProvider;
|
||||
private final Provider<ObjectReader<Request>> requestProvider;
|
||||
private final Provider<ObjectReader<SubscriptionUpdate>> subscriptionProvider;
|
||||
private final Provider<ObjectReader<TransportUpdate>> transportProvider;
|
||||
private final Provider<StructReader<Ack>> ackProvider;
|
||||
private final Provider<StructReader<UnverifiedBatch>> batchProvider;
|
||||
private final Provider<StructReader<Offer>> offerProvider;
|
||||
private final Provider<StructReader<Request>> requestProvider;
|
||||
private final Provider<StructReader<SubscriptionUpdate>> subscriptionProvider;
|
||||
private final Provider<StructReader<TransportUpdate>> transportProvider;
|
||||
|
||||
@Inject
|
||||
ProtocolReaderFactoryImpl(ReaderFactory readerFactory,
|
||||
Provider<ObjectReader<Ack>> ackProvider,
|
||||
Provider<ObjectReader<UnverifiedBatch>> batchProvider,
|
||||
Provider<ObjectReader<Offer>> offerProvider,
|
||||
Provider<ObjectReader<Request>> requestProvider,
|
||||
Provider<ObjectReader<SubscriptionUpdate>> subscriptionProvider,
|
||||
Provider<ObjectReader<TransportUpdate>> transportProvider) {
|
||||
Provider<StructReader<Ack>> ackProvider,
|
||||
Provider<StructReader<UnverifiedBatch>> batchProvider,
|
||||
Provider<StructReader<Offer>> offerProvider,
|
||||
Provider<StructReader<Request>> requestProvider,
|
||||
Provider<StructReader<SubscriptionUpdate>> subscriptionProvider,
|
||||
Provider<StructReader<TransportUpdate>> transportProvider) {
|
||||
this.readerFactory = readerFactory;
|
||||
this.ackProvider = ackProvider;
|
||||
this.batchProvider = batchProvider;
|
||||
|
||||
@@ -11,7 +11,7 @@ import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.TransportUpdate;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UnverifiedBatch;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
import net.sf.briar.api.serial.ReaderFactory;
|
||||
|
||||
@@ -20,19 +20,19 @@ class ProtocolReaderImpl implements ProtocolReader {
|
||||
private final Reader reader;
|
||||
|
||||
ProtocolReaderImpl(InputStream in, ReaderFactory readerFactory,
|
||||
ObjectReader<Ack> ackReader,
|
||||
ObjectReader<UnverifiedBatch> batchReader,
|
||||
ObjectReader<Offer> offerReader,
|
||||
ObjectReader<Request> requestReader,
|
||||
ObjectReader<SubscriptionUpdate> subscriptionReader,
|
||||
ObjectReader<TransportUpdate> transportReader) {
|
||||
StructReader<Ack> ackReader,
|
||||
StructReader<UnverifiedBatch> batchReader,
|
||||
StructReader<Offer> offerReader,
|
||||
StructReader<Request> requestReader,
|
||||
StructReader<SubscriptionUpdate> subscriptionReader,
|
||||
StructReader<TransportUpdate> transportReader) {
|
||||
reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
reader.addObjectReader(Types.OFFER, offerReader);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
reader.addObjectReader(Types.SUBSCRIPTION_UPDATE, subscriptionReader);
|
||||
reader.addObjectReader(Types.TRANSPORT_UPDATE, transportReader);
|
||||
reader.addStructReader(Types.ACK, ackReader);
|
||||
reader.addStructReader(Types.BATCH, batchReader);
|
||||
reader.addStructReader(Types.OFFER, offerReader);
|
||||
reader.addStructReader(Types.REQUEST, requestReader);
|
||||
reader.addStructReader(Types.SUBSCRIPTION_UPDATE, subscriptionReader);
|
||||
reader.addStructReader(Types.TRANSPORT_UPDATE, transportReader);
|
||||
}
|
||||
|
||||
public boolean eof() throws IOException {
|
||||
|
||||
@@ -11,10 +11,10 @@ import net.sf.briar.api.protocol.Request;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class RequestReader implements ObjectReader<Request> {
|
||||
class RequestReader implements StructReader<Request> {
|
||||
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
@@ -22,7 +22,7 @@ class RequestReader implements ObjectReader<Request> {
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public Request readObject(Reader r) throws IOException {
|
||||
public Request readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
|
||||
@@ -12,29 +12,29 @@ import net.sf.briar.api.protocol.SubscriptionUpdate;
|
||||
import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class SubscriptionUpdateReader implements ObjectReader<SubscriptionUpdate> {
|
||||
class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
|
||||
|
||||
private final ObjectReader<Group> groupReader;
|
||||
private final StructReader<Group> groupReader;
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
SubscriptionUpdateReader(ObjectReader<Group> groupReader,
|
||||
SubscriptionUpdateReader(StructReader<Group> groupReader,
|
||||
PacketFactory packetFactory) {
|
||||
this.groupReader = groupReader;
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public SubscriptionUpdate readObject(Reader r) throws IOException {
|
||||
public SubscriptionUpdate readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readStructId(Types.SUBSCRIPTION_UPDATE);
|
||||
r.addObjectReader(Types.GROUP, groupReader);
|
||||
r.addStructReader(Types.GROUP, groupReader);
|
||||
Map<Group, Long> subs = r.readMap(Group.class, Long.class);
|
||||
r.removeObjectReader(Types.GROUP);
|
||||
r.removeStructReader(Types.GROUP);
|
||||
long timestamp = r.readInt64();
|
||||
if(timestamp < 0L) throw new FormatException();
|
||||
r.removeConsumer(counting);
|
||||
|
||||
@@ -21,28 +21,28 @@ import net.sf.briar.api.protocol.Types;
|
||||
import net.sf.briar.api.protocol.UniqueId;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.CountingConsumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
class TransportUpdateReader implements ObjectReader<TransportUpdate> {
|
||||
class TransportUpdateReader implements StructReader<TransportUpdate> {
|
||||
|
||||
private final PacketFactory packetFactory;
|
||||
private final ObjectReader<Transport> transportReader;
|
||||
private final StructReader<Transport> transportReader;
|
||||
|
||||
TransportUpdateReader(PacketFactory packetFactory) {
|
||||
this.packetFactory = packetFactory;
|
||||
transportReader = new TransportReader();
|
||||
}
|
||||
|
||||
public TransportUpdate readObject(Reader r) throws IOException {
|
||||
public TransportUpdate readStruct(Reader r) throws IOException {
|
||||
// Initialise the consumer
|
||||
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readStructId(Types.TRANSPORT_UPDATE);
|
||||
r.addObjectReader(Types.TRANSPORT, transportReader);
|
||||
r.addStructReader(Types.TRANSPORT, transportReader);
|
||||
Collection<Transport> transports = r.readList(Transport.class);
|
||||
r.removeObjectReader(Types.TRANSPORT);
|
||||
r.removeStructReader(Types.TRANSPORT);
|
||||
if(transports.size() > MAX_TRANSPORTS) throw new FormatException();
|
||||
long timestamp = r.readInt64();
|
||||
r.removeConsumer(counting);
|
||||
@@ -57,9 +57,9 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
|
||||
return packetFactory.createTransportUpdate(transports, timestamp);
|
||||
}
|
||||
|
||||
private static class TransportReader implements ObjectReader<Transport> {
|
||||
private static class TransportReader implements StructReader<Transport> {
|
||||
|
||||
public Transport readObject(Reader r) throws IOException {
|
||||
public Transport readStruct(Reader r) throws IOException {
|
||||
r.readStructId(Types.TRANSPORT);
|
||||
// Read the ID
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.Map;
|
||||
import net.sf.briar.api.Bytes;
|
||||
import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.serial.Consumer;
|
||||
import net.sf.briar.api.serial.ObjectReader;
|
||||
import net.sf.briar.api.serial.StructReader;
|
||||
import net.sf.briar.api.serial.Reader;
|
||||
|
||||
// This class is not thread-safe
|
||||
@@ -23,7 +23,7 @@ class ReaderImpl implements Reader {
|
||||
private final InputStream in;
|
||||
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
|
||||
|
||||
private ObjectReader<?>[] objectReaders = new ObjectReader<?>[] {};
|
||||
private StructReader<?>[] structReaders = new StructReader<?>[] {};
|
||||
private boolean hasLookahead = false, eof = false;
|
||||
private byte next, nextNext;
|
||||
private byte[] buf = null;
|
||||
@@ -98,21 +98,22 @@ class ReaderImpl implements Reader {
|
||||
if(!consumers.remove(c)) throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void addObjectReader(int id, ObjectReader<?> o) {
|
||||
public void addStructReader(int id, StructReader<?> o) {
|
||||
if(id < 0 || id > 255) throw new IllegalArgumentException();
|
||||
if(objectReaders.length < id + 1) {
|
||||
ObjectReader<?>[] newObjectReaders = new ObjectReader<?>[id + 1];
|
||||
System.arraycopy(objectReaders, 0, newObjectReaders, 0,
|
||||
objectReaders.length);
|
||||
objectReaders = newObjectReaders;
|
||||
if(structReaders.length < id + 1) {
|
||||
int len = Math.min(256, Math.max(id + 1, structReaders.length * 2));
|
||||
StructReader<?>[] newStructReaders = new StructReader<?>[len];
|
||||
System.arraycopy(structReaders, 0, newStructReaders, 0,
|
||||
structReaders.length);
|
||||
structReaders = newStructReaders;
|
||||
}
|
||||
objectReaders[id] = o;
|
||||
structReaders[id] = o;
|
||||
}
|
||||
|
||||
public void removeObjectReader(int id) {
|
||||
if(id < 0 || id > objectReaders.length)
|
||||
public void removeStructReader(int id) {
|
||||
if(id < 0 || id > structReaders.length)
|
||||
throw new IllegalArgumentException();
|
||||
objectReaders[id] = null;
|
||||
structReaders[id] = null;
|
||||
}
|
||||
|
||||
public boolean hasBoolean() throws IOException {
|
||||
@@ -335,10 +336,6 @@ class ReaderImpl implements Reader {
|
||||
|| (next & Tag.SHORT_MASK) == Tag.SHORT_LIST;
|
||||
}
|
||||
|
||||
public List<Object> readList() throws IOException {
|
||||
return readList(Object.class);
|
||||
}
|
||||
|
||||
public <E> List<E> readList(Class<E> e) throws IOException {
|
||||
if(!hasList()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
@@ -385,8 +382,8 @@ class ReaderImpl implements Reader {
|
||||
if(hasFloat64()) return Double.valueOf(readFloat64());
|
||||
if(hasString()) return readString();
|
||||
if(hasBytes()) return new Bytes(readBytes());
|
||||
if(hasList()) return readList();
|
||||
if(hasMap()) return readMap();
|
||||
if(hasList()) return readList(Object.class);
|
||||
if(hasMap()) return readMap(Object.class, Object.class);
|
||||
if(hasNull()) {
|
||||
readNull();
|
||||
return null;
|
||||
@@ -462,10 +459,6 @@ class ReaderImpl implements Reader {
|
||||
|| (next & Tag.SHORT_MASK) == Tag.SHORT_MAP;
|
||||
}
|
||||
|
||||
public Map<Object, Object> readMap() throws IOException {
|
||||
return readMap(Object.class, Object.class);
|
||||
}
|
||||
|
||||
public <K, V> Map<K, V> readMap(Class<K> k, Class<V> v) throws IOException {
|
||||
if(!hasMap()) throw new FormatException();
|
||||
consumeLookahead();
|
||||
@@ -538,11 +531,11 @@ class ReaderImpl implements Reader {
|
||||
|
||||
public <T> T readStruct(int id, Class<T> t) throws IOException {
|
||||
if(!hasStruct(id)) throw new FormatException();
|
||||
if(id >= objectReaders.length) throw new FormatException();
|
||||
ObjectReader<?> o = objectReaders[id];
|
||||
if(o == null) throw new FormatException();
|
||||
if(id < 0 || id >= structReaders.length) throw new FormatException();
|
||||
StructReader<?> s = structReaders[id];
|
||||
if(s == null) throw new FormatException();
|
||||
try {
|
||||
return t.cast(o.readObject(this));
|
||||
return t.cast(s.readStruct(this));
|
||||
} catch(ClassCastException e) {
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user