mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Renamed "user-defined types" as "structs" in the serialisation format.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package net.sf.briar.api.protocol;
|
||||
|
||||
/** User-defined type identifiers for encoding and decoding protocol objects. */
|
||||
/** Struct identifiers for encoding and decoding protocol objects. */
|
||||
public interface Types {
|
||||
|
||||
static final int ACK = 0;
|
||||
|
||||
@@ -69,7 +69,7 @@ public interface Reader {
|
||||
boolean hasNull() throws IOException;
|
||||
void readNull() throws IOException;
|
||||
|
||||
boolean hasUserDefined(int id) throws IOException;
|
||||
<T> T readUserDefined(int id, Class<T> t) throws IOException;
|
||||
void readUserDefinedId(int id) throws IOException;
|
||||
boolean hasStruct(int id) throws IOException;
|
||||
<T> T readStruct(int id, Class<T> t) throws IOException;
|
||||
void readStructId(int id) throws IOException;
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ public interface SerialComponent {
|
||||
|
||||
int getSerialisedUniqueIdLength(int id);
|
||||
|
||||
int getSerialisedUserDefinedIdLength(int id);
|
||||
int getSerialisedStructIdLength(int id);
|
||||
}
|
||||
|
||||
@@ -34,5 +34,5 @@ public interface Writer {
|
||||
|
||||
void writeNull() throws IOException;
|
||||
|
||||
void writeUserDefinedId(int tag) throws IOException;
|
||||
void writeStructId(int id) throws IOException;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class AckReader implements ObjectReader<Ack> {
|
||||
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Read and digest the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedId(Types.ACK);
|
||||
r.readStructId(Types.ACK);
|
||||
r.addObjectReader(Types.BATCH_ID, batchIdReader);
|
||||
Collection<BatchId> batches = r.readList(BatchId.class);
|
||||
r.removeObjectReader(Types.BATCH_ID);
|
||||
@@ -42,7 +42,7 @@ class AckReader implements ObjectReader<Ack> {
|
||||
private static class BatchIdReader implements ObjectReader<BatchId> {
|
||||
|
||||
public BatchId readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.BATCH_ID);
|
||||
r.readStructId(Types.BATCH_ID);
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
return new BatchId(b);
|
||||
|
||||
@@ -29,7 +29,7 @@ class AuthorFactoryImpl implements AuthorFactory {
|
||||
throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.AUTHOR);
|
||||
w.writeStructId(Types.AUTHOR);
|
||||
w.writeString(name);
|
||||
w.writeBytes(publicKey);
|
||||
MessageDigest messageDigest = crypto.getMessageDigest();
|
||||
|
||||
@@ -29,7 +29,7 @@ class AuthorReader implements ObjectReader<Author> {
|
||||
messageDigest.reset();
|
||||
// Read and digest the data
|
||||
r.addConsumer(digesting);
|
||||
r.readUserDefinedId(Types.AUTHOR);
|
||||
r.readStructId(Types.AUTHOR);
|
||||
String name = r.readString(ProtocolConstants.MAX_AUTHOR_NAME_LENGTH);
|
||||
byte[] publicKey = r.readBytes(ProtocolConstants.MAX_PUBLIC_KEY_LENGTH);
|
||||
r.removeConsumer(digesting);
|
||||
|
||||
@@ -38,7 +38,7 @@ class BatchReader implements ObjectReader<Batch> {
|
||||
// Read and digest the data
|
||||
r.addConsumer(counting);
|
||||
r.addConsumer(digesting);
|
||||
r.readUserDefinedId(Types.BATCH);
|
||||
r.readStructId(Types.BATCH);
|
||||
r.addObjectReader(Types.MESSAGE, messageReader);
|
||||
List<Message> messages = r.readList(Message.class);
|
||||
r.removeObjectReader(Types.MESSAGE);
|
||||
|
||||
@@ -28,7 +28,7 @@ class GroupFactoryImpl implements GroupFactory {
|
||||
public Group createGroup(String name, byte[] publicKey) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.GROUP);
|
||||
w.writeStructId(Types.GROUP);
|
||||
w.writeString(name);
|
||||
if(publicKey == null) w.writeNull();
|
||||
else w.writeBytes(publicKey);
|
||||
|
||||
@@ -29,7 +29,7 @@ class GroupReader implements ObjectReader<Group> {
|
||||
messageDigest.reset();
|
||||
// Read and digest the data
|
||||
r.addConsumer(digesting);
|
||||
r.readUserDefinedId(Types.GROUP);
|
||||
r.readStructId(Types.GROUP);
|
||||
String name = r.readString(ProtocolConstants.MAX_GROUP_NAME_LENGTH);
|
||||
byte[] publicKey = null;
|
||||
if(r.hasNull()) r.readNull();
|
||||
|
||||
@@ -107,7 +107,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
w.addConsumer(groupConsumer);
|
||||
}
|
||||
// Write the message
|
||||
w.writeUserDefinedId(Types.MESSAGE);
|
||||
w.writeStructId(Types.MESSAGE);
|
||||
if(parent == null) w.writeNull();
|
||||
else w.writeBytes(parent.getBytes());
|
||||
if(group == null) w.writeNull();
|
||||
@@ -153,7 +153,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
}
|
||||
|
||||
private void writeGroup(Writer w, Group g) throws IOException {
|
||||
w.writeUserDefinedId(Types.GROUP);
|
||||
w.writeStructId(Types.GROUP);
|
||||
w.writeString(g.getName());
|
||||
byte[] publicKey = g.getPublicKey();
|
||||
if(publicKey == null) w.writeNull();
|
||||
@@ -161,7 +161,7 @@ class MessageFactoryImpl implements MessageFactory {
|
||||
}
|
||||
|
||||
private void writeAuthor(Writer w, Author a) throws IOException {
|
||||
w.writeUserDefinedId(Types.AUTHOR);
|
||||
w.writeStructId(Types.AUTHOR);
|
||||
w.writeString(a.getName());
|
||||
w.writeBytes(a.getPublicKey());
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import net.sf.briar.api.serial.Reader;
|
||||
class MessageIdReader implements ObjectReader<MessageId> {
|
||||
|
||||
public MessageId readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.MESSAGE_ID);
|
||||
r.readStructId(Types.MESSAGE_ID);
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
return new MessageId(b);
|
||||
|
||||
@@ -50,14 +50,14 @@ class MessageReader implements ObjectReader<Message> {
|
||||
r.addConsumer(copying);
|
||||
r.addConsumer(counting);
|
||||
// Read the initial tag
|
||||
r.readUserDefinedId(Types.MESSAGE);
|
||||
r.readStructId(Types.MESSAGE);
|
||||
// Read the parent's message ID, if there is one
|
||||
MessageId parent = null;
|
||||
if(r.hasNull()) {
|
||||
r.readNull();
|
||||
} else {
|
||||
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
|
||||
parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class);
|
||||
parent = r.readStruct(Types.MESSAGE_ID, MessageId.class);
|
||||
r.removeObjectReader(Types.MESSAGE_ID);
|
||||
}
|
||||
// Read the group, if there is one
|
||||
@@ -66,7 +66,7 @@ class MessageReader implements ObjectReader<Message> {
|
||||
r.readNull();
|
||||
} else {
|
||||
r.addObjectReader(Types.GROUP, groupReader);
|
||||
group = r.readUserDefined(Types.GROUP, Group.class);
|
||||
group = r.readStruct(Types.GROUP, Group.class);
|
||||
r.removeObjectReader(Types.GROUP);
|
||||
}
|
||||
// Read the author, if there is one
|
||||
@@ -75,7 +75,7 @@ class MessageReader implements ObjectReader<Message> {
|
||||
r.readNull();
|
||||
} else {
|
||||
r.addObjectReader(Types.AUTHOR, authorReader);
|
||||
author = r.readUserDefined(Types.AUTHOR, Author.class);
|
||||
author = r.readStruct(Types.AUTHOR, Author.class);
|
||||
r.removeObjectReader(Types.AUTHOR);
|
||||
}
|
||||
// Read the subject
|
||||
|
||||
@@ -29,7 +29,7 @@ class OfferReader implements ObjectReader<Offer> {
|
||||
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedId(Types.OFFER);
|
||||
r.readStructId(Types.OFFER);
|
||||
r.addObjectReader(Types.MESSAGE_ID, messageIdReader);
|
||||
Collection<MessageId> messages = r.readList(MessageId.class);
|
||||
r.removeObjectReader(Types.MESSAGE_ID);
|
||||
|
||||
@@ -39,51 +39,51 @@ class ProtocolReaderImpl implements ProtocolReader {
|
||||
}
|
||||
|
||||
public boolean hasAck() throws IOException {
|
||||
return reader.hasUserDefined(Types.ACK);
|
||||
return reader.hasStruct(Types.ACK);
|
||||
}
|
||||
|
||||
public Ack readAck() throws IOException {
|
||||
return reader.readUserDefined(Types.ACK, Ack.class);
|
||||
return reader.readStruct(Types.ACK, Ack.class);
|
||||
}
|
||||
|
||||
public boolean hasBatch() throws IOException {
|
||||
return reader.hasUserDefined(Types.BATCH);
|
||||
return reader.hasStruct(Types.BATCH);
|
||||
}
|
||||
|
||||
public Batch readBatch() throws IOException {
|
||||
return reader.readUserDefined(Types.BATCH, Batch.class);
|
||||
return reader.readStruct(Types.BATCH, Batch.class);
|
||||
}
|
||||
|
||||
public boolean hasOffer() throws IOException {
|
||||
return reader.hasUserDefined(Types.OFFER);
|
||||
return reader.hasStruct(Types.OFFER);
|
||||
}
|
||||
|
||||
public Offer readOffer() throws IOException {
|
||||
return reader.readUserDefined(Types.OFFER, Offer.class);
|
||||
return reader.readStruct(Types.OFFER, Offer.class);
|
||||
}
|
||||
|
||||
public boolean hasRequest() throws IOException {
|
||||
return reader.hasUserDefined(Types.REQUEST);
|
||||
return reader.hasStruct(Types.REQUEST);
|
||||
}
|
||||
|
||||
public Request readRequest() throws IOException {
|
||||
return reader.readUserDefined(Types.REQUEST, Request.class);
|
||||
return reader.readStruct(Types.REQUEST, Request.class);
|
||||
}
|
||||
|
||||
public boolean hasSubscriptionUpdate() throws IOException {
|
||||
return reader.hasUserDefined(Types.SUBSCRIPTION_UPDATE);
|
||||
return reader.hasStruct(Types.SUBSCRIPTION_UPDATE);
|
||||
}
|
||||
|
||||
public SubscriptionUpdate readSubscriptionUpdate() throws IOException {
|
||||
return reader.readUserDefined(Types.SUBSCRIPTION_UPDATE,
|
||||
return reader.readStruct(Types.SUBSCRIPTION_UPDATE,
|
||||
SubscriptionUpdate.class);
|
||||
}
|
||||
|
||||
public boolean hasTransportUpdate() throws IOException {
|
||||
return reader.hasUserDefined(Types.TRANSPORT_UPDATE);
|
||||
return reader.hasStruct(Types.TRANSPORT_UPDATE);
|
||||
}
|
||||
|
||||
public TransportUpdate readTransportUpdate() throws IOException {
|
||||
return reader.readUserDefined(Types.TRANSPORT_UPDATE, TransportUpdate.class);
|
||||
return reader.readStruct(Types.TRANSPORT_UPDATE, TransportUpdate.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class RequestReader implements ObjectReader<Request> {
|
||||
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedId(Types.REQUEST);
|
||||
r.readStructId(Types.REQUEST);
|
||||
byte[] bitmap = r.readBytes(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
r.removeConsumer(counting);
|
||||
// Convert the bitmap into a BitSet
|
||||
|
||||
@@ -30,7 +30,7 @@ class SubscriptionUpdateReader implements ObjectReader<SubscriptionUpdate> {
|
||||
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedId(Types.SUBSCRIPTION_UPDATE);
|
||||
r.readStructId(Types.SUBSCRIPTION_UPDATE);
|
||||
r.addObjectReader(Types.GROUP, groupReader);
|
||||
Map<Group, Long> subs = r.readMap(Group.class, Long.class);
|
||||
r.removeObjectReader(Types.GROUP);
|
||||
|
||||
@@ -35,7 +35,7 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
|
||||
new CountingConsumer(ProtocolConstants.MAX_PACKET_LENGTH);
|
||||
// Read the data
|
||||
r.addConsumer(counting);
|
||||
r.readUserDefinedId(Types.TRANSPORT_UPDATE);
|
||||
r.readStructId(Types.TRANSPORT_UPDATE);
|
||||
r.addObjectReader(Types.TRANSPORT, transportReader);
|
||||
Collection<Transport> transports = r.readList(Transport.class);
|
||||
r.removeObjectReader(Types.TRANSPORT);
|
||||
@@ -58,7 +58,7 @@ class TransportUpdateReader implements ObjectReader<TransportUpdate> {
|
||||
private static class TransportReader implements ObjectReader<Transport> {
|
||||
|
||||
public Transport readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.TRANSPORT);
|
||||
r.readStructId(Types.TRANSPORT);
|
||||
// Read the ID
|
||||
byte[] b = r.readBytes(UniqueId.LENGTH);
|
||||
if(b.length != UniqueId.LENGTH) throw new FormatException();
|
||||
|
||||
@@ -23,7 +23,7 @@ class AckWriterImpl implements AckWriter {
|
||||
AckWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.ACK)
|
||||
headerLength = serial.getSerialisedStructIdLength(Types.ACK)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
idLength = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
@@ -41,7 +41,7 @@ class AckWriterImpl implements AckWriter {
|
||||
int overhead = started ? footerLength : headerLength + footerLength;
|
||||
if(capacity < idLength + overhead) return false;
|
||||
if(!started) start();
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeStructId(Types.BATCH_ID);
|
||||
w.writeBytes(b.getBytes());
|
||||
capacity -= idLength;
|
||||
return true;
|
||||
@@ -56,7 +56,7 @@ class AckWriterImpl implements AckWriter {
|
||||
}
|
||||
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeStructId(Types.ACK);
|
||||
w.writeListStart();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
|
||||
@@ -28,7 +28,7 @@ class BatchWriterImpl implements BatchWriter {
|
||||
BatchWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory, MessageDigest messageDigest) {
|
||||
this.out = out;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.BATCH)
|
||||
headerLength = serial.getSerialisedStructIdLength(Types.BATCH)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
w = writerFactory.createWriter(this.out);
|
||||
@@ -70,7 +70,7 @@ class BatchWriterImpl implements BatchWriter {
|
||||
private void start() throws IOException {
|
||||
messageDigest.reset();
|
||||
w.addConsumer(digestingConsumer);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeStructId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
remaining -= headerLength;
|
||||
started = true;
|
||||
|
||||
@@ -23,7 +23,7 @@ class OfferWriterImpl implements OfferWriter {
|
||||
OfferWriterImpl(OutputStream out, SerialComponent serial,
|
||||
WriterFactory writerFactory) {
|
||||
this.out = out;
|
||||
headerLength = serial.getSerialisedUserDefinedIdLength(Types.OFFER)
|
||||
headerLength = serial.getSerialisedStructIdLength(Types.OFFER)
|
||||
+ serial.getSerialisedListStartLength();
|
||||
idLength = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
|
||||
footerLength = serial.getSerialisedListEndLength();
|
||||
@@ -41,7 +41,7 @@ class OfferWriterImpl implements OfferWriter {
|
||||
int overhead = started ? footerLength : headerLength + footerLength;
|
||||
if(capacity < idLength + overhead) return false;
|
||||
if(!started) start();
|
||||
w.writeUserDefinedId(Types.MESSAGE_ID);
|
||||
w.writeStructId(Types.MESSAGE_ID);
|
||||
w.writeBytes(m.getBytes());
|
||||
capacity -= idLength;
|
||||
return true;
|
||||
@@ -56,7 +56,7 @@ class OfferWriterImpl implements OfferWriter {
|
||||
}
|
||||
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedId(Types.OFFER);
|
||||
w.writeStructId(Types.OFFER);
|
||||
w.writeListStart();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
|
||||
@@ -21,7 +21,7 @@ class RequestWriterImpl implements RequestWriter {
|
||||
|
||||
public void writeRequest(BitSet b, int length)
|
||||
throws IOException {
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeStructId(Types.REQUEST);
|
||||
// If the number of bits isn't a multiple of 8, round up to a byte
|
||||
int bytes = length % 8 == 0 ? length / 8 : length / 8 + 1;
|
||||
byte[] bitmap = new byte[bytes];
|
||||
|
||||
@@ -24,7 +24,7 @@ class SubscriptionUpdateWriterImpl implements SubscriptionUpdateWriter {
|
||||
|
||||
public void writeSubscriptions(Map<Group, Long> subs, long timestamp)
|
||||
throws IOException {
|
||||
w.writeUserDefinedId(Types.SUBSCRIPTION_UPDATE);
|
||||
w.writeStructId(Types.SUBSCRIPTION_UPDATE);
|
||||
w.writeMapStart();
|
||||
for(Entry<Group, Long> e : subs.entrySet()) {
|
||||
writeGroup(w, e.getKey());
|
||||
@@ -36,7 +36,7 @@ class SubscriptionUpdateWriterImpl implements SubscriptionUpdateWriter {
|
||||
}
|
||||
|
||||
private void writeGroup(Writer w, Group g) throws IOException {
|
||||
w.writeUserDefinedId(Types.GROUP);
|
||||
w.writeStructId(Types.GROUP);
|
||||
w.writeString(g.getName());
|
||||
byte[] publicKey = g.getPublicKey();
|
||||
if(publicKey == null) w.writeNull();
|
||||
|
||||
@@ -22,10 +22,10 @@ class TransportUpdateWriterImpl implements TransportUpdateWriter {
|
||||
|
||||
public void writeTransports(Collection<Transport> transports,
|
||||
long timestamp) throws IOException {
|
||||
w.writeUserDefinedId(Types.TRANSPORT_UPDATE);
|
||||
w.writeStructId(Types.TRANSPORT_UPDATE);
|
||||
w.writeListStart();
|
||||
for(Transport p : transports) {
|
||||
w.writeUserDefinedId(Types.TRANSPORT);
|
||||
w.writeStructId(Types.TRANSPORT);
|
||||
w.writeBytes(p.getId().getBytes());
|
||||
w.writeInt32(p.getIndex().getInt());
|
||||
w.writeMap(p);
|
||||
|
||||
@@ -51,7 +51,7 @@ class ReaderImpl implements Reader {
|
||||
}
|
||||
next = (byte) i;
|
||||
// If necessary, read another lookahead byte
|
||||
if(next == Tag.USER) {
|
||||
if(next == Tag.STRUCT) {
|
||||
i = in.read();
|
||||
if(i == -1) throw new FormatException();
|
||||
nextNext = (byte) i;
|
||||
@@ -64,7 +64,7 @@ class ReaderImpl implements Reader {
|
||||
assert hasLookahead;
|
||||
for(Consumer c : consumers) {
|
||||
c.write(next);
|
||||
if(next == Tag.USER) c.write(nextNext);
|
||||
if(next == Tag.STRUCT) c.write(nextNext);
|
||||
}
|
||||
hasLookahead = false;
|
||||
}
|
||||
@@ -376,7 +376,7 @@ class ReaderImpl implements Reader {
|
||||
}
|
||||
|
||||
private Object readObject() throws IOException {
|
||||
if(hasUserDefined()) return readUserDefined();
|
||||
if(hasStruct()) return readStruct();
|
||||
if(hasBoolean()) return Boolean.valueOf(readBoolean());
|
||||
if(hasUint7()) return Byte.valueOf(readUint7());
|
||||
if(hasInt8()) return Byte.valueOf(readInt8());
|
||||
@@ -396,19 +396,19 @@ class ReaderImpl implements Reader {
|
||||
throw new FormatException();
|
||||
}
|
||||
|
||||
private boolean hasUserDefined() throws IOException {
|
||||
private boolean hasStruct() throws IOException {
|
||||
if(!hasLookahead) readLookahead(true);
|
||||
if(eof) return false;
|
||||
return next == Tag.USER
|
||||
|| (next & Tag.SHORT_USER_MASK) == Tag.SHORT_USER;
|
||||
return next == Tag.STRUCT
|
||||
|| (next & Tag.SHORT_STRUCT_MASK) == Tag.SHORT_STRUCT;
|
||||
}
|
||||
|
||||
private Object readUserDefined() throws IOException {
|
||||
if(!hasUserDefined()) throw new FormatException();
|
||||
int tag;
|
||||
if(next == Tag.USER) tag = 0xFF & nextNext;
|
||||
else tag = 0xFF & next ^ Tag.SHORT_USER;
|
||||
return readUserDefined(tag, Object.class);
|
||||
private Object readStruct() throws IOException {
|
||||
if(!hasStruct()) throw new FormatException();
|
||||
int id;
|
||||
if(next == Tag.STRUCT) id = 0xFF & nextNext;
|
||||
else id = 0xFF & next ^ Tag.SHORT_STRUCT;
|
||||
return readStruct(id, Object.class);
|
||||
}
|
||||
|
||||
private <T> T readObject(Class<T> t) throws IOException {
|
||||
@@ -529,19 +529,19 @@ class ReaderImpl implements Reader {
|
||||
consumeLookahead();
|
||||
}
|
||||
|
||||
public boolean hasUserDefined(int id) throws IOException {
|
||||
public boolean hasStruct(int id) throws IOException {
|
||||
if(id < 0 || id > 255) throw new IllegalArgumentException();
|
||||
if(!hasLookahead) readLookahead(true);
|
||||
if(eof) return false;
|
||||
if(next == Tag.USER)
|
||||
if(next == Tag.STRUCT)
|
||||
return id == (0xFF & nextNext);
|
||||
else if((next & Tag.SHORT_USER_MASK) == Tag.SHORT_USER)
|
||||
return id == (0xFF & next ^ Tag.SHORT_USER);
|
||||
else if((next & Tag.SHORT_STRUCT_MASK) == Tag.SHORT_STRUCT)
|
||||
return id == (0xFF & next ^ Tag.SHORT_STRUCT);
|
||||
else return false;
|
||||
}
|
||||
|
||||
public <T> T readUserDefined(int id, Class<T> t) throws IOException {
|
||||
if(!hasUserDefined(id)) throw new FormatException();
|
||||
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();
|
||||
@@ -552,8 +552,8 @@ class ReaderImpl implements Reader {
|
||||
}
|
||||
}
|
||||
|
||||
public void readUserDefinedId(int id) throws IOException {
|
||||
if(!hasUserDefined(id)) throw new FormatException();
|
||||
public void readStructId(int id) throws IOException {
|
||||
if(!hasStruct(id)) throw new FormatException();
|
||||
consumeLookahead();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,18 @@ import net.sf.briar.api.serial.SerialComponent;
|
||||
class SerialComponentImpl implements SerialComponent {
|
||||
|
||||
public int getSerialisedListEndLength() {
|
||||
// END tag
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int getSerialisedListStartLength() {
|
||||
// LIST_START tag
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int getSerialisedUniqueIdLength(int id) {
|
||||
// User-defined ID, BYTES tag, length spec, bytes
|
||||
return getSerialisedUserDefinedIdLength(id) + 1
|
||||
// Struct ID, BYTES tag, length spec, bytes
|
||||
return getSerialisedStructIdLength(id) + 1
|
||||
+ getSerialisedLengthSpecLength(UniqueId.LENGTH) + UniqueId.LENGTH;
|
||||
}
|
||||
|
||||
@@ -26,7 +28,7 @@ class SerialComponentImpl implements SerialComponent {
|
||||
return 5; // Int32
|
||||
}
|
||||
|
||||
public int getSerialisedUserDefinedIdLength(int id) {
|
||||
public int getSerialisedStructIdLength(int id) {
|
||||
assert id >= 0 && id <= 255;
|
||||
return id < 32 ? 1 : 2;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ interface Tag {
|
||||
static final byte MAP_START = -14; // 1111 0010
|
||||
static final byte END = -15; // 1111 0001
|
||||
static final byte NULL = -16; // 1111 0000
|
||||
static final byte USER = -17; // 1110 1111
|
||||
static final byte STRUCT = -17; // 1110 1111
|
||||
|
||||
static final int SHORT_MASK = 0xF0; // Match first four bits
|
||||
static final int SHORT_STRING = 0x80; // 1000 xxxx
|
||||
@@ -26,6 +26,6 @@ interface Tag {
|
||||
static final int SHORT_LIST = 0xA0; // 1010 xxxx
|
||||
static final int SHORT_MAP = 0xB0; // 1011 xxxx
|
||||
|
||||
static final int SHORT_USER_MASK = 0xE0; // Match first three bits
|
||||
static final int SHORT_USER = 0xC0; // 110x xxxx
|
||||
static final int SHORT_STRUCT_MASK = 0xE0; // Match first three bits
|
||||
static final int SHORT_STRUCT = 0xC0; // 110x xxxx
|
||||
}
|
||||
|
||||
@@ -187,12 +187,12 @@ class WriterImpl implements Writer {
|
||||
write(Tag.NULL);
|
||||
}
|
||||
|
||||
public void writeUserDefinedId(int id) throws IOException {
|
||||
public void writeStructId(int id) throws IOException {
|
||||
if(id < 0 || id > 255) throw new IllegalArgumentException();
|
||||
if(id < 32) {
|
||||
write((byte) (Tag.SHORT_USER | id));
|
||||
write((byte) (Tag.SHORT_STRUCT | id));
|
||||
} else {
|
||||
write(Tag.USER);
|
||||
write(Tag.STRUCT);
|
||||
write((byte) id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class AckReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.ACK, Ack.class);
|
||||
reader.readStruct(Types.ACK, Ack.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -73,7 +73,7 @@ public class AckReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
assertEquals(ack, reader.readUserDefined(Types.ACK, Ack.class));
|
||||
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -93,25 +93,25 @@ public class AckReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.ACK, ackReader);
|
||||
|
||||
assertEquals(ack, reader.readUserDefined(Types.ACK, Ack.class));
|
||||
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
private byte[] createAck(boolean tooBig) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeStructId(Types.ACK);
|
||||
w.writeListStart();
|
||||
byte[] b = new byte[UniqueId.LENGTH];
|
||||
Random random = new Random();
|
||||
while(out.size() + BatchId.LENGTH + 3
|
||||
< ProtocolConstants.MAX_PACKET_LENGTH) {
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeStructId(Types.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
}
|
||||
if(tooBig) {
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeStructId(Types.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public class AckReaderTest extends TestCase {
|
||||
private byte[] createEmptyAck() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeStructId(Types.ACK);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
|
||||
@@ -61,7 +61,7 @@ public class BatchReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.BATCH, Batch.class);
|
||||
reader.readStruct(Types.BATCH, Batch.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -85,7 +85,7 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -136,17 +136,17 @@ public class BatchReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.BATCH, batchReader);
|
||||
|
||||
assertEquals(batch, reader.readUserDefined(Types.BATCH, Batch.class));
|
||||
assertEquals(batch, reader.readStruct(Types.BATCH, Batch.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
private byte[] createBatch(int size) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeStructId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
// We're using a fake message reader, so it's OK to use a fake message
|
||||
w.writeUserDefinedId(Types.MESSAGE);
|
||||
w.writeStructId(Types.MESSAGE);
|
||||
w.writeBytes(new byte[size - 10]);
|
||||
w.writeListEnd();
|
||||
byte[] b = out.toByteArray();
|
||||
@@ -157,7 +157,7 @@ public class BatchReaderTest extends TestCase {
|
||||
private byte[] createEmptyBatch() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeStructId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
@@ -166,7 +166,7 @@ public class BatchReaderTest extends TestCase {
|
||||
private class TestMessageReader implements ObjectReader<Message> {
|
||||
|
||||
public Message readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(Types.MESSAGE);
|
||||
r.readStructId(Types.MESSAGE);
|
||||
r.readBytes();
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class RequestReaderTest extends TestCase {
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
|
||||
try {
|
||||
reader.readUserDefined(Types.REQUEST, Request.class);
|
||||
reader.readStruct(Types.REQUEST, Request.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
context.assertIsSatisfied();
|
||||
@@ -69,7 +69,7 @@ public class RequestReaderTest extends TestCase {
|
||||
Reader reader = readerFactory.createReader(in);
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
|
||||
assertEquals(request, reader.readUserDefined(Types.REQUEST,
|
||||
assertEquals(request, reader.readStruct(Types.REQUEST,
|
||||
Request.class));
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -99,7 +99,7 @@ public class RequestReaderTest extends TestCase {
|
||||
RequestReader requestReader =
|
||||
new RequestReader(new RequestFactoryImpl());
|
||||
reader.addObjectReader(Types.REQUEST, requestReader);
|
||||
Request r = reader.readUserDefined(Types.REQUEST, Request.class);
|
||||
Request r = reader.readStruct(Types.REQUEST, Request.class);
|
||||
BitSet decoded = r.getBitmap();
|
||||
// Check that the decoded BitSet matches the original - we can't
|
||||
// use equals() because of padding, but the first i bits should
|
||||
@@ -115,7 +115,7 @@ public class RequestReaderTest extends TestCase {
|
||||
private byte[] createRequest(boolean tooBig) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeStructId(Types.REQUEST);
|
||||
// Allow one byte for the REQUEST tag, one byte for the BYTES tag,
|
||||
// and five bytes for the length as an int32
|
||||
int size = ProtocolConstants.MAX_PACKET_LENGTH - 7;
|
||||
@@ -128,7 +128,7 @@ public class RequestReaderTest extends TestCase {
|
||||
private byte[] createRequest(byte[] bitmap) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeStructId(Types.REQUEST);
|
||||
w.writeBytes(bitmap);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -371,41 +371,41 @@ public class ReaderImplTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUserDefined() throws Exception {
|
||||
public void testReadStruct() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(255);
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
// Test both tag formats, short and long
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertEquals("foo", r.readUserDefined(0, Foo.class).s);
|
||||
assertTrue(r.hasUserDefined(255));
|
||||
assertEquals("foo", r.readUserDefined(255, Bar.class).s);
|
||||
// Test both ID formats, short and long
|
||||
assertTrue(r.hasStruct(0));
|
||||
assertEquals("foo", r.readStruct(0, Foo.class).s);
|
||||
assertTrue(r.hasStruct(255));
|
||||
assertEquals("foo", r.readStruct(255, Bar.class).s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUserDefinedWithConsumer() throws Exception {
|
||||
public void testReadStructWithConsumer() throws Exception {
|
||||
setContents("C0" + "83666F6F" + "EF" + "FF" + "83666F6F");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(255, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(255);
|
||||
r.readStructId(255);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
@@ -421,23 +421,23 @@ public class ReaderImplTest extends TestCase {
|
||||
out.write(b, off, len);
|
||||
}
|
||||
});
|
||||
// Test both tag formats, short and long
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertEquals("foo", r.readUserDefined(0, Foo.class).s);
|
||||
assertTrue(r.hasUserDefined(255));
|
||||
assertEquals("foo", r.readUserDefined(255, Bar.class).s);
|
||||
// Test both ID formats, short and long
|
||||
assertTrue(r.hasStruct(0));
|
||||
assertEquals("foo", r.readStruct(0, Foo.class).s);
|
||||
assertTrue(r.hasStruct(255));
|
||||
assertEquals("foo", r.readStruct(255, Bar.class).s);
|
||||
// Check that everything was passed to the consumer
|
||||
assertEquals("C0" + "83666F6F" + "EF" + "FF" + "83666F6F",
|
||||
StringUtils.toHexString(out.toByteArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnknownTagThrowsFormatException() throws Exception {
|
||||
public void testUnknownStructIdThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
// No object reader has been added for tag 0
|
||||
assertTrue(r.hasStruct(0));
|
||||
// No object reader has been added for struct ID 0
|
||||
try {
|
||||
r.readUserDefined(0, Foo.class);
|
||||
r.readStruct(0, Foo.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
@@ -445,17 +445,17 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testWrongClassThrowsFormatException() throws Exception {
|
||||
setContents("C0" + "83666F6F");
|
||||
// Add an object reader for tag 0, class Foo
|
||||
// Add an object reader for struct ID 0, class Foo
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
assertTrue(r.hasUserDefined(0));
|
||||
assertTrue(r.hasStruct(0));
|
||||
// Trying to read the object as class Bar should throw a FormatException
|
||||
try {
|
||||
r.readUserDefined(0, Bar.class);
|
||||
r.readStruct(0, Bar.class);
|
||||
fail();
|
||||
} catch(FormatException expected) {}
|
||||
}
|
||||
@@ -463,10 +463,10 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testReadListUsingObjectReader() throws Exception {
|
||||
setContents("A" + "1" + "C0" + "83666F6F");
|
||||
// Add an object reader for a user-defined type
|
||||
// Add an object reader for a struct
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
@@ -479,16 +479,16 @@ public class ReaderImplTest extends TestCase {
|
||||
@Test
|
||||
public void testReadMapUsingObjectReader() throws Exception {
|
||||
setContents("B" + "1" + "C0" + "83666F6F" + "C1" + "83626172");
|
||||
// Add object readers for two user-defined types
|
||||
// Add object readers for two structs
|
||||
r.addObjectReader(0, new ObjectReader<Foo>() {
|
||||
public Foo readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(0);
|
||||
r.readStructId(0);
|
||||
return new Foo(r.readString());
|
||||
}
|
||||
});
|
||||
r.addObjectReader(1, new ObjectReader<Bar>() {
|
||||
public Bar readObject(Reader r) throws IOException {
|
||||
r.readUserDefinedId(1);
|
||||
r.readStructId(1);
|
||||
return new Bar(r.readString());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -265,19 +265,19 @@ public class WriterImplTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteShortUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedId(0);
|
||||
w.writeUserDefinedId(31);
|
||||
// SHORT_USER tag (3 bits), 0 (5 bits), SHORT_USER tag (3 bits),
|
||||
public void testWriteShortStructId() throws IOException {
|
||||
w.writeStructId(0);
|
||||
w.writeStructId(31);
|
||||
// SHORT_STRUCT tag (3 bits), 0 (5 bits), SHORT_STRUCT tag (3 bits),
|
||||
// 31 (5 bits)
|
||||
checkContents("C0" + "DF");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedId(32);
|
||||
w.writeUserDefinedId(255);
|
||||
// USER tag, 32 as uint8, USER tag, 255 as uint8
|
||||
public void testWriteStructId() throws IOException {
|
||||
w.writeStructId(32);
|
||||
w.writeStructId(255);
|
||||
// STRUCT tag, 32 as uint8, STRUCT tag, 255 as uint8
|
||||
checkContents("EF" + "20" + "EF" + "FF");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user