java.lang.Object
com.maxmind.db.Decoder
Decoder for MaxMind DB data.
This class CANNOT be shared between threads
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumEnumeration of data types supported in MaxMind DB format.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ByteBufferBuffer holding the database data.private final NodeCacheCache of the tree nodes read.private final NodeCache.LoaderLoader supplying the nodes to be cached.(package private) booleanSet by the unit tests to exercise pointer decoding; false in normal use.private static final int[]Value offsets added for each pointer size.private final longOffset of the data section start.private static final CharsetCharset the database strings are decoded with.private final CharsetDecoderDecoder for the database strings.Constructor Summary
ConstructorsConstructorDescriptionDecoder(NodeCache cache, ByteBuffer buffer, long pointerBase) Construct a decoder over the data section of a MaxMind DB file.Method Summary
Modifier and TypeMethodDescription(package private) Objectdecode()Decode the value stored at the buffer's current position.(package private) Objectdecode(int offset) Decode the value stored at an offset in the data section.decodeArray(int size) Decode an array of size entries from the data section.private BigIntegerdecodeBigInteger(int size) Decodes an arbitrary precision integer.private static BooleandecodeBoolean(int size) Decodes a boolean value.private ObjectdecodeByType(Decoder.Type type, int size) Decodes one value of the given type.private DoubledecodeDouble(int size) Decodes a double value.private FloatdecodeFloat(int size) Decodes a float value.private IntegerdecodeInt32(int size) Decodes a 32-bit signed integer.private intdecodeInteger(int size) Decodes an integer at the current position.private intdecodeInteger(int base, int size) Decodes an integer from a buffer position.(package private) static intdecodeInteger(ByteBuffer buffer, int base, int size) Assemble a big-endian integer from size bytes of the buffer.private longdecodeLong(int size) Decodes a 64-bit signed integer.decodeMap(int size) Decode a map with size key-value entries from the data section.private StringdecodeString(int size) Decodes a string value.private IntegerdecodeUint16(int size) Decodes a 16-bit unsigned integer.private LongdecodeUint32(int size) Decodes a 32-bit unsigned integer.private byte[]getByteArray(int length) Reads bytes from the current position.private static byte[]getByteArray(ByteBuffer buffer, int length) Reads bytes from a buffer position.
Field Details
UTF_8
Charset the database strings are decoded with.POINTER_VALUE_OFFSETS
private static final int[] POINTER_VALUE_OFFSETSValue offsets added for each pointer size.POINTER_TEST_HACK
boolean POINTER_TEST_HACKSet by the unit tests to exercise pointer decoding; false in normal use. Not settable through a constructor.cache
Cache of the tree nodes read.pointerBase
private final long pointerBaseOffset of the data section start.utfDecoder
Decoder for the database strings.buffer
Buffer holding the database data.cacheLoader
Loader supplying the nodes to be cached.
Constructor Details
Decoder
Decoder(NodeCache cache, ByteBuffer buffer, long pointerBase) Construct a decoder over the data section of a MaxMind DB file.- Parameters:
cache- the node cache that pointer indirections resolve throughbuffer- the database data section, positioned by the callerpointerBase- the base offset that stored pointer values are relative to
Method Details
decode
Decode the value stored at an offset in the data section.- Parameters:
offset- the position in the data section to decode from- Returns:
- the decoded value, of the type named by the control byte at that position
- Throws:
IOException- if the offset is past the end of the data section, or the data at it is corrupt
decode
Decode the value stored at the buffer's current position.- Returns:
- the decoded value, of the type named by the control byte at the current position
- Throws:
IOException- if the data at the current position is corrupt
decodeByType
Decodes one value of the given type.- Parameters:
type- the type of the valuesize- the payload size from the control byte- Returns:
- the decoded value
- Throws:
IOException- if the data cannot be read
decodeString
Decodes a string value.- Parameters:
size- the number of bytes to read- Returns:
- the decoded text
- Throws:
CharacterCodingException- if the bytes are not valid UTF-8
decodeUint16
Decodes a 16-bit unsigned integer.- Parameters:
size- the number of bytes to read- Returns:
- the value, 0 to 65535
decodeInt32
Decodes a 32-bit signed integer.- Parameters:
size- the number of bytes to read- Returns:
- the value, signed 32-bit
decodeLong
private long decodeLong(int size) Decodes a 64-bit signed integer.- Parameters:
size- the number of bytes to read- Returns:
- the value, signed 64-bit
decodeUint32
Decodes a 32-bit unsigned integer.- Parameters:
size- the number of bytes to read- Returns:
- the value, 0 to 2^32-1
decodeInteger
private int decodeInteger(int size) Decodes an integer at the current position.- Parameters:
size- the number of bytes to read- Returns:
- the value read
decodeInteger
private int decodeInteger(int base, int size) Decodes an integer from a buffer position.- Parameters:
base- the buffer position to read fromsize- the number of bytes to read- Returns:
- the value read
decodeInteger
Assemble a big-endian integer from size bytes of the buffer.- Parameters:
buffer- the buffer the bytes are read from, left advanced past thembase- the starting value that the bytes are shifted in on top ofsize- the number of bytes to read- Returns:
- the assembled integer
decodeBigInteger
Decodes an arbitrary precision integer.- Parameters:
size- the number of bytes to read- Returns:
- the value, always positive
decodeDouble
Decodes a double value.- Parameters:
size- the number of bytes, must be 8- Returns:
- the number read
- Throws:
InvalidDatabaseException- if the size is not 8
decodeFloat
Decodes a float value.- Parameters:
size- the number of bytes, must be 4- Returns:
- the number read
- Throws:
InvalidDatabaseException- if the size is not 4
decodeBoolean
Decodes a boolean value.- Parameters:
size- the payload size, 0 for false or 1 for true- Returns:
- the flag read
- Throws:
InvalidDatabaseException- if the size is neither 0 nor 1
decodeArray
Decode an array of size entries from the data section.- Parameters:
size- the number of entries to decode- Returns:
- modifiable, don't modify it
- Throws:
IOException- if the data section is truncated or invalid
decodeMap
Decode a map with size key-value entries from the data section.- Parameters:
size- the number of entries to decode- Returns:
- modifiable, don't modify it
- Throws:
IOException- if the data section is truncated or invalid
getByteArray
private byte[] getByteArray(int length) Reads bytes from the current position.- Parameters:
length- the number of bytes to read- Returns:
- the bytes read
getByteArray
Reads bytes from a buffer position.- Parameters:
buffer- the buffer to read fromlength- the number of bytes to read- Returns:
- the bytes read