mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Removed writer's count of bytes written and renamed a method.
This commit is contained in:
@@ -13,7 +13,7 @@ public class AuthorId extends UniqueId {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.AUTHOR_ID);
|
||||
w.writeUserDefinedId(Types.AUTHOR_ID);
|
||||
w.writeBytes(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class BatchId extends UniqueId {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.BATCH_ID);
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
w.writeBytes(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class GroupId extends UniqueId {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.GROUP_ID);
|
||||
w.writeUserDefinedId(Types.GROUP_ID);
|
||||
w.writeBytes(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class MessageId extends UniqueId {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.MESSAGE_ID);
|
||||
w.writeUserDefinedId(Types.MESSAGE_ID);
|
||||
w.writeBytes(id);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ import java.util.Map;
|
||||
|
||||
public interface Writer {
|
||||
|
||||
long getBytesWritten();
|
||||
|
||||
void writeBoolean(boolean b) throws IOException;
|
||||
|
||||
void writeUint7(byte b) throws IOException;
|
||||
@@ -33,5 +31,5 @@ public interface Writer {
|
||||
|
||||
void writeNull() throws IOException;
|
||||
|
||||
void writeUserDefinedTag(int tag) throws IOException;
|
||||
void writeUserDefinedId(int tag) throws IOException;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class AuthorImpl implements Author {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.AUTHOR);
|
||||
w.writeUserDefinedId(Types.AUTHOR);
|
||||
w.writeString(name);
|
||||
w.writeBytes(publicKey);
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class GroupImpl implements Group {
|
||||
}
|
||||
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(Types.GROUP);
|
||||
w.writeUserDefinedId(Types.GROUP);
|
||||
w.writeString(name);
|
||||
if(publicKey == null) w.writeNull();
|
||||
else w.writeBytes(publicKey);
|
||||
|
||||
@@ -74,7 +74,7 @@ class MessageEncoderImpl implements MessageEncoder {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
// Write the message
|
||||
w.writeUserDefinedTag(Types.MESSAGE);
|
||||
w.writeUserDefinedId(Types.MESSAGE);
|
||||
if(parent == null) w.writeNull();
|
||||
else parent.writeTo(w);
|
||||
if(group == null) w.writeNull();
|
||||
|
||||
@@ -55,7 +55,7 @@ class AckWriterImpl implements AckWriter {
|
||||
}
|
||||
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedTag(Types.ACK);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeListStart();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
|
||||
@@ -61,7 +61,7 @@ class BatchWriterImpl implements BatchWriter {
|
||||
|
||||
private void start() throws IOException {
|
||||
messageDigest.reset();
|
||||
w.writeUserDefinedTag(Types.BATCH);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
capacity -= headerLength;
|
||||
started = true;
|
||||
|
||||
@@ -55,7 +55,7 @@ class OfferWriterImpl implements OfferWriter {
|
||||
}
|
||||
|
||||
private void start() throws IOException {
|
||||
w.writeUserDefinedTag(Types.OFFER);
|
||||
w.writeUserDefinedId(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.writeUserDefinedTag(Types.REQUEST);
|
||||
w.writeUserDefinedId(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];
|
||||
|
||||
@@ -22,7 +22,7 @@ class SubscriptionWriterImpl implements SubscriptionWriter {
|
||||
|
||||
public void writeSubscriptions(Map<Group, Long> subs, long timestamp)
|
||||
throws IOException {
|
||||
w.writeUserDefinedTag(Types.SUBSCRIPTION_UPDATE);
|
||||
w.writeUserDefinedId(Types.SUBSCRIPTION_UPDATE);
|
||||
w.writeMap(subs);
|
||||
w.writeInt64(timestamp);
|
||||
out.flush();
|
||||
|
||||
@@ -22,10 +22,10 @@ class TransportWriterImpl implements TransportWriter {
|
||||
|
||||
public void writeTransports(Map<String, Map<String, String>> transports,
|
||||
long timestamp) throws IOException {
|
||||
w.writeUserDefinedTag(Types.TRANSPORT_UPDATE);
|
||||
w.writeUserDefinedId(Types.TRANSPORT_UPDATE);
|
||||
w.writeListStart();
|
||||
for(Entry<String, Map<String, String>> e : transports.entrySet()) {
|
||||
w.writeUserDefinedTag(Types.TRANSPORT_PROPERTIES);
|
||||
w.writeUserDefinedId(Types.TRANSPORT_PROPERTIES);
|
||||
w.writeString(e.getKey());
|
||||
w.writeMap(e.getValue());
|
||||
}
|
||||
|
||||
@@ -14,45 +14,35 @@ import net.sf.briar.api.serial.Writer;
|
||||
class WriterImpl implements Writer {
|
||||
|
||||
private final OutputStream out;
|
||||
private long bytesWritten = 0L;
|
||||
|
||||
WriterImpl(OutputStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
public long getBytesWritten() {
|
||||
return bytesWritten;
|
||||
}
|
||||
|
||||
public void writeBoolean(boolean b) throws IOException {
|
||||
if(b) out.write(Tag.TRUE);
|
||||
else out.write(Tag.FALSE);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeUint7(byte b) throws IOException {
|
||||
if(b < 0) throw new IllegalArgumentException();
|
||||
out.write(b);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeInt8(byte b) throws IOException {
|
||||
out.write(Tag.INT8);
|
||||
out.write(b);
|
||||
bytesWritten += 2;
|
||||
}
|
||||
|
||||
public void writeInt16(short s) throws IOException {
|
||||
out.write(Tag.INT16);
|
||||
out.write((byte) (s >> 8));
|
||||
out.write((byte) ((s << 8) >> 8));
|
||||
bytesWritten += 3;
|
||||
}
|
||||
|
||||
public void writeInt32(int i) throws IOException {
|
||||
out.write(Tag.INT32);
|
||||
writeInt32Bits(i);
|
||||
bytesWritten += 5;
|
||||
}
|
||||
|
||||
private void writeInt32Bits(int i) throws IOException {
|
||||
@@ -65,7 +55,6 @@ class WriterImpl implements Writer {
|
||||
public void writeInt64(long l) throws IOException {
|
||||
out.write(Tag.INT64);
|
||||
writeInt64Bits(l);
|
||||
bytesWritten += 9;
|
||||
}
|
||||
|
||||
private void writeInt64Bits(long l) throws IOException {
|
||||
@@ -94,13 +83,11 @@ class WriterImpl implements Writer {
|
||||
public void writeFloat32(float f) throws IOException {
|
||||
out.write(Tag.FLOAT32);
|
||||
writeInt32Bits(Float.floatToRawIntBits(f));
|
||||
bytesWritten += 5;
|
||||
}
|
||||
|
||||
public void writeFloat64(double d) throws IOException {
|
||||
out.write(Tag.FLOAT64);
|
||||
writeInt64Bits(Double.doubleToRawLongBits(d));
|
||||
bytesWritten += 9;
|
||||
}
|
||||
|
||||
public void writeString(String s) throws IOException {
|
||||
@@ -111,7 +98,6 @@ class WriterImpl implements Writer {
|
||||
writeLength(b.length);
|
||||
}
|
||||
out.write(b);
|
||||
bytesWritten += b.length + 1;
|
||||
}
|
||||
|
||||
private void writeLength(int i) throws IOException {
|
||||
@@ -129,7 +115,6 @@ class WriterImpl implements Writer {
|
||||
writeLength(b.length);
|
||||
}
|
||||
out.write(b);
|
||||
bytesWritten += b.length + 1;
|
||||
}
|
||||
|
||||
public void writeList(Collection<?> c) throws IOException {
|
||||
@@ -140,7 +125,6 @@ class WriterImpl implements Writer {
|
||||
writeLength(length);
|
||||
}
|
||||
for(Object o : c) writeObject(o);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
private void writeObject(Object o) throws IOException {
|
||||
@@ -162,12 +146,10 @@ class WriterImpl implements Writer {
|
||||
|
||||
public void writeListStart() throws IOException {
|
||||
out.write(Tag.LIST_START);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeListEnd() throws IOException {
|
||||
out.write(Tag.END);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeMap(Map<?, ?> m) throws IOException {
|
||||
@@ -181,33 +163,27 @@ class WriterImpl implements Writer {
|
||||
writeObject(e.getKey());
|
||||
writeObject(e.getValue());
|
||||
}
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeMapStart() throws IOException {
|
||||
out.write(Tag.MAP_START);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeMapEnd() throws IOException {
|
||||
out.write(Tag.END);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeNull() throws IOException {
|
||||
out.write(Tag.NULL);
|
||||
bytesWritten++;
|
||||
}
|
||||
|
||||
public void writeUserDefinedTag(int tag) throws IOException {
|
||||
if(tag < 0 || tag > 255) throw new IllegalArgumentException();
|
||||
if(tag < 32) {
|
||||
out.write((byte) (Tag.SHORT_USER | tag));
|
||||
bytesWritten++;
|
||||
public void writeUserDefinedId(int id) throws IOException {
|
||||
if(id < 0 || id > 255) throw new IllegalArgumentException();
|
||||
if(id < 32) {
|
||||
out.write((byte) (Tag.SHORT_USER | id));
|
||||
} else {
|
||||
out.write(Tag.USER);
|
||||
out.write((byte) tag);
|
||||
bytesWritten += 2;
|
||||
out.write((byte) id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,18 +100,18 @@ public class AckReaderTest extends TestCase {
|
||||
private byte[] createAck(boolean tooBig) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedTag(Types.ACK);
|
||||
w.writeUserDefinedId(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.writeUserDefinedTag(Types.BATCH_ID);
|
||||
w.writeUserDefinedId(Types.BATCH_ID);
|
||||
random.nextBytes(b);
|
||||
w.writeBytes(b);
|
||||
}
|
||||
if(tooBig) {
|
||||
w.writeUserDefinedTag(Types.BATCH_ID);
|
||||
w.writeUserDefinedId(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.writeUserDefinedTag(Types.ACK);
|
||||
w.writeUserDefinedId(Types.ACK);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
|
||||
@@ -143,10 +143,10 @@ public class BatchReaderTest extends TestCase {
|
||||
private byte[] createBatch(int size) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
|
||||
Writer w = writerFactory.createWriter(out);
|
||||
w.writeUserDefinedTag(Types.BATCH);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
// We're using a fake message reader, so it's OK to use a fake message
|
||||
w.writeUserDefinedTag(Types.MESSAGE);
|
||||
w.writeUserDefinedId(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.writeUserDefinedTag(Types.BATCH);
|
||||
w.writeUserDefinedId(Types.BATCH);
|
||||
w.writeListStart();
|
||||
w.writeListEnd();
|
||||
return out.toByteArray();
|
||||
|
||||
@@ -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.writeUserDefinedTag(Types.REQUEST);
|
||||
w.writeUserDefinedId(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.writeUserDefinedTag(Types.REQUEST);
|
||||
w.writeUserDefinedId(Types.REQUEST);
|
||||
w.writeBytes(bitmap);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -269,8 +269,8 @@ public class WriterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWriteShortUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedTag(0);
|
||||
w.writeUserDefinedTag(31);
|
||||
w.writeUserDefinedId(0);
|
||||
w.writeUserDefinedId(31);
|
||||
// SHORT_USER tag (3 bits), 0 (5 bits), SHORT_USER tag (3 bits),
|
||||
// 31 (5 bits)
|
||||
checkContents("C0" + "DF");
|
||||
@@ -278,8 +278,8 @@ public class WriterImplTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testWriteUserDefinedTag() throws IOException {
|
||||
w.writeUserDefinedTag(32);
|
||||
w.writeUserDefinedTag(255);
|
||||
w.writeUserDefinedId(32);
|
||||
w.writeUserDefinedId(255);
|
||||
// USER tag, 32 as uint8, USER tag, 255 as uint8
|
||||
checkContents("EF" + "20" + "EF" + "FF");
|
||||
}
|
||||
@@ -288,7 +288,7 @@ public class WriterImplTest extends TestCase {
|
||||
public void testWriteCollectionOfWritables() throws IOException {
|
||||
Writable writable = new Writable() {
|
||||
public void writeTo(Writer w) throws IOException {
|
||||
w.writeUserDefinedTag(0);
|
||||
w.writeUserDefinedId(0);
|
||||
w.writeString("foo");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user