Modify MaxMind-DB-Reader-java

to remove the dependency on the large com.fasterxml.jackson.databind JSON package,
and use POJOs instead.
This commit is contained in:
zzz
2018-11-20 11:03:47 +00:00
parent 98de1ae404
commit 3923db0677
6 changed files with 80 additions and 80 deletions
+32 -38
View File
@@ -12,10 +12,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.*;
/*
* Decoder for MaxMind DB data.
*
@@ -25,8 +21,6 @@ final class Decoder {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final int[] POINTER_VALUE_OFFSETS = { 0, 0, 1 << 11, (1 << 19) + ((1) << 11), 0 };
// XXX - This is only for unit testings. We should possibly make a
@@ -71,12 +65,12 @@ final class Decoder {
private final NodeCache.Loader cacheLoader = new NodeCache.Loader() {
@Override
public JsonNode load(int key) throws IOException {
public Object load(int key) throws IOException {
return decode(key);
}
};
JsonNode decode(int offset) throws IOException {
Object decode(int offset) throws IOException {
if (offset >= this.buffer.capacity()) {
throw new InvalidDatabaseException(
"The MaxMind DB file's data section contains bad data: "
@@ -87,7 +81,7 @@ final class Decoder {
return decode();
}
JsonNode decode() throws IOException {
Object decode() throws IOException {
int ctrlByte = 0xFF & this.buffer.get();
Type type = Type.fromControlByte(ctrlByte);
@@ -103,12 +97,12 @@ final class Decoder {
// for unit testing
if (this.POINTER_TEST_HACK) {
return new LongNode(pointer);
return Long.valueOf(pointer);
}
int targetOffset = (int) pointer;
int position = buffer.position();
JsonNode node = cache.get(targetOffset, cacheLoader);
Object node = cache.get(targetOffset, cacheLoader);
buffer.position(position);
return node;
}
@@ -147,7 +141,7 @@ final class Decoder {
return this.decodeByType(type, size);
}
private JsonNode decodeByType(Type type, int size)
private Object decodeByType(Type type, int size)
throws IOException {
switch (type) {
case MAP:
@@ -157,13 +151,13 @@ final class Decoder {
case BOOLEAN:
return Decoder.decodeBoolean(size);
case UTF8_STRING:
return new TextNode(this.decodeString(size));
return this.decodeString(size);
case DOUBLE:
return this.decodeDouble(size);
case FLOAT:
return this.decodeFloat(size);
case BYTES:
return new BinaryNode(this.getByteArray(size));
return this.getByteArray(size);
case UINT16:
return this.decodeUint16(size);
case UINT32:
@@ -188,12 +182,12 @@ final class Decoder {
return s;
}
private IntNode decodeUint16(int size) {
return new IntNode(this.decodeInteger(size));
private Integer decodeUint16(int size) {
return Integer.valueOf(this.decodeInteger(size));
}
private IntNode decodeInt32(int size) {
return new IntNode(this.decodeInteger(size));
private Integer decodeInt32(int size) {
return Integer.valueOf(this.decodeInteger(size));
}
private long decodeLong(int size) {
@@ -204,8 +198,8 @@ final class Decoder {
return integer;
}
private LongNode decodeUint32(int size) {
return new LongNode(this.decodeLong(size));
private Long decodeUint32(int size) {
return Long.valueOf(this.decodeLong(size));
}
private int decodeInteger(int size) {
@@ -224,36 +218,36 @@ final class Decoder {
return integer;
}
private BigIntegerNode decodeBigInteger(int size) {
private BigInteger decodeBigInteger(int size) {
byte[] bytes = this.getByteArray(size);
return new BigIntegerNode(new BigInteger(1, bytes));
return new BigInteger(1, bytes);
}
private DoubleNode decodeDouble(int size) throws InvalidDatabaseException {
private Double decodeDouble(int size) throws InvalidDatabaseException {
if (size != 8) {
throw new InvalidDatabaseException(
"The MaxMind DB file's data section contains bad data: "
+ "invalid size of double.");
}
return new DoubleNode(this.buffer.getDouble());
return Double.valueOf(this.buffer.getDouble());
}
private FloatNode decodeFloat(int size) throws InvalidDatabaseException {
private Float decodeFloat(int size) throws InvalidDatabaseException {
if (size != 4) {
throw new InvalidDatabaseException(
"The MaxMind DB file's data section contains bad data: "
+ "invalid size of float.");
}
return new FloatNode(this.buffer.getFloat());
return Float.valueOf(this.buffer.getFloat());
}
private static BooleanNode decodeBoolean(int size)
private static Boolean decodeBoolean(int size)
throws InvalidDatabaseException {
switch (size) {
case 0:
return BooleanNode.FALSE;
return Boolean.FALSE;
case 1:
return BooleanNode.TRUE;
return Boolean.TRUE;
default:
throw new InvalidDatabaseException(
"The MaxMind DB file's data section contains bad data: "
@@ -261,28 +255,28 @@ final class Decoder {
}
}
private JsonNode decodeArray(int size) throws IOException {
private List<Object> decodeArray(int size) throws IOException {
List<JsonNode> array = new ArrayList<JsonNode>(size);
List<Object> array = new ArrayList<Object>(size);
for (int i = 0; i < size; i++) {
JsonNode r = this.decode();
Object r = this.decode();
array.add(r);
}
return new ArrayNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableList(array));
return Collections.unmodifiableList(array);
}
private JsonNode decodeMap(int size) throws IOException {
private Map<String, Object> decodeMap(int size) throws IOException {
int capacity = (int) (size / 0.75F + 1.0F);
Map<String, JsonNode> map = new HashMap<String, JsonNode>(capacity);
Map<String, Object> map = new HashMap<String, Object>(capacity);
for (int i = 0; i < size; i++) {
String key = this.decode().asText();
JsonNode value = this.decode();
String key = (String) this.decode();
Object value = this.decode();
map.put(key, value);
}
return new ObjectNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableMap(map));
return Collections.unmodifiableMap(map);
}
private byte[] getByteArray(int length) {