Class DefaultDatatypeCoder

java.lang.Object
org.firebirdsql.gds.ng.DefaultDatatypeCoder
All Implemented Interfaces:
DatatypeCoder

public class DefaultDatatypeCoder extends Object implements DatatypeCoder
The default datatype coder.

Implements the encoding and decoding for the wire protocol.

As a lot of the implementation also applies to the big endian and little endian decoders for the JNA implementation, this class is not placed in package org.firebirdsql.gds.ng.wire

Since:
3
Author:
Mark Rotteveel
  • Constructor Details

    • DefaultDatatypeCoder

      public DefaultDatatypeCoder(IEncodingFactory encodingFactory)
      Creates a default datatype coder for the wire protocol.

      In almost all cases, it is better to use forEncodingFactory(IEncodingFactory).

      Parameters:
      encodingFactory - encoding factory
  • Method Details

    • forEncodingFactory

      public static DefaultDatatypeCoder forEncodingFactory(IEncodingFactory encodingFactory)
      Returns an instance of DefaultDatatypeCoder for an encoding factory.
      Parameters:
      encodingFactory - encoding factory
      Returns:
      datatype coder, this might be a cached instance
    • sizeOfShort

      public int sizeOfShort()
      Description copied from interface: DatatypeCoder
      The size of an encoded short in this data type coder.
      Specified by:
      sizeOfShort in interface DatatypeCoder
      Returns:
      size of an encoded short (either 2 or 4 bytes)
    • encodeShort

      public byte[] encodeShort(short val)
      Description copied from interface: DatatypeCoder
      Encode a short value as a byte array of length DatatypeCoder.sizeOfShort().
      Specified by:
      encodeShort in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array
    • encodeShort

      public byte[] encodeShort(int val)
      Description copied from interface: DatatypeCoder
      Encode a short value as a byte array of length DatatypeCoder.sizeOfShort().
      Specified by:
      encodeShort in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array
    • encodeShort

      public void encodeShort(int val, byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Encode a short value into buf starting at offset off for DatatypeCoder.sizeOfShort() bytes.

      NOTE: Implementations using 4 bytes to encode a short may choose to encode val (an int) as-is (which means the most significant two bytes can have a value other than 0x0000 or 0xFFFF, and a value of 0xFFFF (65_535) may be encoded as 0x0000_FFFF, and not as 0xFFFF_FFFF (-1). This behaviour may change at any time. For consistent behaviour, explicitly cast to short when calling this method.

      Specified by:
      encodeShort in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      buf - byte array of sufficient size (warning: this is datatype coder specific, see DatatypeCoder.sizeOfShort()), never null
      off - offset to start encoding
    • decodeShort

      public short decodeShort(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a short value from buf from the first DatatypeCoder.sizeOfShort() bytes.
      Specified by:
      decodeShort in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size (warning: this is datatype coder specific, see DatatypeCoder.sizeOfShort())
      Returns:
      short value from buf, or 0 when buf is null
    • decodeShort

      public short decodeShort(byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Decode a short value from buf starting at offset off for DatatypeCoder.sizeOfShort() bytes.
      Specified by:
      decodeShort in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size (warning: this is datatype coder specific, see DatatypeCoder.sizeOfShort()), never null
      off - offset to start decoding
      Returns:
      short value from buf
    • encodeInt

      public byte[] encodeInt(int val)
      Description copied from interface: DatatypeCoder
      Encode an int value as a byte array of 4 bytes.
      Specified by:
      encodeInt in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array, or 0 when buf is null
    • encodeInt

      public void encodeInt(int val, byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Encode an int value into buf starting at index off for 4 bytes.
      Specified by:
      encodeInt in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      buf - byte array of sufficient size, never null
      off - offset to start encoding
    • decodeInt

      public int decodeInt(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode an int value from buf from the first 4 bytes.
      Specified by:
      decodeInt in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size
      Returns:
      int value decoded from buf, or 0 when buf is null
    • decodeInt

      public int decodeInt(byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Decode an int value from buf starting at offset off for 4 bytes.
      Specified by:
      decodeInt in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size, never null
      off - offset to start decoding
      Returns:
      int value decoded from buf
    • encodeLong

      public byte[] encodeLong(long val)
      Description copied from interface: DatatypeCoder
      Encode a long value as a byte array of 8 bytes.
      Specified by:
      encodeLong in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array
    • decodeLong

      public long decodeLong(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a long value from buf from the first 8 bytes.
      Specified by:
      decodeLong in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size, or 0 when buf is null
      Returns:
      long value decoded from buf
    • encodeFloat

      public byte[] encodeFloat(float val)
      Description copied from interface: DatatypeCoder
      Encode a float value as a byte array of 4 bytes.
      Specified by:
      encodeFloat in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array
    • decodeFloat

      public float decodeFloat(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a float value from buf from the first 4 bytes.
      Specified by:
      decodeFloat in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size, or 0 when buf is null
      Returns:
      float value decoded from buf
    • encodeDouble

      public byte[] encodeDouble(double val)
      Description copied from interface: DatatypeCoder
      Encode a double value as a byte array of 8 bytes.
      Specified by:
      encodeDouble in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array
    • decodeDouble

      public double decodeDouble(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a double value from buf from the first 8 bytes.
      Specified by:
      decodeDouble in interface DatatypeCoder
      Parameters:
      buf - byte array of sufficient size, or 0 when buf is null
      Returns:
      double value decoded from buf
    • encodeString

      public final byte[] encodeString(String val)
      Description copied from interface: DatatypeCoder
      Encode a String value as a byte array using the encoding of this datatype coder.
      Specified by:
      encodeString in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array, or null if val is null
    • createWriter

      public final Writer createWriter(OutputStream out)
      Description copied from interface: DatatypeCoder
      Creates a writer wrapping an input stream.
      Specified by:
      createWriter in interface DatatypeCoder
      Parameters:
      out - output stream
      Returns:
      writer applying the encoding of this datatype when writing
    • decodeString

      public final String decodeString(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a String from buf using the encoding of this datatype coder.
      Specified by:
      decodeString in interface DatatypeCoder
      Parameters:
      buf - byte array to be decoded
      Returns:
      String decoded from buf, or null if buf is null
    • createReader

      public final Reader createReader(InputStream in)
      Description copied from interface: DatatypeCoder
      Creates a reader wrapping an input stream.
      Specified by:
      createReader in interface DatatypeCoder
      Parameters:
      in - input stream
      Returns:
      reader applying the encoding of this datatype coder when reading
    • decodeBoolean

      public boolean decodeBoolean(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode a boolean from buf from the first byte.
      Specified by:
      decodeBoolean in interface DatatypeCoder
      Parameters:
      buf - (expected) 1 bytes
      Returns:
      false when 0, true for all other values, or false if buf is null
    • encodeBoolean

      public byte[] encodeBoolean(boolean val)
      Description copied from interface: DatatypeCoder
      Encodes boolean as a byte array of 1 byte.
      Specified by:
      encodeBoolean in interface DatatypeCoder
      Parameters:
      val - value to encode
      Returns:
      true as 1, false as 0.
    • decodeLocalTime

      public LocalTime decodeLocalTime(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalTime from buf from the first 4 bytes.
      Specified by:
      decodeLocalTime in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 4 bytes
      Returns:
      LocalTime decoded from buf, or null if buf is null
    • decodeLocalTime

      public LocalTime decodeLocalTime(byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalTime from buf from the 4 bytes starting at off.
      Specified by:
      decodeLocalTime in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 4 bytes from off, never null
      off - offset of the time value in buf
      Returns:
      LocalTime decoded from buf
    • encodeLocalTime

      public byte[] encodeLocalTime(LocalTime val)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalTime as a byte array of 4 bytes.
      Specified by:
      encodeLocalTime in interface DatatypeCoder
      Parameters:
      val - value to encode
      Returns:
      val encoded as a byte array, or null if val is null
    • encodeLocalTime

      public void encodeLocalTime(LocalTime val, byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalTime to a byte array, requiring 4 bytes.
      Specified by:
      encodeLocalTime in interface DatatypeCoder
      Parameters:
      val - value to encode
      buf - byte array with at least 4 bytes starting at off, never null
      off - offset of the time value in buf
    • decodeLocalDate

      public LocalDate decodeLocalDate(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalDate from buf from the first 4 bytes.
      Specified by:
      decodeLocalDate in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 4 bytes
      Returns:
      LocalDate decoded from buf, or null if buf is null
    • decodeLocalDate

      public LocalDate decodeLocalDate(byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalDate from buf from the 4 bytes starting at off.
      Specified by:
      decodeLocalDate in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 4 bytes from off, never null
      off - offset of the time value in buf
      Returns:
      LocalDate decoded from buf
    • encodeLocalDate

      public byte[] encodeLocalDate(LocalDate val)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalDate as a byte array of 4 bytes.
      Specified by:
      encodeLocalDate in interface DatatypeCoder
      Parameters:
      val - value to encode
      Returns:
      val encoded as a byte array, or null if val is null
    • encodeLocalDate

      public void encodeLocalDate(LocalDate val, byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalDate to a byte array, requiring 4 bytes.
      Specified by:
      encodeLocalDate in interface DatatypeCoder
      Parameters:
      val - value to encode
      buf - byte array with at least 4 bytes starting at off, never null
      off - offset of the date value in buf
    • decodeLocalDateTime

      public LocalDateTime decodeLocalDateTime(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalDateTime from buf from the first 8 bytes.
      Specified by:
      decodeLocalDateTime in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 8 bytes
      Returns:
      LocalDateTime decoded from buf, or null if buf is null
    • decodeLocalDateTime

      public LocalDateTime decodeLocalDateTime(byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Decode java.time.LocalDateTime from buf from the 8 bytes starting at off.
      Specified by:
      decodeLocalDateTime in interface DatatypeCoder
      Parameters:
      buf - (expected) at least 8 bytes from off, never null
      off - offset of the datetime value in buf
      Returns:
      LocalDateTime decoded from buf
    • encodeLocalDateTime

      public byte[] encodeLocalDateTime(LocalDateTime val)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalDateTime as a byte array of 8 bytes.
      Specified by:
      encodeLocalDateTime in interface DatatypeCoder
      Parameters:
      val - value to encode
      Returns:
      val encoded as a byte array, or null if val is null
    • encodeLocalDateTime

      public void encodeLocalDateTime(LocalDateTime val, byte[] buf, int off)
      Description copied from interface: DatatypeCoder
      Encode a java.time.LocalDateTime to a byte array, requiring 8 bytes.
      Specified by:
      encodeLocalDateTime in interface DatatypeCoder
      Parameters:
      val - value to encode
      buf - byte array with at least 8 bytes starting at off, never null
      off - offset of the datetime value in buf
    • networkOrder

      protected byte[] networkOrder(byte[] buf)
      Returns buf as an array in network byte order.

      If this is a big-endian coder, buf should be returned as-is. Otherwise, a new array must be returned with the bytes reversed, as the operation must be repeatable on the same original byte array.

      Parameters:
      buf - byte array
      Returns:
      new byte array in network byte order (or buf if this a big-endian coder, so the array is already network byte order)
    • decodeDecimal64

      public Decimal64 decodeDecimal64(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decodes a decimal64 from a byte array of 8 bytes.
      Specified by:
      decodeDecimal64 in interface DatatypeCoder
      Parameters:
      buf - data to decode (expects exactly 8 bytes)
      Returns:
      Decimal64 decoded from buf, or null if buf is null
    • encodeDecimal64

      public byte[] encodeDecimal64(Decimal64 val)
      Description copied from interface: DatatypeCoder
      Encodes a decimal64 as a byte array of 8 bytes.
      Specified by:
      encodeDecimal64 in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array, or null if val is null
    • decodeDecimal128

      public Decimal128 decodeDecimal128(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decodes a decimal128 from a byte array of 16 bytes.
      Specified by:
      decodeDecimal128 in interface DatatypeCoder
      Parameters:
      buf - data to decode (expects exactly 16 bytes)
      Returns:
      Decimal128 decoded from buf, or null if buf is null
    • encodeDecimal128

      public byte[] encodeDecimal128(Decimal128 val)
      Description copied from interface: DatatypeCoder
      Encodes a decimal128 as a byte array of 16 bytes.
      Specified by:
      encodeDecimal128 in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array, or null if val is null
    • decodeInt128

      public BigInteger decodeInt128(byte[] buf)
      Description copied from interface: DatatypeCoder
      Decodes a BigInteger from a byte array of 16 bytes (int128 format).
      Specified by:
      decodeInt128 in interface DatatypeCoder
      Parameters:
      buf - data to decode (expects exactly 16 bytes)
      Returns:
      BigInteger decoded from buf, or null if val is null
    • encodeInt128

      public byte[] encodeInt128(BigInteger val)
      Description copied from interface: DatatypeCoder
      Encodes a BigInteger as a byte array of 16 bytes (int128 format).

      The implementation expects to be passed a value that fits in 16 bytes. If a larger value is passed, an IllegalArgumentException is thrown.

      Specified by:
      encodeInt128 in interface DatatypeCoder
      Parameters:
      val - value to be encoded
      Returns:
      val encoded as a byte array, or null if val is null
    • getEncodingFactory

      public final IEncodingFactory getEncodingFactory()
      Specified by:
      getEncodingFactory in interface DatatypeCoder
      Returns:
      encoding factory.
    • getEncodingDefinition

      public final EncodingDefinition getEncodingDefinition()
      Specified by:
      getEncodingDefinition in interface DatatypeCoder
      Returns:
      encoding definition used by this datatype coder for string conversions.
    • getEncoding

      public final Encoding getEncoding()
      Specified by:
      getEncoding in interface DatatypeCoder
      Returns:
      encoding used by this datatype coder for string conversions.
    • forEncodingDefinition

      public final DatatypeCoder forEncodingDefinition(EncodingDefinition encodingDefinition)
      Description copied from interface: DatatypeCoder
      Return a derived datatype coder that applies the supplied encoding definition for string conversions.
      Specified by:
      forEncodingDefinition in interface DatatypeCoder
      Parameters:
      encodingDefinition - encoding definition
      Returns:
      derived datatype coder (this instance, if encoding definition is the same)
    • unwrap

      public DatatypeCoder unwrap()
      Description copied from interface: DatatypeCoder
      Unwrap this datatype coder to its parent (or itself).
      Specified by:
      unwrap in interface DatatypeCoder
      Returns:
      parent of this datatype code, or itself if it has no parent.
    • equals

      public final boolean equals(Object o)
      Description copied from interface: DatatypeCoder

      Equality: same basic type (i.e.: wire protocol/JNA type + endianness) and same encoding definition.

      This does not need to take into account the encoding factory, as usage should be limited to datatype coders derived from the same connection.

      Specified by:
      equals in interface DatatypeCoder
      Overrides:
      equals in class Object
      Parameters:
      o - object to compare to
      Returns:
      true if other is an equivalent datatype coder.
    • hashCode

      public final int hashCode()
      Specified by:
      hashCode in interface DatatypeCoder
      Overrides:
      hashCode in class Object