diff --git a/briar-api/src/org/briarproject/api/data/BdfDictionary.java b/briar-api/src/org/briarproject/api/data/BdfDictionary.java index b37cfc606737a8dd8f0ba94a7f743cb09edb6e94..eecea996c123ed0dfb4ca4ef42987531362b3c22 100644 --- a/briar-api/src/org/briarproject/api/data/BdfDictionary.java +++ b/briar-api/src/org/briarproject/api/data/BdfDictionary.java @@ -1,8 +1,9 @@ package org.briarproject.api.data; -import java.util.Hashtable; +import java.util.HashMap; -public class BdfDictionary extends Hashtable<String, Object> { +// This class is not thread-safe +public class BdfDictionary extends HashMap<String, Object> { public Boolean getBoolean(String key, Boolean defaultValue) { Object o = get(key); diff --git a/briar-api/src/org/briarproject/api/data/BdfList.java b/briar-api/src/org/briarproject/api/data/BdfList.java index d39588d0d667e89f22018277a4a706930a27d181..949d414676c10f27d6824df29841cbeb573beb49 100644 --- a/briar-api/src/org/briarproject/api/data/BdfList.java +++ b/briar-api/src/org/briarproject/api/data/BdfList.java @@ -1,8 +1,9 @@ package org.briarproject.api.data; -import java.util.Vector; +import java.util.ArrayList; -public class BdfList extends Vector<Object> { +// This class is not thread-safe +public class BdfList extends ArrayList<Object> { public Boolean getBoolean(int index, Boolean defaultValue) { Object o = get(index); diff --git a/briar-api/src/org/briarproject/api/db/Metadata.java b/briar-api/src/org/briarproject/api/db/Metadata.java index c54d15cff74f2504e77bf52d64c9c02c934ea7e7..b70df227f9d4573a69af8315dfcadbdc71da0fea 100644 --- a/briar-api/src/org/briarproject/api/db/Metadata.java +++ b/briar-api/src/org/briarproject/api/db/Metadata.java @@ -3,4 +3,9 @@ package org.briarproject.api.db; import java.util.Hashtable; public class Metadata extends Hashtable<String, byte[]> { + + /** + * Special value to indicate that a key is being removed. + */ + public static final byte[] REMOVE = new byte[0]; } diff --git a/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java b/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java index 313b62f4b1752e6b26fe7b45dc233314ae430085..fc7dc3f9dc5a2808f71a993aa5984c2f1bf4eb99 100644 --- a/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java +++ b/briar-core/src/org/briarproject/data/MetadataEncoderImpl.java @@ -2,14 +2,16 @@ package org.briarproject.data; import org.briarproject.api.FormatException; import org.briarproject.api.data.BdfDictionary; -import org.briarproject.api.data.BdfList; import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.db.Metadata; import org.briarproject.util.StringUtils; import java.io.ByteArrayOutputStream; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import static org.briarproject.api.db.Metadata.REMOVE; import static org.briarproject.data.Types.DICTIONARY; import static org.briarproject.data.Types.END; import static org.briarproject.data.Types.FALSE; @@ -34,10 +36,10 @@ class MetadataEncoderImpl implements MetadataEncoder { public Metadata encode(BdfDictionary d) throws FormatException { Metadata m = new Metadata(); ByteArrayOutputStream out = new ByteArrayOutputStream(); - for (Map.Entry<String, Object> e : d.entrySet()) { + for (Entry<String, Object> e : d.entrySet()) { if (e.getValue() == null) { // Special case: if the value is null, the key is being removed - m.put(e.getKey(), null); + m.put(e.getKey(), REMOVE); } else { encodeObject(out, e.getValue()); m.put(e.getKey(), out.toByteArray()); @@ -59,9 +61,8 @@ class MetadataEncoderImpl implements MetadataEncoder { else if (o instanceof Double) encodeFloat(out, (Double) o); else if (o instanceof String) encodeString(out, (String) o); else if (o instanceof byte[]) encodeRaw(out, (byte[]) o); - else if (o instanceof BdfList) encodeList(out, (BdfList) o); - else if (o instanceof BdfDictionary) encodeDictionary(out, - (BdfDictionary) o); + else if (o instanceof List) encodeList(out, (List) o); + else if (o instanceof Map) encodeDictionary(out, (Map) o); else throw new FormatException(); } @@ -154,18 +155,19 @@ class MetadataEncoderImpl implements MetadataEncoder { out.write(b, 0, b.length); } - private void encodeList(ByteArrayOutputStream out, BdfList list) + private void encodeList(ByteArrayOutputStream out, List list) throws FormatException { out.write(LIST); for (Object o : list) encodeObject(out, o); out.write(END); } - private void encodeDictionary(ByteArrayOutputStream out, - BdfDictionary dict) throws FormatException { + private void encodeDictionary(ByteArrayOutputStream out, Map<?, ?> map) + throws FormatException { out.write(DICTIONARY); - for (Map.Entry<String, Object> e : dict.entrySet()) { - encodeString(out, e.getKey()); + for (Entry<?, ?> e : map.entrySet()) { + if (!(e.getKey() instanceof String)) throw new FormatException(); + encodeString(out, (String) e.getKey()); encodeObject(out, e.getValue()); } out.write(END); diff --git a/briar-core/src/org/briarproject/data/MetadataParserImpl.java b/briar-core/src/org/briarproject/data/MetadataParserImpl.java index e705bcca597c65caf31a8bdb79666fb7d5ef0289..ff9258f14934d0d92328a53b2f422ec922dd9c99 100644 --- a/briar-core/src/org/briarproject/data/MetadataParserImpl.java +++ b/briar-core/src/org/briarproject/data/MetadataParserImpl.java @@ -8,7 +8,7 @@ import org.briarproject.api.db.Metadata; import org.briarproject.util.StringUtils; import java.io.ByteArrayInputStream; -import java.util.Map; +import java.util.Map.Entry; import static org.briarproject.data.Types.DICTIONARY; import static org.briarproject.data.Types.END; @@ -33,7 +33,7 @@ class MetadataParserImpl implements MetadataParser { @Override public BdfDictionary parse(Metadata m) throws FormatException { BdfDictionary dict = new BdfDictionary(); - for (Map.Entry<String, byte[]> e : m.entrySet()) + for (Entry<String, byte[]> e : m.entrySet()) dict.put(e.getKey(), parseObject(e.getValue())); return dict; }