mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Removed the serialisation reader's internal array of struct readers.
This commit is contained in:
@@ -18,9 +18,6 @@ public interface Reader {
|
|||||||
void addConsumer(Consumer c);
|
void addConsumer(Consumer c);
|
||||||
void removeConsumer(Consumer c);
|
void removeConsumer(Consumer c);
|
||||||
|
|
||||||
void addStructReader(int id, StructReader<?> r);
|
|
||||||
void removeStructReader(int id);
|
|
||||||
|
|
||||||
boolean hasBoolean() throws IOException;
|
boolean hasBoolean() throws IOException;
|
||||||
boolean readBoolean() throws IOException;
|
boolean readBoolean() throws IOException;
|
||||||
|
|
||||||
@@ -68,6 +65,5 @@ public interface Reader {
|
|||||||
void readNull() throws IOException;
|
void readNull() throws IOException;
|
||||||
|
|
||||||
boolean hasStruct(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;
|
void readStructId(int id) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package net.sf.briar.protocol;
|
package net.sf.briar.protocol;
|
||||||
|
|
||||||
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
|
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 static net.sf.briar.api.protocol.Types.SUBSCRIPTION_UPDATE;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -29,9 +29,10 @@ class SubscriptionUpdateReader implements StructReader<SubscriptionUpdate> {
|
|||||||
r.addConsumer(counting);
|
r.addConsumer(counting);
|
||||||
r.readStructId(SUBSCRIPTION_UPDATE);
|
r.readStructId(SUBSCRIPTION_UPDATE);
|
||||||
// Read the subscriptions
|
// Read the subscriptions
|
||||||
r.addStructReader(GROUP, groupReader);
|
List<Group> subs = new ArrayList<Group>();
|
||||||
List<Group> subs = r.readList(Group.class);
|
r.readListStart();
|
||||||
r.removeStructReader(GROUP);
|
while(!r.hasListEnd()) subs.add(groupReader.readStruct(r));
|
||||||
|
r.readListEnd();
|
||||||
// Read the version number
|
// Read the version number
|
||||||
long version = r.readInt64();
|
long version = r.readInt64();
|
||||||
if(version < 0L) throw new FormatException();
|
if(version < 0L) throw new FormatException();
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import java.util.Map;
|
|||||||
import net.sf.briar.api.Bytes;
|
import net.sf.briar.api.Bytes;
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
import net.sf.briar.api.serial.Consumer;
|
import net.sf.briar.api.serial.Consumer;
|
||||||
import net.sf.briar.api.serial.StructReader;
|
|
||||||
import net.sf.briar.api.serial.Reader;
|
import net.sf.briar.api.serial.Reader;
|
||||||
|
|
||||||
// This class is not thread-safe
|
// This class is not thread-safe
|
||||||
@@ -23,7 +22,6 @@ class ReaderImpl implements Reader {
|
|||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
|
private final Collection<Consumer> consumers = new ArrayList<Consumer>(0);
|
||||||
|
|
||||||
private StructReader<?>[] structReaders = new StructReader<?>[] {};
|
|
||||||
private boolean hasLookahead = false, eof = false;
|
private boolean hasLookahead = false, eof = false;
|
||||||
private byte next, nextNext;
|
private byte next, nextNext;
|
||||||
private byte[] buf = null;
|
private byte[] buf = null;
|
||||||
@@ -98,24 +96,6 @@ class ReaderImpl implements Reader {
|
|||||||
if(!consumers.remove(c)) throw new IllegalArgumentException();
|
if(!consumers.remove(c)) throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addStructReader(int id, StructReader<?> r) {
|
|
||||||
if(id < 0 || id > 255) throw new IllegalArgumentException();
|
|
||||||
if(structReaders.length < id + 1) {
|
|
||||||
int len = Math.min(256, Math.max(id + 1, structReaders.length * 2));
|
|
||||||
StructReader<?>[] newStructReaders = new StructReader<?>[len];
|
|
||||||
System.arraycopy(structReaders, 0, newStructReaders, 0,
|
|
||||||
structReaders.length);
|
|
||||||
structReaders = newStructReaders;
|
|
||||||
}
|
|
||||||
structReaders[id] = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeStructReader(int id) {
|
|
||||||
if(id < 0 || id > structReaders.length)
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
structReaders[id] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasBoolean() throws IOException {
|
public boolean hasBoolean() throws IOException {
|
||||||
if(!hasLookahead) readLookahead(true);
|
if(!hasLookahead) readLookahead(true);
|
||||||
if(eof) return false;
|
if(eof) return false;
|
||||||
@@ -371,7 +351,6 @@ class ReaderImpl implements Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object readObject() throws IOException {
|
private Object readObject() throws IOException {
|
||||||
if(hasStruct()) return readStruct();
|
|
||||||
if(hasBoolean()) return Boolean.valueOf(readBoolean());
|
if(hasBoolean()) return Boolean.valueOf(readBoolean());
|
||||||
if(hasUint7()) return Byte.valueOf(readUint7());
|
if(hasUint7()) return Byte.valueOf(readUint7());
|
||||||
if(hasInt8()) return Byte.valueOf(readInt8());
|
if(hasInt8()) return Byte.valueOf(readInt8());
|
||||||
@@ -391,21 +370,6 @@ class ReaderImpl implements Reader {
|
|||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasStruct() throws IOException {
|
|
||||||
if(!hasLookahead) readLookahead(true);
|
|
||||||
if(eof) return false;
|
|
||||||
return next == Tag.STRUCT
|
|
||||||
|| (next & Tag.SHORT_STRUCT_MASK) == Tag.SHORT_STRUCT;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
private <T> T readObject(Class<T> t) throws IOException {
|
||||||
try {
|
try {
|
||||||
Object o = readObject();
|
Object o = readObject();
|
||||||
@@ -529,18 +493,6 @@ class ReaderImpl implements Reader {
|
|||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T readStruct(int id, Class<T> t) throws IOException {
|
|
||||||
if(!hasStruct(id)) throw new FormatException();
|
|
||||||
if(id < 0 || id >= structReaders.length) throw new FormatException();
|
|
||||||
StructReader<?> s = structReaders[id];
|
|
||||||
if(s == null) throw new FormatException();
|
|
||||||
try {
|
|
||||||
return t.cast(s.readStruct(this));
|
|
||||||
} catch(ClassCastException e) {
|
|
||||||
throw new FormatException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void readStructId(int id) throws IOException {
|
public void readStructId(int id) throws IOException {
|
||||||
if(!hasStruct(id)) throw new FormatException();
|
if(!hasStruct(id)) throw new FormatException();
|
||||||
consumeLookahead();
|
consumeLookahead();
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package net.sf.briar.serial;
|
|||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -13,9 +11,6 @@ import java.util.Map.Entry;
|
|||||||
import net.sf.briar.BriarTestCase;
|
import net.sf.briar.BriarTestCase;
|
||||||
import net.sf.briar.api.Bytes;
|
import net.sf.briar.api.Bytes;
|
||||||
import net.sf.briar.api.FormatException;
|
import net.sf.briar.api.FormatException;
|
||||||
import net.sf.briar.api.serial.Consumer;
|
|
||||||
import net.sf.briar.api.serial.StructReader;
|
|
||||||
import net.sf.briar.api.serial.Reader;
|
|
||||||
import net.sf.briar.util.StringUtils;
|
import net.sf.briar.util.StringUtils;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -370,135 +365,6 @@ public class ReaderImplTest extends BriarTestCase {
|
|||||||
assertTrue(r.eof());
|
assertTrue(r.eof());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReadStruct() throws Exception {
|
|
||||||
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
|
|
||||||
// Add readers for two structs
|
|
||||||
r.addStructReader(0, new StructReader<Foo>() {
|
|
||||||
public Foo readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(0);
|
|
||||||
return new Foo(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
r.addStructReader(255, new StructReader<Bar>() {
|
|
||||||
public Bar readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(255);
|
|
||||||
return new Bar(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 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 testReadStructWithConsumer() throws Exception {
|
|
||||||
setContents("C0" + "83666F6F" + "F1" + "FF" + "83666F6F");
|
|
||||||
// Add readers for two structs
|
|
||||||
r.addStructReader(0, new StructReader<Foo>() {
|
|
||||||
public Foo readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(0);
|
|
||||||
return new Foo(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
r.addStructReader(255, new StructReader<Bar>() {
|
|
||||||
public Bar readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(255);
|
|
||||||
return new Bar(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Add a consumer
|
|
||||||
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
r.addConsumer(new Consumer() {
|
|
||||||
|
|
||||||
public void write(byte b) throws IOException {
|
|
||||||
out.write(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
|
||||||
out.write(b, off, len);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 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" + "F1" + "FF" + "83666F6F",
|
|
||||||
StringUtils.toHexString(out.toByteArray()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnknownStructIdThrowsFormatException() throws Exception {
|
|
||||||
setContents("C0" + "83666F6F");
|
|
||||||
assertTrue(r.hasStruct(0));
|
|
||||||
// No reader has been added for struct ID 0
|
|
||||||
try {
|
|
||||||
r.readStruct(0, Foo.class);
|
|
||||||
fail();
|
|
||||||
} catch(FormatException expected) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWrongClassThrowsFormatException() throws Exception {
|
|
||||||
setContents("C0" + "83666F6F");
|
|
||||||
// Add a reader for struct ID 0, class Foo
|
|
||||||
r.addStructReader(0, new StructReader<Foo>() {
|
|
||||||
public Foo readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(0);
|
|
||||||
return new Foo(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
assertTrue(r.hasStruct(0));
|
|
||||||
// Trying to read the struct as class Bar should throw a FormatException
|
|
||||||
try {
|
|
||||||
r.readStruct(0, Bar.class);
|
|
||||||
fail();
|
|
||||||
} catch(FormatException expected) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReadListUsingStructReader() throws Exception {
|
|
||||||
setContents("A" + "1" + "C0" + "83666F6F");
|
|
||||||
// Add a reader for a struct
|
|
||||||
r.addStructReader(0, new StructReader<Foo>() {
|
|
||||||
public Foo readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(0);
|
|
||||||
return new Foo(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Check that the reader is used for lists
|
|
||||||
List<Foo> l = r.readList(Foo.class);
|
|
||||||
assertEquals(1, l.size());
|
|
||||||
assertEquals("foo", l.get(0).s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReadMapUsingStructReader() throws Exception {
|
|
||||||
setContents("B" + "1" + "C0" + "83666F6F" + "C1" + "83626172");
|
|
||||||
// Add readers for two structs
|
|
||||||
r.addStructReader(0, new StructReader<Foo>() {
|
|
||||||
public Foo readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(0);
|
|
||||||
return new Foo(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
r.addStructReader(1, new StructReader<Bar>() {
|
|
||||||
public Bar readStruct(Reader r) throws IOException {
|
|
||||||
r.readStructId(1);
|
|
||||||
return new Bar(r.readString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Check that the readers are used for maps
|
|
||||||
Map<Foo, Bar> m = r.readMap(Foo.class, Bar.class);
|
|
||||||
assertEquals(1, m.size());
|
|
||||||
Entry<Foo, Bar> e = m.entrySet().iterator().next();
|
|
||||||
assertEquals("foo", e.getKey().s);
|
|
||||||
assertEquals("bar", e.getValue().s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxLengthAppliesInsideMap() throws Exception {
|
public void testMaxLengthAppliesInsideMap() throws Exception {
|
||||||
@@ -535,22 +401,4 @@ public class ReaderImplTest extends BriarTestCase {
|
|||||||
in = new ByteArrayInputStream(StringUtils.fromHexString(hex));
|
in = new ByteArrayInputStream(StringUtils.fromHexString(hex));
|
||||||
r = new ReaderImpl(in);
|
r = new ReaderImpl(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Foo {
|
|
||||||
|
|
||||||
private final String s;
|
|
||||||
|
|
||||||
private Foo(String s) {
|
|
||||||
this.s = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Bar {
|
|
||||||
|
|
||||||
private final String s;
|
|
||||||
|
|
||||||
private Bar(String s) {
|
|
||||||
this.s = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user