Made BdfDictionary and BdfList thread-safe.

This commit is contained in:
akwizgran
2016-01-19 11:03:27 +00:00
parent 77054cbae7
commit cd08867611
4 changed files with 12 additions and 10 deletions

View File

@@ -2,10 +2,11 @@ package org.briarproject.api.data;
import org.briarproject.api.FormatException;
import java.util.HashMap;
import java.util.Hashtable;
// This class is not thread-safe
public class BdfDictionary extends HashMap<String, Object> {
public class BdfDictionary extends Hashtable<String, Object> {
public static final Object NULL_VALUE = new Object();
public Boolean getBoolean(String key) throws FormatException {
Object o = get(key);

View File

@@ -2,10 +2,9 @@ package org.briarproject.api.data;
import org.briarproject.api.FormatException;
import java.util.ArrayList;
import java.util.Vector;
// This class is not thread-safe
public class BdfList extends ArrayList<Object> {
public class BdfList extends Vector<Object> {
public Boolean getBoolean(int index) throws FormatException {
Object o = get(index);

View File

@@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.briarproject.api.db.Metadata.REMOVE;
import static org.briarproject.data.Types.DICTIONARY;
import static org.briarproject.data.Types.END;
@@ -37,7 +38,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
Metadata m = new Metadata();
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (Entry<String, Object> e : d.entrySet()) {
if (e.getValue() == null) {
if (e.getValue() == NULL_VALUE) {
// Special case: if the value is null, the key is being removed
m.put(e.getKey(), REMOVE);
} else {
@@ -51,7 +52,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
private void encodeObject(ByteArrayOutputStream out, Object o)
throws FormatException {
if (o == null) out.write(NULL);
if (o == NULL_VALUE) out.write(NULL);
else if (o instanceof Boolean) out.write((Boolean) o ? TRUE : FALSE);
else if (o instanceof Byte) encodeInteger(out, (Byte) o);
else if (o instanceof Short) encodeInteger(out, (Short) o);

View File

@@ -10,6 +10,7 @@ import org.briarproject.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.util.Map.Entry;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.briarproject.api.db.Metadata.REMOVE;
import static org.briarproject.data.Types.DICTIONARY;
import static org.briarproject.data.Types.END;
@@ -40,7 +41,7 @@ class MetadataParserImpl implements MetadataParser {
}
private Object parseValue(byte[] b) throws FormatException {
if (b == REMOVE) return null;
if (b == REMOVE) return NULL_VALUE;
ByteArrayInputStream in = new ByteArrayInputStream(b);
Object o = parseObject(in);
if (in.available() > 0) throw new FormatException();
@@ -50,7 +51,7 @@ class MetadataParserImpl implements MetadataParser {
private Object parseObject(ByteArrayInputStream in) throws FormatException {
switch(in.read()) {
case NULL:
return null;
return NULL_VALUE;
case TRUE:
return Boolean.TRUE;
case FALSE: