mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Convert BDF types in lists and dictionaries.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.api.data;
|
package org.briarproject.api.data;
|
||||||
|
|
||||||
|
import org.briarproject.api.Bytes;
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
|
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
@@ -23,24 +24,32 @@ public class BdfDictionary extends Hashtable<String, Object> {
|
|||||||
public Long getInteger(String key) throws FormatException {
|
public Long getInteger(String key) throws FormatException {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof Long) return (Long) o;
|
if (o instanceof Long) return (Long) o;
|
||||||
|
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||||
|
if (o instanceof Short) return ((Short) o).longValue();
|
||||||
|
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getInteger(String key, Long defaultValue) {
|
public Long getInteger(String key, Long defaultValue) {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof Long) return (Long) o;
|
if (o instanceof Long) return (Long) o;
|
||||||
|
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||||
|
if (o instanceof Short) return ((Short) o).longValue();
|
||||||
|
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getFloat(String key) throws FormatException {
|
public Double getFloat(String key) throws FormatException {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof Double) return (Double) o;
|
if (o instanceof Double) return (Double) o;
|
||||||
|
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getFloat(String key, Double defaultValue) {
|
public Double getFloat(String key, Double defaultValue) {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof Double) return (Double) o;
|
if (o instanceof Double) return (Double) o;
|
||||||
|
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +68,14 @@ public class BdfDictionary extends Hashtable<String, Object> {
|
|||||||
public byte[] getRaw(String key) throws FormatException {
|
public byte[] getRaw(String key) throws FormatException {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof byte[]) return (byte[]) o;
|
if (o instanceof byte[]) return (byte[]) o;
|
||||||
|
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getRaw(String key, byte[] defaultValue) {
|
public byte[] getRaw(String key, byte[] defaultValue) {
|
||||||
Object o = get(key);
|
Object o = get(key);
|
||||||
if (o instanceof byte[]) return (byte[]) o;
|
if (o instanceof byte[]) return (byte[]) o;
|
||||||
|
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.api.data;
|
package org.briarproject.api.data;
|
||||||
|
|
||||||
|
import org.briarproject.api.Bytes;
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
|
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
@@ -21,24 +22,32 @@ public class BdfList extends Vector<Object> {
|
|||||||
public Long getInteger(int index) throws FormatException {
|
public Long getInteger(int index) throws FormatException {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof Long) return (Long) o;
|
if (o instanceof Long) return (Long) o;
|
||||||
|
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||||
|
if (o instanceof Short) return ((Short) o).longValue();
|
||||||
|
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getInteger(int index, Long defaultValue) {
|
public Long getInteger(int index, Long defaultValue) {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof Long) return (Long) o;
|
if (o instanceof Long) return (Long) o;
|
||||||
|
if (o instanceof Integer) return ((Integer) o).longValue();
|
||||||
|
if (o instanceof Short) return ((Short) o).longValue();
|
||||||
|
if (o instanceof Byte) return ((Byte) o).longValue();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getFloat(int index) throws FormatException {
|
public Double getFloat(int index) throws FormatException {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof Double) return (Double) o;
|
if (o instanceof Double) return (Double) o;
|
||||||
|
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double getFloat(int index, Double defaultValue) {
|
public Double getFloat(int index, Double defaultValue) {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof Double) return (Double) o;
|
if (o instanceof Double) return (Double) o;
|
||||||
|
if (o instanceof Float) return ((Float) o).doubleValue();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,12 +66,14 @@ public class BdfList extends Vector<Object> {
|
|||||||
public byte[] getRaw(int index) throws FormatException {
|
public byte[] getRaw(int index) throws FormatException {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof byte[]) return (byte[]) o;
|
if (o instanceof byte[]) return (byte[]) o;
|
||||||
|
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getRaw(int index, byte[] defaultValue) {
|
public byte[] getRaw(int index, byte[] defaultValue) {
|
||||||
Object o = get(index);
|
Object o = get(index);
|
||||||
if (o instanceof byte[]) return (byte[]) o;
|
if (o instanceof byte[]) return (byte[]) o;
|
||||||
|
if (o instanceof Bytes) return ((Bytes) o).getBytes();
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.data;
|
package org.briarproject.data;
|
||||||
|
|
||||||
|
import org.briarproject.api.Bytes;
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
import org.briarproject.api.data.MetadataEncoder;
|
import org.briarproject.api.data.MetadataEncoder;
|
||||||
@@ -62,6 +63,7 @@ class MetadataEncoderImpl implements MetadataEncoder {
|
|||||||
else if (o instanceof Double) encodeFloat(out, (Double) o);
|
else if (o instanceof Double) encodeFloat(out, (Double) o);
|
||||||
else if (o instanceof String) encodeString(out, (String) o);
|
else if (o instanceof String) encodeString(out, (String) o);
|
||||||
else if (o instanceof byte[]) encodeRaw(out, (byte[]) o);
|
else if (o instanceof byte[]) encodeRaw(out, (byte[]) o);
|
||||||
|
else if (o instanceof Bytes) encodeRaw(out, ((Bytes) o).getBytes());
|
||||||
else if (o instanceof List) encodeList(out, (List) o);
|
else if (o instanceof List) encodeList(out, (List) o);
|
||||||
else if (o instanceof Map) encodeDictionary(out, (Map) o);
|
else if (o instanceof Map) encodeDictionary(out, (Map) o);
|
||||||
else throw new FormatException();
|
else throw new FormatException();
|
||||||
|
|||||||
Reference in New Issue
Block a user