Added start/end methods for writing indefinite lists and maps.

This commit is contained in:
akwizgran
2011-07-11 10:07:01 +01:00
parent 63f1caebac
commit 51e371f7ca
3 changed files with 45 additions and 54 deletions

View File

@@ -22,11 +22,13 @@ public interface Writer {
void writeRaw(byte[] b) throws IOException;
void writeRaw(Raw r) throws IOException;
void writeList(List<?> l, boolean definite) 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 writeMapStart() throws IOException;
void writeMapEnd() throws IOException;
void writeNull() throws IOException;
}

View File

@@ -106,16 +106,10 @@ class WriterImpl implements Writer {
writeRaw(r.getBytes());
}
public void writeList(List<?> l, boolean definite) throws IOException {
if(definite) {
out.write(Tag.LIST_DEF);
writeIntAny(l.size());
for(Object o : l) writeObject(o);
} else {
out.write(Tag.LIST_INDEF);
for(Object o : l) writeObject(o);
out.write(Tag.END);
}
public void writeList(List<?> l) throws IOException {
out.write(Tag.LIST_DEF);
writeIntAny(l.size());
for(Object o : l) writeObject(o);
}
private void writeObject(Object o) throws IOException {
@@ -134,30 +128,29 @@ class WriterImpl implements Writer {
else throw new IllegalStateException();
}
public void writeList(List<?> l) throws IOException {
writeList(l, true);
public void writeListStart() throws IOException {
out.write(Tag.LIST_INDEF);
}
public void writeMap(Map<?, ?> m, boolean definite) throws IOException {
if(definite) {
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 writeListEnd() throws IOException {
out.write(Tag.END);
}
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 {

View File

@@ -146,57 +146,53 @@ public class WriterImplTest extends TestCase {
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
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("foo");
l.add(Long.valueOf(128L)); // Written as an int16
w.writeList(l, true);
w.writeList(l);
checkContents("F5" + "03" + "01" + "F703666F6F" + "FC0080");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testWriteDefiniteMap() throws IOException {
// 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(new RawImpl(new byte[] {}), null); // Empty array != null
w.writeMap(m, true);
w.writeMap(m);
checkContents("F4" + "02" + "F703666F6F" + "7B" + "F600" + "F0");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testWriteIndefiniteList() throws IOException {
List l = new ArrayList();
l.add(Byte.valueOf((byte) 1)); // Written as a uint7
l.add("foo");
l.add(Long.valueOf(128L)); // Written as an int16
w.writeList(l, false);
w.writeListStart();
w.writeIntAny((byte) 1); // Written as uint7
w.writeUtf8("foo");
w.writeIntAny(128L); // Written as an int16
w.writeListEnd();
checkContents("F3" + "01" + "F703666F6F" + "FC0080" + "F1");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testWriteIndefiniteMap() throws IOException {
// Use LinkedHashMap to get predictable iteration order
Map m = new LinkedHashMap();
m.put("foo", Integer.valueOf(123)); // Written as a uint7
m.put(new RawImpl(new byte[] {}), null); // Empty array != null
w.writeMap(m, false);
w.writeMapStart();
w.writeUtf8("foo");
w.writeIntAny(123); // Written as a uint7
w.writeRaw(new byte[] {});
w.writeNull();
w.writeMapEnd();
checkContents("F2" + "F703666F6F" + "7B" + "F600" + "F0" + "F1");
}
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testWriteNestedMapsAndLists() throws IOException {
Map m = new LinkedHashMap();
Map<Object, Object> m = new LinkedHashMap<Object, Object>();
m.put("foo", Integer.valueOf(123));
List l = new ArrayList();
List<Object> l = new ArrayList<Object>();
l.add(Byte.valueOf((byte) 1));
Map m1 = new LinkedHashMap();
Map<Object, Object> m1 = new LinkedHashMap<Object, Object>();
m1.put(m, l);
w.writeMap(m1);
checkContents("F4" + "01" + "F4" + "01" + "F703666F6F" + "7B" +