From 51e371f7ca55e54b795363bbe1b685d8c5dbcbf8 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Mon, 11 Jul 2011 10:07:01 +0100 Subject: [PATCH] Added start/end methods for writing indefinite lists and maps. --- api/net/sf/briar/api/serial/Writer.java | 6 +- .../net/sf/briar/serial/WriterImpl.java | 55 ++++++++----------- test/net/sf/briar/serial/WriterImplTest.java | 42 +++++++------- 3 files changed, 47 insertions(+), 56 deletions(-) diff --git a/api/net/sf/briar/api/serial/Writer.java b/api/net/sf/briar/api/serial/Writer.java index 48661a82fb..5ea269eb5a 100644 --- a/api/net/sf/briar/api/serial/Writer.java +++ b/api/net/sf/briar/api/serial/Writer.java @@ -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; } diff --git a/components/net/sf/briar/serial/WriterImpl.java b/components/net/sf/briar/serial/WriterImpl.java index aca49adc17..626a47a48f 100644 --- a/components/net/sf/briar/serial/WriterImpl.java +++ b/components/net/sf/briar/serial/WriterImpl.java @@ -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 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 writeListStart() throws IOException { + out.write(Tag.LIST_INDEF); + } + + 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 { diff --git a/test/net/sf/briar/serial/WriterImplTest.java b/test/net/sf/briar/serial/WriterImplTest.java index c50e3a552f..f4afce160e 100644 --- a/test/net/sf/briar/serial/WriterImplTest.java +++ b/test/net/sf/briar/serial/WriterImplTest.java @@ -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" + -- GitLab