Code cleanup: import static.

This commit is contained in:
akwizgran
2013-01-17 16:56:58 +00:00
parent 77a5fea5c8
commit b8247968b6
18 changed files with 124 additions and 104 deletions

View File

@@ -1,5 +1,6 @@
package net.sf.briar.db;
import static java.sql.Types.BINARY;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static net.sf.briar.db.DatabaseConstants.EXPIRY_MODULUS;
@@ -12,7 +13,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -514,10 +514,10 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ZERO())";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getId().getBytes());
if(m.getParent() == null) ps.setNull(2, Types.BINARY);
if(m.getParent() == null) ps.setNull(2, BINARY);
else ps.setBytes(2, m.getParent().getBytes());
ps.setBytes(3, m.getGroup().getBytes());
if(m.getAuthor() == null) ps.setNull(4, Types.BINARY);
if(m.getAuthor() == null) ps.setNull(4, BINARY);
else ps.setBytes(4, m.getAuthor().getBytes());
ps.setString(5, m.getSubject());
ps.setLong(6, m.getTimestamp());
@@ -607,7 +607,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, m.getId().getBytes());
if(m.getParent() == null) ps.setNull(2, Types.BINARY);
if(m.getParent() == null) ps.setNull(2, BINARY);
else ps.setBytes(2, m.getParent().getBytes());
ps.setString(3, m.getSubject());
ps.setLong(4, m.getTimestamp());
@@ -795,7 +795,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt());
ps.setBytes(2, g.getBytes());
if(nextId == null) ps.setNull(3, Types.BINARY); // At the tail
if(nextId == null) ps.setNull(3, BINARY); // At the tail
else ps.setBytes(3, nextId); // In the middle
ps.setLong(4, deleted);
affected = ps.executeUpdate();
@@ -2086,7 +2086,7 @@ abstract class JdbcDatabase implements Database<Connection> {
sql = "UPDATE visibilities SET nextId = ?, deleted = ?"
+ " WHERE contactId = ? AND nextId = ?";
ps1 = txn.prepareStatement(sql);
if(nextId == null) ps1.setNull(1, Types.BINARY); // At the tail
if(nextId == null) ps1.setNull(1, BINARY); // At the tail
else ps1.setBytes(1, nextId); // At the head or in the middle
ps1.setLong(2, now);
ps1.setInt(3, contactId);
@@ -2182,7 +2182,7 @@ abstract class JdbcDatabase implements Database<Connection> {
sql = "UPDATE visibilities SET nextId = ?, deleted = ?"
+ " WHERE contactId = ? AND nextId = ?";
ps = txn.prepareStatement(sql);
if(nextId == null) ps.setNull(1, Types.BINARY); // At the tail
if(nextId == null) ps.setNull(1, BINARY); // At the tail
else ps.setBytes(1, nextId); // At the head or in the middle
ps.setLong(2, clock.currentTimeMillis());
ps.setInt(3, c.getInt());

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.ACK;
import java.io.IOException;
import java.util.ArrayList;
@@ -12,12 +13,11 @@ import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.PacketFactory;
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.StructReader;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.StructReader;
class AckReader implements StructReader<Ack> {
@@ -32,7 +32,7 @@ class AckReader implements StructReader<Ack> {
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readStructId(Types.ACK);
r.readStructId(ACK);
r.setMaxBytesLength(UniqueId.LENGTH);
List<Bytes> raw = r.readList(Bytes.class);
r.resetMaxBytesLength();

View File

@@ -1,5 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.Types.AUTHOR;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -8,7 +10,6 @@ import net.sf.briar.api.crypto.MessageDigest;
import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorFactory;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
@@ -29,7 +30,7 @@ class AuthorFactoryImpl implements AuthorFactory {
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.AUTHOR);
w.writeStructId(AUTHOR);
w.writeString(name);
w.writeBytes(publicKey);
MessageDigest messageDigest = crypto.getMessageDigest();

View File

@@ -2,6 +2,7 @@ package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_AUTHOR_NAME_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PUBLIC_KEY_LENGTH;
import static net.sf.briar.api.protocol.Types.AUTHOR;
import java.io.IOException;
@@ -10,7 +11,6 @@ import net.sf.briar.api.crypto.MessageDigest;
import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorFactory;
import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.DigestingConsumer;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.StructReader;
@@ -30,7 +30,7 @@ class AuthorReader implements StructReader<Author> {
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
// Read and digest the data
r.addConsumer(digesting);
r.readStructId(Types.AUTHOR);
r.readStructId(AUTHOR);
String name = r.readString(MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = r.readBytes(MAX_PUBLIC_KEY_LENGTH);
r.removeConsumer(digesting);

View File

@@ -1,5 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.Types.GROUP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -8,7 +10,6 @@ import net.sf.briar.api.crypto.MessageDigest;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
@@ -28,7 +29,7 @@ class GroupFactoryImpl implements GroupFactory {
public Group createGroup(String name, byte[] publicKey) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.GROUP);
w.writeStructId(GROUP);
w.writeString(name);
if(publicKey == null) w.writeNull();
else w.writeBytes(publicKey);

View File

@@ -2,6 +2,7 @@ package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_GROUP_NAME_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PUBLIC_KEY_LENGTH;
import static net.sf.briar.api.protocol.Types.GROUP;
import java.io.IOException;
@@ -9,7 +10,6 @@ import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.MessageDigest;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.DigestingConsumer;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.StructReader;
@@ -27,7 +27,7 @@ class GroupReader implements StructReader<Group> {
DigestingConsumer digesting = new DigestingConsumer(messageDigest);
// Read and digest the data
r.addConsumer(digesting);
r.readStructId(Types.GROUP);
r.readStructId(GROUP);
String name = r.readString(MAX_GROUP_NAME_LENGTH);
byte[] publicKey = null;
if(r.hasNull()) r.readNull();

View File

@@ -5,6 +5,9 @@ import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_SIGNATURE_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_SUBJECT_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.SALT_LENGTH;
import static net.sf.briar.api.protocol.Types.AUTHOR;
import static net.sf.briar.api.protocol.Types.GROUP;
import static net.sf.briar.api.protocol.Types.MESSAGE;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -23,7 +26,6 @@ import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
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.DigestingConsumer;
@@ -111,7 +113,7 @@ class MessageFactoryImpl implements MessageFactory {
w.addConsumer(groupConsumer);
}
// Write the message
w.writeStructId(Types.MESSAGE);
w.writeStructId(MESSAGE);
if(parent == null) w.writeNull();
else w.writeBytes(parent.getBytes());
if(group == null) w.writeNull();
@@ -157,7 +159,7 @@ class MessageFactoryImpl implements MessageFactory {
}
private void writeGroup(Writer w, Group g) throws IOException {
w.writeStructId(Types.GROUP);
w.writeStructId(GROUP);
w.writeString(g.getName());
byte[] publicKey = g.getPublicKey();
if(publicKey == null) w.writeNull();
@@ -165,7 +167,7 @@ class MessageFactoryImpl implements MessageFactory {
}
private void writeAuthor(Writer w, Author a) throws IOException {
w.writeStructId(Types.AUTHOR);
w.writeStructId(AUTHOR);
w.writeString(a.getName());
w.writeBytes(a.getPublicKey());
}

View File

@@ -5,6 +5,9 @@ import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_SIGNATURE_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_SUBJECT_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.SALT_LENGTH;
import static net.sf.briar.api.protocol.Types.AUTHOR;
import static net.sf.briar.api.protocol.Types.GROUP;
import static net.sf.briar.api.protocol.Types.MESSAGE;
import java.io.IOException;
@@ -12,7 +15,6 @@ import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.protocol.UniqueId;
import net.sf.briar.api.protocol.UnverifiedMessage;
import net.sf.briar.api.serial.CopyingConsumer;
@@ -37,7 +39,7 @@ class MessageReader implements StructReader<UnverifiedMessage> {
r.addConsumer(copying);
r.addConsumer(counting);
// Read the initial tag
r.readStructId(Types.MESSAGE);
r.readStructId(MESSAGE);
// Read the parent's message ID, if there is one
MessageId parent = null;
if(r.hasNull()) {
@@ -52,18 +54,18 @@ class MessageReader implements StructReader<UnverifiedMessage> {
if(r.hasNull()) {
r.readNull();
} else {
r.addStructReader(Types.GROUP, groupReader);
group = r.readStruct(Types.GROUP, Group.class);
r.removeStructReader(Types.GROUP);
r.addStructReader(GROUP, groupReader);
group = r.readStruct(GROUP, Group.class);
r.removeStructReader(GROUP);
}
// Read the author, if there is one
Author author = null;
if(r.hasNull()) {
r.readNull();
} else {
r.addStructReader(Types.AUTHOR, authorReader);
author = r.readStruct(Types.AUTHOR, Author.class);
r.removeStructReader(Types.AUTHOR);
r.addStructReader(AUTHOR, authorReader);
author = r.readStruct(AUTHOR, Author.class);
r.removeStructReader(AUTHOR);
}
// Read the subject
String subject = r.readString(MAX_SUBJECT_LENGTH);

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.OFFER;
import java.io.IOException;
import java.util.ArrayList;
@@ -12,12 +13,11 @@ import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.PacketFactory;
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.StructReader;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.StructReader;
class OfferReader implements StructReader<Offer> {
@@ -32,7 +32,7 @@ class OfferReader implements StructReader<Offer> {
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readStructId(Types.OFFER);
r.readStructId(OFFER);
r.setMaxBytesLength(UniqueId.LENGTH);
List<Bytes> raw = r.readList(Bytes.class);
r.resetMaxBytesLength();

View File

@@ -1,5 +1,12 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.Types.ACK;
import static net.sf.briar.api.protocol.Types.MESSAGE;
import static net.sf.briar.api.protocol.Types.OFFER;
import static net.sf.briar.api.protocol.Types.REQUEST;
import static net.sf.briar.api.protocol.Types.SUBSCRIPTION_UPDATE;
import static net.sf.briar.api.protocol.Types.TRANSPORT_UPDATE;
import java.io.IOException;
import java.io.InputStream;
@@ -9,7 +16,6 @@ import net.sf.briar.api.protocol.ProtocolReader;
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.Types;
import net.sf.briar.api.protocol.UnverifiedMessage;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.ReaderFactory;
@@ -27,12 +33,12 @@ class ProtocolReaderImpl implements ProtocolReader {
StructReader<SubscriptionUpdate> subscriptionReader,
StructReader<TransportUpdate> transportReader) {
reader = readerFactory.createReader(in);
reader.addStructReader(Types.ACK, ackReader);
reader.addStructReader(Types.MESSAGE, messageReader);
reader.addStructReader(Types.OFFER, offerReader);
reader.addStructReader(Types.REQUEST, requestReader);
reader.addStructReader(Types.SUBSCRIPTION_UPDATE, subscriptionReader);
reader.addStructReader(Types.TRANSPORT_UPDATE, transportReader);
reader.addStructReader(ACK, ackReader);
reader.addStructReader(MESSAGE, messageReader);
reader.addStructReader(OFFER, offerReader);
reader.addStructReader(REQUEST, requestReader);
reader.addStructReader(SUBSCRIPTION_UPDATE, subscriptionReader);
reader.addStructReader(TRANSPORT_UPDATE, transportReader);
}
public boolean eof() throws IOException {
@@ -40,51 +46,51 @@ class ProtocolReaderImpl implements ProtocolReader {
}
public boolean hasAck() throws IOException {
return reader.hasStruct(Types.ACK);
return reader.hasStruct(ACK);
}
public Ack readAck() throws IOException {
return reader.readStruct(Types.ACK, Ack.class);
return reader.readStruct(ACK, Ack.class);
}
public boolean hasMessage() throws IOException {
return reader.hasStruct(Types.MESSAGE);
return reader.hasStruct(MESSAGE);
}
public UnverifiedMessage readMessage() throws IOException {
return reader.readStruct(Types.MESSAGE, UnverifiedMessage.class);
return reader.readStruct(MESSAGE, UnverifiedMessage.class);
}
public boolean hasOffer() throws IOException {
return reader.hasStruct(Types.OFFER);
return reader.hasStruct(OFFER);
}
public Offer readOffer() throws IOException {
return reader.readStruct(Types.OFFER, Offer.class);
return reader.readStruct(OFFER, Offer.class);
}
public boolean hasRequest() throws IOException {
return reader.hasStruct(Types.REQUEST);
return reader.hasStruct(REQUEST);
}
public Request readRequest() throws IOException {
return reader.readStruct(Types.REQUEST, Request.class);
return reader.readStruct(REQUEST, Request.class);
}
public boolean hasSubscriptionUpdate() throws IOException {
return reader.hasStruct(Types.SUBSCRIPTION_UPDATE);
return reader.hasStruct(SUBSCRIPTION_UPDATE);
}
public SubscriptionUpdate readSubscriptionUpdate() throws IOException {
return reader.readStruct(Types.SUBSCRIPTION_UPDATE,
return reader.readStruct(SUBSCRIPTION_UPDATE,
SubscriptionUpdate.class);
}
public boolean hasTransportUpdate() throws IOException {
return reader.hasStruct(Types.TRANSPORT_UPDATE);
return reader.hasStruct(TRANSPORT_UPDATE);
}
public TransportUpdate readTransportUpdate() throws IOException {
return reader.readStruct(Types.TRANSPORT_UPDATE, TransportUpdate.class);
return reader.readStruct(TRANSPORT_UPDATE, TransportUpdate.class);
}
}

View File

@@ -1,6 +1,13 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.ACK;
import static net.sf.briar.api.protocol.Types.GROUP;
import static net.sf.briar.api.protocol.Types.OFFER;
import static net.sf.briar.api.protocol.Types.REQUEST;
import static net.sf.briar.api.protocol.Types.SUBSCRIPTION_UPDATE;
import static net.sf.briar.api.protocol.Types.TRANSPORT;
import static net.sf.briar.api.protocol.Types.TRANSPORT_UPDATE;
import java.io.IOException;
import java.io.OutputStream;
@@ -17,7 +24,6 @@ import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.SubscriptionUpdate;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.SerialComponent;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory;
@@ -40,7 +46,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
public int getMaxMessagesForAck(long capacity) {
int packet = (int) Math.min(capacity, MAX_PACKET_LENGTH);
int overhead = serial.getSerialisedStructIdLength(Types.ACK)
int overhead = serial.getSerialisedStructIdLength(ACK)
+ serial.getSerialisedListStartLength()
+ serial.getSerialisedListEndLength();
int idLength = serial.getSerialisedUniqueIdLength();
@@ -49,7 +55,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
public int getMaxMessagesForOffer(long capacity) {
int packet = (int) Math.min(capacity, MAX_PACKET_LENGTH);
int overhead = serial.getSerialisedStructIdLength(Types.OFFER)
int overhead = serial.getSerialisedStructIdLength(OFFER)
+ serial.getSerialisedListStartLength()
+ serial.getSerialisedListEndLength();
int idLength = serial.getSerialisedUniqueIdLength();
@@ -57,7 +63,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
}
public void writeAck(Ack a) throws IOException {
w.writeStructId(Types.ACK);
w.writeStructId(ACK);
w.writeListStart();
for(MessageId m : a.getMessageIds()) w.writeBytes(m.getBytes());
w.writeListEnd();
@@ -70,7 +76,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
}
public void writeOffer(Offer o) throws IOException {
w.writeStructId(Types.OFFER);
w.writeStructId(OFFER);
w.writeListStart();
for(MessageId m : o.getMessageIds()) w.writeBytes(m.getBytes());
w.writeListEnd();
@@ -91,7 +97,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
bitmap[offset] |= bit;
}
}
w.writeStructId(Types.REQUEST);
w.writeStructId(REQUEST);
w.writeUint7((byte) (bytes * 8 - length));
w.writeBytes(bitmap);
if(flush) out.flush();
@@ -99,7 +105,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
public void writeSubscriptionUpdate(SubscriptionUpdate s)
throws IOException {
w.writeStructId(Types.SUBSCRIPTION_UPDATE);
w.writeStructId(SUBSCRIPTION_UPDATE);
// Holes
w.writeMapStart();
for(Entry<GroupId, GroupId> e : s.getHoles().entrySet()) {
@@ -122,7 +128,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
}
private void writeGroup(Writer w, Group g) throws IOException {
w.writeStructId(Types.GROUP);
w.writeStructId(GROUP);
w.writeString(g.getName());
byte[] publicKey = g.getPublicKey();
if(publicKey == null) w.writeNull();
@@ -130,10 +136,10 @@ class ProtocolWriterImpl implements ProtocolWriter {
}
public void writeTransportUpdate(TransportUpdate t) throws IOException {
w.writeStructId(Types.TRANSPORT_UPDATE);
w.writeStructId(TRANSPORT_UPDATE);
w.writeListStart();
for(Transport p : t.getTransports()) {
w.writeStructId(Types.TRANSPORT);
w.writeStructId(TRANSPORT);
w.writeBytes(p.getId().getBytes());
w.writeMap(p.getProperties());
}

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.REQUEST;
import java.io.IOException;
import java.util.BitSet;
@@ -8,11 +9,10 @@ import java.util.BitSet;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.PacketFactory;
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.StructReader;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.StructReader;
class RequestReader implements StructReader<Request> {
@@ -27,7 +27,7 @@ class RequestReader implements StructReader<Request> {
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readStructId(Types.REQUEST);
r.readStructId(REQUEST);
int padding = r.readUint7();
if(padding > 7) throw new FormatException();
byte[] bitmap = r.readBytes(MAX_PACKET_LENGTH);

View File

@@ -1,6 +1,8 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.GROUP;
import static net.sf.briar.api.protocol.Types.SUBSCRIPTION_UPDATE;
import java.io.IOException;
import java.util.HashMap;
@@ -11,7 +13,6 @@ import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.SubscriptionUpdate;
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;
@@ -34,7 +35,7 @@ class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readStructId(Types.SUBSCRIPTION_UPDATE);
r.readStructId(SUBSCRIPTION_UPDATE);
// Holes
Map<GroupId, GroupId> holes = new HashMap<GroupId, GroupId>();
r.setMaxBytesLength(UniqueId.LENGTH);
@@ -49,9 +50,9 @@ class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
r.readMapEnd();
r.resetMaxBytesLength();
// Subscriptions
r.addStructReader(Types.GROUP, groupReader);
r.addStructReader(GROUP, groupReader);
Map<Group, Long> subs = r.readMap(Group.class, Long.class);
r.removeStructReader(Types.GROUP);
r.removeStructReader(GROUP);
// Expiry time
long expiry = r.readInt64();
if(expiry < 0L) throw new FormatException();

View File

@@ -4,6 +4,8 @@ import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PROPERTIES_PER_TRANSPORT;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PROPERTY_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_TRANSPORTS;
import static net.sf.briar.api.protocol.Types.TRANSPORT;
import static net.sf.briar.api.protocol.Types.TRANSPORT_UPDATE;
import java.io.IOException;
import java.util.Collection;
@@ -16,7 +18,6 @@ import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportUpdate;
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;
@@ -38,10 +39,10 @@ class TransportUpdateReader implements StructReader<TransportUpdate> {
Consumer counting = new CountingConsumer(MAX_PACKET_LENGTH);
// Read the data
r.addConsumer(counting);
r.readStructId(Types.TRANSPORT_UPDATE);
r.addStructReader(Types.TRANSPORT, transportReader);
r.readStructId(TRANSPORT_UPDATE);
r.addStructReader(TRANSPORT, transportReader);
Collection<Transport> transports = r.readList(Transport.class);
r.removeStructReader(Types.TRANSPORT);
r.removeStructReader(TRANSPORT);
if(transports.size() > MAX_TRANSPORTS) throw new FormatException();
long timestamp = r.readInt64();
r.removeConsumer(counting);
@@ -57,7 +58,7 @@ class TransportUpdateReader implements StructReader<TransportUpdate> {
private static class TransportReader implements StructReader<Transport> {
public Transport readStruct(Reader r) throws IOException {
r.readStructId(Types.TRANSPORT);
r.readStructId(TRANSPORT);
// Read the ID
byte[] b = r.readBytes(UniqueId.LENGTH);
if(b.length != UniqueId.LENGTH) throw new FormatException();

View File

@@ -1,5 +1,7 @@
package net.sf.briar.db;
import static java.sql.Types.BINARY;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -7,7 +9,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -119,7 +120,7 @@ public class BasicH2Test extends BriarTestCase {
String sql = "INSERT INTO foo (uniqueId, name) VALUES (?, ?)";
try {
PreparedStatement ps = connection.prepareStatement(sql);
if(id == null) ps.setNull(1, Types.BINARY);
if(id == null) ps.setNull(1, BINARY);
else ps.setBytes(1, id);
ps.setString(2, name);
int rowsAffected = ps.executeUpdate();

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.ACK;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -11,7 +12,6 @@ import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Ack;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.ReaderFactory;
import net.sf.briar.api.serial.SerialComponent;
@@ -52,10 +52,10 @@ public class AckReaderTest extends BriarTestCase {
byte[] b = createAck(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.ACK, ackReader);
reader.addStructReader(ACK, ackReader);
try {
reader.readStruct(Types.ACK, Ack.class);
reader.readStruct(ACK, Ack.class);
fail();
} catch(FormatException expected) {}
context.assertIsSatisfied();
@@ -75,9 +75,9 @@ public class AckReaderTest extends BriarTestCase {
byte[] b = createAck(false);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.ACK, ackReader);
reader.addStructReader(ACK, ackReader);
assertEquals(ack, reader.readStruct(Types.ACK, Ack.class));
assertEquals(ack, reader.readStruct(ACK, Ack.class));
context.assertIsSatisfied();
}
@@ -89,10 +89,10 @@ public class AckReaderTest extends BriarTestCase {
byte[] b = createEmptyAck();
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.ACK, ackReader);
reader.addStructReader(ACK, ackReader);
try {
reader.readStruct(Types.ACK, Ack.class);
reader.readStruct(ACK, Ack.class);
fail();
} catch(FormatException expected) {}
context.assertIsSatisfied();
@@ -101,7 +101,7 @@ public class AckReaderTest extends BriarTestCase {
private byte[] createAck(boolean tooBig) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.ACK);
w.writeStructId(ACK);
w.writeListStart();
while(out.size() + serial.getSerialisedUniqueIdLength()
< MAX_PACKET_LENGTH) {
@@ -116,7 +116,7 @@ public class AckReaderTest extends BriarTestCase {
private byte[] createEmptyAck() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.ACK);
w.writeStructId(ACK);
w.writeListStart();
w.writeListEnd();
return out.toByteArray();

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.OFFER;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -11,7 +12,6 @@ import net.sf.briar.TestUtils;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.Offer;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.ReaderFactory;
import net.sf.briar.api.serial.SerialComponent;
@@ -52,10 +52,10 @@ public class OfferReaderTest extends BriarTestCase {
byte[] b = createOffer(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.OFFER, offerReader);
reader.addStructReader(OFFER, offerReader);
try {
reader.readStruct(Types.OFFER, Offer.class);
reader.readStruct(OFFER, Offer.class);
fail();
} catch(FormatException expected) {}
context.assertIsSatisfied();
@@ -75,9 +75,9 @@ public class OfferReaderTest extends BriarTestCase {
byte[] b = createOffer(false);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.OFFER, offerReader);
reader.addStructReader(OFFER, offerReader);
assertEquals(offer, reader.readStruct(Types.OFFER, Offer.class));
assertEquals(offer, reader.readStruct(OFFER, Offer.class));
context.assertIsSatisfied();
}
@@ -89,10 +89,10 @@ public class OfferReaderTest extends BriarTestCase {
byte[] b = createEmptyOffer();
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.OFFER, offerReader);
reader.addStructReader(OFFER, offerReader);
try {
reader.readStruct(Types.OFFER, Offer.class);
reader.readStruct(OFFER, Offer.class);
fail();
} catch(FormatException expected) {}
context.assertIsSatisfied();
@@ -101,7 +101,7 @@ public class OfferReaderTest extends BriarTestCase {
private byte[] createOffer(boolean tooBig) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.OFFER);
w.writeStructId(OFFER);
w.writeListStart();
while(out.size() + serial.getSerialisedUniqueIdLength()
< MAX_PACKET_LENGTH) {
@@ -116,7 +116,7 @@ public class OfferReaderTest extends BriarTestCase {
private byte[] createEmptyOffer() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.OFFER);
w.writeStructId(OFFER);
w.writeListStart();
w.writeListEnd();
return out.toByteArray();

View File

@@ -1,6 +1,7 @@
package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.Types.REQUEST;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -10,7 +11,6 @@ import net.sf.briar.BriarTestCase;
import net.sf.briar.api.FormatException;
import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.Request;
import net.sf.briar.api.protocol.Types;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.ReaderFactory;
import net.sf.briar.api.serial.Writer;
@@ -53,10 +53,10 @@ public class RequestReaderTest extends BriarTestCase {
byte[] b = createRequest(true);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.REQUEST, requestReader);
reader.addStructReader(REQUEST, requestReader);
try {
reader.readStruct(Types.REQUEST, Request.class);
reader.readStruct(REQUEST, Request.class);
fail();
} catch(FormatException expected) {}
context.assertIsSatisfied();
@@ -76,10 +76,9 @@ public class RequestReaderTest extends BriarTestCase {
byte[] b = createRequest(false);
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
reader.addStructReader(Types.REQUEST, requestReader);
reader.addStructReader(REQUEST, requestReader);
assertEquals(request, reader.readStruct(Types.REQUEST,
Request.class));
assertEquals(request, reader.readStruct(REQUEST, Request.class));
context.assertIsSatisfied();
}
@@ -106,8 +105,8 @@ public class RequestReaderTest extends BriarTestCase {
ByteArrayInputStream in = new ByteArrayInputStream(b);
Reader reader = readerFactory.createReader(in);
RequestReader requestReader = new RequestReader(packetFactory);
reader.addStructReader(Types.REQUEST, requestReader);
Request r = reader.readStruct(Types.REQUEST, Request.class);
reader.addStructReader(REQUEST, requestReader);
Request r = reader.readStruct(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
@@ -123,7 +122,7 @@ public class RequestReaderTest extends BriarTestCase {
private byte[] createRequest(boolean tooBig) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.REQUEST);
w.writeStructId(REQUEST);
// Allow one byte for the REQUEST tag, one byte for the padding length
// as a uint7, one byte for the BYTES tag, and five bytes for the
// length of the byte array as an int32
@@ -139,7 +138,7 @@ public class RequestReaderTest extends BriarTestCase {
private byte[] createRequest(byte[] bitmap) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Writer w = writerFactory.createWriter(out);
w.writeStructId(Types.REQUEST);
w.writeStructId(REQUEST);
w.writeUint7((byte) 0);
w.writeBytes(bitmap);
return out.toByteArray();