mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Minor changes to serialisation library (mostly renaming).
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
package net.sf.briar.api.serial;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ObjectReader<T> {
|
||||
|
||||
T readObject(Reader r) throws IOException;
|
||||
}
|
||||
@@ -18,8 +18,8 @@ public interface Reader {
|
||||
void addConsumer(Consumer c);
|
||||
void removeConsumer(Consumer c);
|
||||
|
||||
void addObjectReader(int id, ObjectReader<?> o);
|
||||
void removeObjectReader(int id);
|
||||
void addStructReader(int id, StructReader<?> o);
|
||||
void removeStructReader(int id);
|
||||
|
||||
boolean hasBoolean() throws IOException;
|
||||
boolean readBoolean() throws IOException;
|
||||
@@ -51,7 +51,6 @@ public interface Reader {
|
||||
byte[] readBytes(int maxLength) throws IOException;
|
||||
|
||||
boolean hasList() throws IOException;
|
||||
List<Object> readList() throws IOException;
|
||||
<E> List<E> readList(Class<E> e) throws IOException;
|
||||
boolean hasListStart() throws IOException;
|
||||
void readListStart() throws IOException;
|
||||
@@ -59,7 +58,6 @@ public interface Reader {
|
||||
void readListEnd() throws IOException;
|
||||
|
||||
boolean hasMap() throws IOException;
|
||||
Map<Object, Object> readMap() throws IOException;
|
||||
<K, V> Map<K, V> readMap(Class<K> k, Class<V> v) throws IOException;
|
||||
boolean hasMapStart() throws IOException;
|
||||
void readMapStart() throws IOException;
|
||||
|
||||
8
api/net/sf/briar/api/serial/StructReader.java
Normal file
8
api/net/sf/briar/api/serial/StructReader.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package net.sf.briar.api.serial;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface StructReader<T> {
|
||||
|
||||
T readStruct(Reader r) throws IOException;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class AckReaderTest extends BriarTestCase {
|
||||
byte[] b = createAck(true);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
reader.addStructReader(Types.ACK, ackReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.ACK, Ack.class);
|
||||
@@ -72,7 +72,7 @@ public class AckReaderTest extends BriarTestCase {
|
||||
byte[] b = createAck(false);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
reader.addStructReader(Types.ACK, ackReader);
|
||||
|
||||
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
|
||||
context.assertIsSatisfied();
|
||||
@@ -86,7 +86,7 @@ public class AckReaderTest extends BriarTestCase {
|
||||
byte[] b = createEmptyAck();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
reader.addStructReader(Types.ACK, ackReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.ACK, Ack.class);
|
||||
|
||||
@@ -10,7 +10,7 @@ import net.sf.briar.api.FormatException;
|
||||
import net.sf.briar.api.protocol.ProtocolConstants;
|
||||
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;
|
||||
import net.sf.briar.api.serial.Writer;
|
||||
@@ -30,7 +30,7 @@ public class BatchReaderTest extends BriarTestCase {
|
||||
private final WriterFactory writerFactory;
|
||||
private final Mockery context;
|
||||
private final UnverifiedMessage message;
|
||||
private final ObjectReader<UnverifiedMessage> messageReader;
|
||||
private final StructReader<UnverifiedMessage> messageReader;
|
||||
|
||||
public BatchReaderTest() throws Exception {
|
||||
super();
|
||||
@@ -51,7 +51,7 @@ public class BatchReaderTest extends BriarTestCase {
|
||||
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH + 1);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
reader.addStructReader(Types.BATCH, batchReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.BATCH, UnverifiedBatch.class);
|
||||
@@ -75,7 +75,7 @@ public class BatchReaderTest extends BriarTestCase {
|
||||
byte[] b = createBatch(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
reader.addStructReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH,
|
||||
UnverifiedBatch.class));
|
||||
@@ -91,7 +91,7 @@ public class BatchReaderTest extends BriarTestCase {
|
||||
byte[] b = createEmptyBatch();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
reader.addStructReader(Types.BATCH, batchReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.BATCH, UnverifiedBatch.class);
|
||||
@@ -123,9 +123,9 @@ public class BatchReaderTest extends BriarTestCase {
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private class TestMessageReader implements ObjectReader<UnverifiedMessage> {
|
||||
private class TestMessageReader implements StructReader<UnverifiedMessage> {
|
||||
|
||||
public UnverifiedMessage readObject(Reader r) throws IOException {
|
||||
public UnverifiedMessage readStruct(Reader r) throws IOException {
|
||||
r.readStructId(Types.MESSAGE);
|
||||
r.readBytes();
|
||||
return message;
|
||||
|
||||
@@ -49,7 +49,7 @@ public class OfferReaderTest extends BriarTestCase {
|
||||
byte[] b = createOffer(true);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.OFFER, offerReader);
|
||||
reader.addStructReader(Types.OFFER, offerReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.OFFER, Offer.class);
|
||||
@@ -72,7 +72,7 @@ public class OfferReaderTest extends BriarTestCase {
|
||||
byte[] b = createOffer(false);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.OFFER, offerReader);
|
||||
reader.addStructReader(Types.OFFER, offerReader);
|
||||
|
||||
assertEquals(offer, reader.readStruct(Types.OFFER, Offer.class));
|
||||
context.assertIsSatisfied();
|
||||
@@ -86,7 +86,7 @@ public class OfferReaderTest extends BriarTestCase {
|
||||
byte[] b = createEmptyOffer();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.OFFER, offerReader);
|
||||
reader.addStructReader(Types.OFFER, offerReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.OFFER, Offer.class);
|
||||
|
||||
@@ -49,7 +49,7 @@ public class RequestReaderTest extends BriarTestCase {
|
||||
byte[] b = createRequest(true);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
reader.addStructReader(Types.REQUEST, requestReader);
|
||||
|
||||
try {
|
||||
reader.readStruct(Types.REQUEST, Request.class);
|
||||
@@ -72,7 +72,7 @@ public class RequestReaderTest extends BriarTestCase {
|
||||
byte[] b = createRequest(false);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
reader.addStructReader(Types.REQUEST, requestReader);
|
||||
|
||||
assertEquals(request, reader.readStruct(Types.REQUEST,
|
||||
Request.class));
|
||||
@@ -102,7 +102,7 @@ public class RequestReaderTest extends BriarTestCase {
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(b);
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
RequestReader requestReader = new RequestReader(packetFactory);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
reader.addStructReader(Types.REQUEST, requestReader);
|
||||
Request r = reader.readStruct(Types.REQUEST, Request.class);
|
||||
BitSet decoded = r.getBitmap();
|
||||
// Check that the decoded BitSet matches the original - we can't
|
||||
|
||||
@@ -14,7 +14,7 @@ import net.sf.briar.BriarTestCase;
|
||||
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;
|
||||
import net.sf.briar.util.StringUtils;
|
||||
|
||||
@@ -373,15 +373,15 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testReadStruct() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
// Add readers for two structs
|
||||
r.addStructReader(0, new StructReader<Foo>() {
|
||||
public Foo readStruct(Reader r) throws IOException {
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.addStructReader(255, new StructReader<Bar>() {
|
||||
public Bar readStruct(Reader r) throws IOException {
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
@@ -396,15 +396,15 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testReadStructWithConsumer() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
// Add readers for two structs
|
||||
r.addStructReader(0, new StructReader<Foo>() {
|
||||
public Foo readStruct(Reader r) throws IOException {
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.addStructReader(255, new StructReader<Bar>() {
|
||||
public Bar readStruct(Reader r) throws IOException {
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
@@ -435,7 +435,7 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
public void testUnknownStructIdThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
assertTrue(r.hasStruct(0));
|
||||
// No object reader has been added for struct ID 0
|
||||
// No reader has been added for struct ID 0
|
||||
try {
|
||||
r.readStruct(0, Foo.class);
|
||||
fail();
|
||||
@@ -445,15 +445,15 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testWrongClassThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
// Add an object reader for struct ID 0, class Foo
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
// Add a reader for struct ID 0, class Foo
|
||||
r.addStructReader(0, new StructReader<Foo>() {
|
||||
public Foo readStruct(Reader r) throws IOException {
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
assertTrue(r.hasStruct(0));
|
||||
// Trying to read the object as class Bar should throw a FormatException
|
||||
// Trying to read the struct as class Bar should throw a FormatException
|
||||
try {
|
||||
r.readStruct(0, Bar.class);
|
||||
fail();
|
||||
@@ -461,38 +461,38 @@ public class ReaderImplTest extends BriarTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadListUsingObjectReader() throws Exception {
|
||||
public void testReadListUsingStructReader() throws Exception {
|
||||
setContents("A" + "1" + "C0" + "83666F6F");
|
||||
// Add an object reader for a struct
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
// Add a reader for a struct
|
||||
r.addStructReader(0, new StructReader<Foo>() {
|
||||
public Foo readStruct(Reader r) throws IOException {
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
// Check that the object reader is used for lists
|
||||
// Check that the reader is used for lists
|
||||
List<Foo> l = r.readList(Foo.class);
|
||||
assertEquals(1, l.size());
|
||||
assertEquals("foo", l.get(0).s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadMapUsingObjectReader() throws Exception {
|
||||
public void testReadMapUsingStructReader() throws Exception {
|
||||
setContents("B" + "1" + "C0" + "83666F6F" + "C1" + "83626172");
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
// Add readers for two structs
|
||||
r.addStructReader(0, new StructReader<Foo>() {
|
||||
public Foo readStruct(Reader r) throws IOException {
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(1, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.addStructReader(1, new StructReader<Bar>() {
|
||||
public Bar readStruct(Reader r) throws IOException {
|
||||
r.readStructId(1);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
// Check that the object readers are used for maps
|
||||
// Check that the readers are used for maps
|
||||
Map<Foo, Bar> m = r.readMap(Foo.class, Bar.class);
|
||||
assertEquals(1, m.size());
|
||||
Entry<Foo, Bar> e = m.entrySet().iterator().next();
|
||||
|
||||
Reference in New Issue
Block a user