mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Added start/end methods for writing indefinite lists and maps.
This commit is contained in:
@@ -22,11 +22,13 @@ public interface Writer {
|
|||||||
void writeRaw(byte[] b) throws IOException;
|
void writeRaw(byte[] b) throws IOException;
|
||||||
void writeRaw(Raw r) throws IOException;
|
void writeRaw(Raw r) throws IOException;
|
||||||
|
|
||||||
void writeList(List<?> l, boolean definite) throws IOException;
|
|
||||||
void writeList(List<?> l) throws IOException;
|
void writeList(List<?> l) throws IOException;
|
||||||
|
void writeListStart() throws IOException;
|
||||||
|
void writeListEnd() throws IOException;
|
||||||
|
|
||||||
void writeMap(Map<?, ?> m, boolean definite) throws IOException;
|
|
||||||
void writeMap(Map<?, ?> m) throws IOException;
|
void writeMap(Map<?, ?> m) throws IOException;
|
||||||
|
void writeMapStart() throws IOException;
|
||||||
|
void writeMapEnd() throws IOException;
|
||||||
|
|
||||||
void writeNull() throws IOException;
|
void writeNull() throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,16 +106,10 @@ class WriterImpl implements Writer {
|
|||||||
writeRaw(r.getBytes());
|
writeRaw(r.getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeList(List<?> l, boolean definite) throws IOException {
|
public void writeList(List<?> l) throws IOException {
|
||||||
if(definite) {
|
out.write(Tag.LIST_DEF);
|
||||||
out.write(Tag.LIST_DEF);
|
writeIntAny(l.size());
|
||||||
writeIntAny(l.size());
|
for(Object o : l) writeObject(o);
|
||||||
for(Object o : l) writeObject(o);
|
|
||||||
} else {
|
|
||||||
out.write(Tag.LIST_INDEF);
|
|
||||||
for(Object o : l) writeObject(o);
|
|
||||||
out.write(Tag.END);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeObject(Object o) throws IOException {
|
private void writeObject(Object o) throws IOException {
|
||||||
@@ -134,30 +128,29 @@ class WriterImpl implements Writer {
|
|||||||
else throw new IllegalStateException();
|
else throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeList(List<?> l) throws IOException {
|
public void writeListStart() throws IOException {
|
||||||
writeList(l, true);
|
out.write(Tag.LIST_INDEF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeMap(Map<?, ?> m, boolean definite) throws IOException {
|
public void writeListEnd() throws IOException {
|
||||||
if(definite) {
|
out.write(Tag.END);
|
||||||
out.write(Tag.MAP_DEF);
|
|
||||||
writeIntAny(m.size());
|
|
||||||
for(Entry<?, ?> e : m.entrySet()) {
|
|
||||||
writeObject(e.getKey());
|
|
||||||
writeObject(e.getValue());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.write(Tag.MAP_INDEF);
|
|
||||||
for(Entry<?, ?> e : m.entrySet()) {
|
|
||||||
writeObject(e.getKey());
|
|
||||||
writeObject(e.getValue());
|
|
||||||
}
|
|
||||||
out.write(Tag.END);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeMap(Map<?, ?> m) throws IOException {
|
public void writeMap(Map<?, ?> m) throws IOException {
|
||||||
writeMap(m, true);
|
out.write(Tag.MAP_DEF);
|
||||||
|
writeIntAny(m.size());
|
||||||
|
for(Entry<?, ?> e : m.entrySet()) {
|
||||||
|
writeObject(e.getKey());
|
||||||
|
writeObject(e.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeMapStart() throws IOException {
|
||||||
|
out.write(Tag.MAP_INDEF);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeMapEnd() throws IOException {
|
||||||
|
out.write(Tag.END);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeNull() throws IOException {
|
public void writeNull() throws IOException {
|
||||||
|
|||||||
@@ -146,57 +146,53 @@ public class WriterImplTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
public void testWriteDefiniteList() throws IOException {
|
public void testWriteDefiniteList() throws IOException {
|
||||||
List l = new ArrayList();
|
List<Object> l = new ArrayList<Object>();
|
||||||
l.add(Byte.valueOf((byte) 1)); // Written as a uint7
|
l.add(Byte.valueOf((byte) 1)); // Written as a uint7
|
||||||
l.add("foo");
|
l.add("foo");
|
||||||
l.add(Long.valueOf(128L)); // Written as an int16
|
l.add(Long.valueOf(128L)); // Written as an int16
|
||||||
w.writeList(l, true);
|
w.writeList(l);
|
||||||
checkContents("F5" + "03" + "01" + "F703666F6F" + "FC0080");
|
checkContents("F5" + "03" + "01" + "F703666F6F" + "FC0080");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
public void testWriteDefiniteMap() throws IOException {
|
public void testWriteDefiniteMap() throws IOException {
|
||||||
// Use LinkedHashMap to get predictable iteration order
|
// Use LinkedHashMap to get predictable iteration order
|
||||||
Map m = new LinkedHashMap();
|
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
|
||||||
m.put("foo", Integer.valueOf(123)); // Written as a uint7
|
m.put("foo", Integer.valueOf(123)); // Written as a uint7
|
||||||
m.put(new RawImpl(new byte[] {}), null); // Empty array != null
|
m.put(new RawImpl(new byte[] {}), null); // Empty array != null
|
||||||
w.writeMap(m, true);
|
w.writeMap(m);
|
||||||
checkContents("F4" + "02" + "F703666F6F" + "7B" + "F600" + "F0");
|
checkContents("F4" + "02" + "F703666F6F" + "7B" + "F600" + "F0");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
public void testWriteIndefiniteList() throws IOException {
|
public void testWriteIndefiniteList() throws IOException {
|
||||||
List l = new ArrayList();
|
w.writeListStart();
|
||||||
l.add(Byte.valueOf((byte) 1)); // Written as a uint7
|
w.writeIntAny((byte) 1); // Written as uint7
|
||||||
l.add("foo");
|
w.writeUtf8("foo");
|
||||||
l.add(Long.valueOf(128L)); // Written as an int16
|
w.writeIntAny(128L); // Written as an int16
|
||||||
w.writeList(l, false);
|
w.writeListEnd();
|
||||||
checkContents("F3" + "01" + "F703666F6F" + "FC0080" + "F1");
|
checkContents("F3" + "01" + "F703666F6F" + "FC0080" + "F1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
public void testWriteIndefiniteMap() throws IOException {
|
public void testWriteIndefiniteMap() throws IOException {
|
||||||
// Use LinkedHashMap to get predictable iteration order
|
w.writeMapStart();
|
||||||
Map m = new LinkedHashMap();
|
w.writeUtf8("foo");
|
||||||
m.put("foo", Integer.valueOf(123)); // Written as a uint7
|
w.writeIntAny(123); // Written as a uint7
|
||||||
m.put(new RawImpl(new byte[] {}), null); // Empty array != null
|
w.writeRaw(new byte[] {});
|
||||||
w.writeMap(m, false);
|
w.writeNull();
|
||||||
|
w.writeMapEnd();
|
||||||
checkContents("F2" + "F703666F6F" + "7B" + "F600" + "F0" + "F1");
|
checkContents("F2" + "F703666F6F" + "7B" + "F600" + "F0" + "F1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
||||||
public void testWriteNestedMapsAndLists() throws IOException {
|
public void testWriteNestedMapsAndLists() throws IOException {
|
||||||
Map m = new LinkedHashMap();
|
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
|
||||||
m.put("foo", Integer.valueOf(123));
|
m.put("foo", Integer.valueOf(123));
|
||||||
List l = new ArrayList();
|
List<Object> l = new ArrayList<Object>();
|
||||||
l.add(Byte.valueOf((byte) 1));
|
l.add(Byte.valueOf((byte) 1));
|
||||||
Map m1 = new LinkedHashMap();
|
Map<Object, Object> m1 = new LinkedHashMap<Object, Object>();
|
||||||
m1.put(m, l);
|
m1.put(m, l);
|
||||||
w.writeMap(m1);
|
w.writeMap(m1);
|
||||||
checkContents("F4" + "01" + "F4" + "01" + "F703666F6F" + "7B" +
|
checkContents("F4" + "01" + "F4" + "01" + "F703666F6F" + "7B" +
|
||||||
|
|||||||
Reference in New Issue
Block a user