java.lang.Object
com.maxmind.db.Decoder

final class Decoderextends Object
Decoder for MaxMind DB data.

This class CANNOT be shared between threads

  • Field Details

    • UTF_8

      private static final Charset UTF_8
      Charset the database strings are decoded with.
    • POINTER_VALUE_OFFSETS

      private static final int[] POINTER_VALUE_OFFSETS
      Value offsets added for each pointer size.
    • POINTER_TEST_HACK

      boolean POINTER_TEST_HACK
      Set by the unit tests to exercise pointer decoding; false in normal use. Not settable through a constructor.
    • cache

      private final NodeCache cache
      Cache of the tree nodes read.
    • pointerBase

      private final long pointerBase
      Offset of the data section start.
    • utfDecoder

      private final CharsetDecoder utfDecoder
      Decoder for the database strings.
    • buffer

      private final ByteBuffer buffer
      Buffer holding the database data.
    • cacheLoader

      private final NodeCache.Loader 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 through
      buffer - the database data section, positioned by the caller
      pointerBase - the base offset that stored pointer values are relative to
  • Method Details

    • decode

      Object decode(int offset) throws IOException
      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

      Object decode() throws IOException
      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

      private Object decodeByType(Decoder.Type type, int size) throws IOException
      Decodes one value of the given type.
      Parameters:
      type - the type of the value
      size - the payload size from the control byte
      Returns:
      the decoded value
      Throws:
      IOException - if the data cannot be read
    • decodeString

      private String decodeString(int size) throws CharacterCodingException
      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

      private Integer decodeUint16(int size)
      Decodes a 16-bit unsigned integer.
      Parameters:
      size - the number of bytes to read
      Returns:
      the value, 0 to 65535
    • decodeInt32

      private Integer decodeInt32(int size)
      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

      private Long decodeUint32(int size)
      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 from
      size - the number of bytes to read
      Returns:
      the value read
    • decodeInteger

      static int decodeInteger(ByteBuffer buffer, int base, int size)
      Assemble a big-endian integer from size bytes of the buffer.
      Parameters:
      buffer - the buffer the bytes are read from, left advanced past them
      base - the starting value that the bytes are shifted in on top of
      size - the number of bytes to read
      Returns:
      the assembled integer
    • decodeBigInteger

      private BigInteger decodeBigInteger(int size)
      Decodes an arbitrary precision integer.
      Parameters:
      size - the number of bytes to read
      Returns:
      the value, always positive
    • decodeDouble

      private Double decodeDouble(int size) throws InvalidDatabaseException
      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

      private Float decodeFloat(int size) throws InvalidDatabaseException
      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

      private static Boolean decodeBoolean(int size) throws InvalidDatabaseException
      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

      private List<Object> decodeArray(int size) throws IOException
      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

      private Map<String,Object> decodeMap(int size) throws IOException
      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

      private static byte[] getByteArray(ByteBuffer buffer, int length)
      Reads bytes from a buffer position.
      Parameters:
      buffer - the buffer to read from
      length - the number of bytes to read
      Returns:
      the bytes read