Removed unnecessary Raw interface.

This commit is contained in:
akwizgran
2011-07-24 17:47:17 +01:00
parent 941460e3bc
commit c98c968b87
24 changed files with 80 additions and 115 deletions

View File

@@ -14,7 +14,7 @@ public class AuthorId extends UniqueId {
public void writeTo(Writer w) throws IOException {
w.writeUserDefinedTag(Tags.AUTHOR_ID);
w.writeRaw(id);
w.writeBytes(id);
}
@Override

View File

@@ -17,7 +17,7 @@ public class BatchId extends UniqueId {
public void writeTo(Writer w) throws IOException {
w.writeUserDefinedTag(Tags.BATCH_ID);
w.writeRaw(id);
w.writeBytes(id);
}
@Override

View File

@@ -17,7 +17,7 @@ public class GroupId extends UniqueId {
public void writeTo(Writer w) throws IOException {
w.writeUserDefinedTag(Tags.GROUP_ID);
w.writeRaw(id);
w.writeBytes(id);
}
@Override

View File

@@ -1,10 +1,8 @@
package net.sf.briar.api.protocol;
import net.sf.briar.api.serial.Raw;
public interface Message {
public interface Message extends Raw {
static final int MAX_SIZE = 1024 * 1023; // Not a typo
static final int MAX_SIZE = (1024 * 1024) - 200;
/** Returns the message's unique identifier. */
MessageId getId();
@@ -26,4 +24,7 @@ public interface Message extends Raw {
/** Returns the size of the message in bytes. */
int getSize();
/** Returns the serialised representation of the message. */
byte[] getBytes();
}

View File

@@ -20,7 +20,7 @@ public class MessageId extends UniqueId {
public void writeTo(Writer w) throws IOException {
w.writeUserDefinedTag(Tags.MESSAGE_ID);
w.writeRaw(id);
w.writeBytes(id);
}
@Override

View File

@@ -2,10 +2,9 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.Writable;
public abstract class UniqueId implements Raw, Writable {
public abstract class UniqueId implements Writable {
public static final int LENGTH = 32;
public static final int SERIALISED_LENGTH = LENGTH + 3;

View File

@@ -2,13 +2,12 @@ package net.sf.briar.api.serial;
import java.util.Arrays;
/** A byte array wrapped in the Raw interface. */
public class RawByteArray implements Raw {
/** A wrapper around a byte array, to allow it to be stored in maps etc. */
public class Bytes {
private final byte[] bytes;
public RawByteArray(byte[] bytes) {
public Bytes(byte[] bytes) {
this.bytes = bytes;
}
@@ -23,7 +22,8 @@ public class RawByteArray implements Raw {
@Override
public boolean equals(Object o) {
if(o instanceof Raw) return Arrays.equals(bytes, ((Raw) o).getBytes());
if(o instanceof Bytes)
return Arrays.equals(bytes, ((Bytes) o).bytes);
return false;
}
}

View File

@@ -1,10 +0,0 @@
package net.sf.briar.api.serial;
/**
* Generic interface for any object that knows how to serialise itself as a
* raw byte array.
*/
public interface Raw {
byte[] getBytes();
}

View File

@@ -38,8 +38,8 @@ public interface Reader {
boolean hasString() throws IOException;
String readString() throws IOException;
boolean hasRaw() throws IOException;
byte[] readRaw() throws IOException;
boolean hasBytes() throws IOException;
byte[] readBytes() throws IOException;
boolean hasList() throws IOException;
List<Object> readList() throws IOException;

View File

@@ -11,7 +11,7 @@ public interface Tag {
static final byte FLOAT32 = -7; // 1111 1001
static final byte FLOAT64 = -8; // 1111 1000
static final byte STRING = -9; // 1111 0111
static final byte RAW = -10; // 1111 0110
static final byte BYTES = -10; // 1111 0110
static final byte LIST = -11; // 1111 0101
static final byte MAP = -12; // 1111 0100
static final byte LIST_START = -13; // 1111 0011
@@ -22,7 +22,7 @@ public interface Tag {
static final int SHORT_MASK = 0xF0; // Match first four bits
static final int SHORT_STRING = 0x80; // 1000 xxxx
static final int SHORT_RAW = 0x90; // 1001 xxxx
static final int SHORT_BYTES = 0x90; // 1001 xxxx
static final int SHORT_LIST = 0xA0; // 1010 xxxx
static final int SHORT_MAP = 0xB0; // 1011 xxxx

View File

@@ -21,8 +21,7 @@ public interface Writer {
void writeFloat64(double d) throws IOException;
void writeString(String s) throws IOException;
void writeRaw(byte[] b) throws IOException;
void writeRaw(Raw r) throws IOException;
void writeBytes(byte[] b) throws IOException;
void writeList(Collection<?> c) throws IOException;
void writeListStart() throws IOException;

View File

@@ -13,7 +13,7 @@ class BatchIdReader implements ObjectReader<BatchId> {
public BatchId readObject(Reader r) throws IOException {
r.readUserDefinedTag(Tags.BATCH_ID);
byte[] b = r.readRaw();
byte[] b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
return new BatchId(b);
}

View File

@@ -13,7 +13,7 @@ class GroupIdReader implements ObjectReader<GroupId> {
public GroupId readObject(Reader r) throws IOException {
r.readUserDefinedTag(Tags.GROUP_ID);
byte[] b = r.readRaw();
byte[] b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
return new GroupId(b);
}

View File

@@ -53,8 +53,8 @@ class GroupImpl implements Group {
w.writeUserDefinedTag(Tags.GROUP);
w.writeString(name);
w.writeBoolean(isRestricted());
if(salt == null) w.writeRaw(publicKey.getEncoded());
else w.writeRaw(salt);
if(salt == null) w.writeBytes(publicKey.getEncoded());
else w.writeBytes(salt);
}
@Override

View File

@@ -29,7 +29,7 @@ class GroupReader implements ObjectReader<Group> {
r.readUserDefinedTag(Tags.GROUP);
String name = r.readString();
boolean restricted = r.readBoolean();
byte[] saltOrKey = r.readRaw();
byte[] saltOrKey = r.readBytes();
r.removeConsumer(digesting);
// Build and return the group
GroupId id = new GroupId(messageDigest.digest());

View File

@@ -44,8 +44,8 @@ class MessageEncoderImpl implements MessageEncoder {
group.writeTo(w);
w.writeInt64(timestamp);
w.writeString(nick);
w.writeRaw(keyPair.getPublic().getEncoded());
w.writeRaw(body);
w.writeBytes(keyPair.getPublic().getEncoded());
w.writeBytes(body);
// Sign the message
byte[] signable = out.toByteArray();
signature.initSign(keyPair.getPrivate());
@@ -53,7 +53,7 @@ class MessageEncoderImpl implements MessageEncoder {
byte[] sig = signature.sign();
signable = null;
// Write the signature
w.writeRaw(sig);
w.writeBytes(sig);
byte[] raw = out.toByteArray();
// The message ID is the hash of the entire message
messageDigest.reset();
@@ -63,7 +63,7 @@ class MessageEncoderImpl implements MessageEncoder {
out.reset();
w = writerFactory.createWriter(out);
w.writeString(nick);
w.writeRaw(keyPair.getPublic().getEncoded());
w.writeBytes(keyPair.getPublic().getEncoded());
messageDigest.reset();
messageDigest.update(out.toByteArray());
AuthorId authorId = new AuthorId(messageDigest.digest());

View File

@@ -41,12 +41,12 @@ class MessageReader implements ObjectReader<Message> {
r.readUserDefinedTag(Tags.MESSAGE);
// Read the parent's message ID
r.readUserDefinedTag(Tags.MESSAGE_ID);
byte[] b = r.readRaw();
byte[] b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
MessageId parent = new MessageId(b);
// Read the group ID
r.readUserDefinedTag(Tags.GROUP_ID);
b = r.readRaw();
b = r.readBytes();
if(b.length != UniqueId.LENGTH) throw new FormatException();
GroupId group = new GroupId(b);
// Read the timestamp
@@ -57,15 +57,15 @@ class MessageReader implements ObjectReader<Message> {
messageDigest.reset();
r.addConsumer(digesting);
r.readString();
byte[] encodedKey = r.readRaw();
byte[] encodedKey = r.readBytes();
r.removeConsumer(digesting);
AuthorId author = new AuthorId(messageDigest.digest());
// Skip the message body
r.readRaw();
r.readBytes();
// Record the length of the signed data
int messageLength = (int) counting.getCount();
// Read the signature
byte[] sig = r.readRaw();
byte[] sig = r.readBytes();
r.removeConsumer(counting);
r.removeConsumer(copying);
// Verify the signature

View File

@@ -7,10 +7,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.briar.api.serial.Bytes;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.api.serial.Tag;
@@ -309,25 +309,25 @@ class ReaderImpl implements Reader {
|| next == Tag.INT32;
}
public boolean hasRaw() throws IOException {
public boolean hasBytes() throws IOException {
if(!started) readNext(true);
if(eof) return false;
return next == Tag.RAW || (next & Tag.SHORT_MASK) == Tag.SHORT_RAW;
return next == Tag.BYTES || (next & Tag.SHORT_MASK) == Tag.SHORT_BYTES;
}
public byte[] readRaw() throws IOException {
if(!hasRaw()) throw new FormatException();
if(next == Tag.RAW) {
public byte[] readBytes() throws IOException {
if(!hasBytes()) throw new FormatException();
if(next == Tag.BYTES) {
readNext(false);
return readRaw(readLength());
return readBytes(readLength());
} else {
int length = 0xFF & next ^ Tag.SHORT_RAW;
int length = 0xFF & next ^ Tag.SHORT_BYTES;
readNext(length == 0);
return readRaw(length);
return readBytes(length);
}
}
private byte[] readRaw(int length) throws IOException {
private byte[] readBytes(int length) throws IOException {
assert length >= 0;
if(length == 0) return EMPTY_BUFFER;
byte[] b = new byte[length];
@@ -395,7 +395,7 @@ class ReaderImpl implements Reader {
if(hasFloat32()) return Float.valueOf(readFloat32());
if(hasFloat64()) return Double.valueOf(readFloat64());
if(hasString()) return readString();
if(hasRaw()) return new RawByteArray(readRaw());
if(hasBytes()) return new Bytes(readBytes());
if(hasList()) return readList();
if(hasMap()) return readMap();
if(hasNull()) {

View File

@@ -7,7 +7,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.Bytes;
import net.sf.briar.api.serial.Tag;
import net.sf.briar.api.serial.Writable;
import net.sf.briar.api.serial.Writer;
@@ -123,20 +123,16 @@ class WriterImpl implements Writer {
else writeInt32(i);
}
public void writeRaw(byte[] b) throws IOException {
if(b.length < 16) out.write((byte) (Tag.SHORT_RAW | b.length));
public void writeBytes(byte[] b) throws IOException {
if(b.length < 16) out.write((byte) (Tag.SHORT_BYTES | b.length));
else {
out.write(Tag.RAW);
out.write(Tag.BYTES);
writeLength(b.length);
}
out.write(b);
bytesWritten += b.length + 1;
}
public void writeRaw(Raw r) throws IOException {
writeRaw(r.getBytes());
}
public void writeList(Collection<?> c) throws IOException {
int length = c.size();
if(length < 16) out.write((byte) (Tag.SHORT_LIST | length));
@@ -158,7 +154,7 @@ class WriterImpl implements Writer {
else if(o instanceof Float) writeFloat32((Float) o);
else if(o instanceof Double) writeFloat64((Double) o);
else if(o instanceof String) writeString((String) o);
else if(o instanceof Raw) writeRaw((Raw) o);
else if(o instanceof Bytes) writeBytes(((Bytes) o).getBytes());
else if(o instanceof List) writeList((List<?>) o);
else if(o instanceof Map) writeMap((Map<?, ?>) o);
else if(o == null) writeNull();

View File

@@ -106,12 +106,12 @@ public class AckReaderTest extends TestCase {
while(out.size() < Ack.MAX_SIZE - BatchId.SERIALISED_LENGTH) {
w.writeUserDefinedTag(Tags.BATCH_ID);
random.nextBytes(b);
w.writeRaw(b);
w.writeBytes(b);
}
if(tooBig) {
w.writeUserDefinedTag(Tags.BATCH_ID);
random.nextBytes(b);
w.writeRaw(b);
w.writeBytes(b);
}
w.writeListEnd();
assertEquals(tooBig, out.size() > Ack.MAX_SIZE);

View File

@@ -150,7 +150,7 @@ public class BatchReaderTest extends TestCase {
w.writeListStart();
// We're using a fake message reader, so it's OK to use a fake message
w.writeUserDefinedTag(Tags.MESSAGE);
w.writeRaw(new byte[size - 10]);
w.writeBytes(new byte[size - 10]);
w.writeListEnd();
byte[] b = out.toByteArray();
assertEquals(size, b.length);
@@ -170,7 +170,7 @@ public class BatchReaderTest extends TestCase {
public Message readObject(Reader r) throws IOException {
r.readUserDefinedTag(Tags.MESSAGE);
r.readRaw();
r.readBytes();
return message;
}
}

View File

@@ -12,8 +12,7 @@ import junit.framework.TestCase;
import net.sf.briar.api.serial.Consumer;
import net.sf.briar.api.serial.FormatException;
import net.sf.briar.api.serial.ObjectReader;
import net.sf.briar.api.serial.Raw;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.api.serial.Bytes;
import net.sf.briar.api.serial.Reader;
import net.sf.briar.util.StringUtils;
@@ -134,12 +133,12 @@ public class ReaderImplTest extends TestCase {
}
@Test
public void testReadRaw() throws Exception {
public void testReadBytes() throws Exception {
setContents("F603010203" + "93010203" + "F600" + "90");
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readRaw()));
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readRaw()));
assertTrue(Arrays.equals(new byte[] {}, r.readRaw()));
assertTrue(Arrays.equals(new byte[] {}, r.readRaw()));
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readBytes()));
assertTrue(Arrays.equals(new byte[] {1, 2, 3}, r.readBytes()));
assertTrue(Arrays.equals(new byte[] {}, r.readBytes()));
assertTrue(Arrays.equals(new byte[] {}, r.readBytes()));
assertTrue(r.eof());
}
@@ -197,9 +196,9 @@ public class ReaderImplTest extends TestCase {
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Raw raw = new RawByteArray(new byte[] {});
assertTrue(m.containsKey(raw));
assertNull(m.get(raw));
Bytes b = new Bytes(new byte[] {});
assertTrue(m.containsKey(b));
assertNull(m.get(b));
assertTrue(r.eof());
}
@@ -210,9 +209,9 @@ public class ReaderImplTest extends TestCase {
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Raw raw = new RawByteArray(new byte[] {});
assertTrue(m.containsKey(raw));
assertNull(m.get(raw));
Bytes b = new Bytes(new byte[] {});
assertTrue(m.containsKey(b));
assertNull(m.get(b));
assertTrue(r.eof());
}
@@ -275,9 +274,9 @@ public class ReaderImplTest extends TestCase {
assertNotNull(m);
assertEquals(2, m.size());
assertEquals((byte) 123, m.get("foo"));
Raw raw = new RawByteArray(new byte[] {});
assertTrue(m.containsKey(raw));
assertNull(m.get(raw));
Bytes b = new Bytes(new byte[] {});
assertTrue(m.containsKey(b));
assertNull(m.get(b));
assertTrue(r.eof());
}
@@ -291,7 +290,7 @@ public class ReaderImplTest extends TestCase {
assertFalse(r.hasMapEnd());
assertEquals((byte) 123, r.readIntAny());
assertFalse(r.hasMapEnd());
assertTrue(Arrays.equals(new byte[] {}, r.readRaw()));
assertTrue(Arrays.equals(new byte[] {}, r.readBytes()));
assertFalse(r.hasMapEnd());
assertTrue(r.hasNull());
r.readNull();

View File

@@ -10,7 +10,6 @@ import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.briar.api.serial.RawByteArray;
import net.sf.briar.api.serial.Writable;
import net.sf.briar.api.serial.Writer;
import net.sf.briar.util.StringUtils;
@@ -152,38 +151,20 @@ public class WriterImplTest extends TestCase {
}
@Test
public void testWriteShortRawBytes() throws IOException {
w.writeRaw(new byte[] {
public void testWriteShortBytes() throws IOException {
w.writeBytes(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
});
// SHORT_RAW tag, length 15, raw bytes
// SHORT_BYTES tag, length 15, bytes
checkContents("9" + "F" + "000102030405060708090A0B0C0D0E");
}
@Test
public void testWriteShortRawObject() throws IOException {
w.writeRaw(new RawByteArray(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
}));
// SHORT_RAW tag, length 15, raw bytes
checkContents("9" + "F" + "000102030405060708090A0B0C0D0E");
}
@Test
public void testWriteRawBytes() throws IOException {
w.writeRaw(new byte[] {
public void testWriteBytes() throws IOException {
w.writeBytes(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
});
// RAW tag, length 16 as uint7, raw bytes
checkContents("F6" + "10" + "000102030405060708090A0B0C0D0E0F");
}
@Test
public void testWriteRawObject() throws IOException {
w.writeRaw(new RawByteArray(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}));
// RAW tag, length 16 as uint7, raw bytes
// BYTES tag, length 16 as uint7, bytes
checkContents("F6" + "10" + "000102030405060708090A0B0C0D0E0F");
}
@@ -257,11 +238,11 @@ public class WriterImplTest extends TestCase {
w.writeMapStart();
w.writeString("foo"); // Written as short string
w.writeIntAny(123); // Written as a uint7
w.writeRaw(new byte[] {}); // Written as short raw
w.writeBytes(new byte[] {}); // Written as short bytes
w.writeNull();
w.writeMapEnd();
// MAP_START tag, "foo" as short string, 123 as uint7,
// byte[] {} as short raw, NULL tag, END tag
// byte[] {} as short bytes, NULL tag, END tag
checkContents("F2" + "83666F6F" + "7B" + "90" + "F0" + "F1");
}

View File

@@ -25,7 +25,7 @@ public class StringUtils {
else return s;
}
/** Converts the given raw byte array to a hex string. */
/** Converts the given byte array to a hex string. */
public static String toHexString(byte[] bytes) {
StringBuilder s = new StringBuilder(bytes.length * 2);
for(byte b : bytes) {
@@ -37,7 +37,7 @@ public class StringUtils {
return s.toString();
}
/** Converts the given hex string to a raw byte array. */
/** Converts the given hex string to a byte array. */
public static byte[] fromHexString(String hex) {
int len = hex.length();
if(len % 2 != 0) throw new IllegalArgumentException("Not a hex string");