- All Implemented Interfaces:
DatatypeCoder
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
-
Field Summary
Fields inherited from interface org.firebirdsql.gds.ng.DatatypeCoder
FRACTIONS_PER_HOUR, FRACTIONS_PER_MILLISECOND, FRACTIONS_PER_MINUTE, FRACTIONS_PER_SECOND, NANOSECONDS_PER_FRACTION
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultDatatypeCoder
(IEncodingFactory encodingFactory) Creates a default datatype coder for the wire protocol. -
Method Summary
Modifier and TypeMethodDescriptionfinal Reader
Creates a reader wrapping an input stream.final Writer
createWriter
(OutputStream out) Creates a writer wrapping an input stream.boolean
decodeBoolean
(byte[] buf) Decode a boolean frombuf
from the first byte.decodeDecimal128
(byte[] buf) Decodes a decimal128 from a byte array of 16 bytes.decodeDecimal64
(byte[] buf) Decodes a decimal64 from a byte array of 8 bytes.double
decodeDouble
(byte[] buf) Decode a double value frombuf
from the first 8 bytes.float
decodeFloat
(byte[] buf) Decode a float value frombuf
from the first 4 bytes.int
decodeInt
(byte[] buf) Decode an int value frombuf
from the first 4 bytes.int
decodeInt
(byte[] buf, int off) Decode an int value frombuf
starting at offsetoff
for 4 bytes.decodeInt128
(byte[] buf) Decodes a BigInteger from a byte array of 16 bytes (int128 format).decodeLocalDate
(byte[] buf) Decodejava.time.LocalDate
frombuf
from the first 4 bytes.decodeLocalDate
(byte[] buf, int off) Decodejava.time.LocalDate
frombuf
from the 4 bytes starting atoff
.decodeLocalDateTime
(byte[] buf) Decodejava.time.LocalDateTime
frombuf
from the first 8 bytes.decodeLocalDateTime
(byte[] buf, int off) Decodejava.time.LocalDateTime
frombuf
from the 8 bytes starting atoff
.decodeLocalTime
(byte[] buf) Decodejava.time.LocalTime
frombuf
from the first 4 bytes.decodeLocalTime
(byte[] buf, int off) Decodejava.time.LocalTime
frombuf
from the 4 bytes starting atoff
.long
decodeLong
(byte[] buf) Decode a long value frombuf
from the first 8 bytes.short
decodeShort
(byte[] buf) Decode a short value frombuf
from the firstDatatypeCoder.sizeOfShort()
bytes.short
decodeShort
(byte[] buf, int off) final String
decodeString
(byte[] buf) Decode aString
frombuf
using the encoding of this datatype coder.byte[]
encodeBoolean
(boolean val) Encodes boolean as a byte array of 1 byte.byte[]
Encodes a decimal128 as a byte array of 16 bytes.byte[]
encodeDecimal64
(Decimal64 val) Encodes a decimal64 as a byte array of 8 bytes.byte[]
encodeDouble
(double val) Encode a double value as a byte array of 8 bytes.byte[]
encodeFloat
(float val) Encode a float value as a byte array of 4 bytes.byte[]
encodeInt
(int val) Encode an int value as a byte array of 4 bytes.void
encodeInt
(int val, byte[] buf, int off) Encode an int value intobuf
starting at indexoff
for 4 bytes.byte[]
encodeInt128
(BigInteger val) Encodes a BigInteger as a byte array of 16 bytes (int128 format).byte[]
encodeLocalDate
(LocalDate val) Encode ajava.time.LocalDate
as a byte array of 4 bytes.void
encodeLocalDate
(LocalDate val, byte[] buf, int off) Encode ajava.time.LocalDate
to a byte array, requiring 4 bytes.byte[]
Encode ajava.time.LocalDateTime
as a byte array of 8 bytes.void
encodeLocalDateTime
(LocalDateTime val, byte[] buf, int off) Encode ajava.time.LocalDateTime
to a byte array, requiring 8 bytes.byte[]
encodeLocalTime
(LocalTime val) Encode ajava.time.LocalTime
as a byte array of 4 bytes.void
encodeLocalTime
(LocalTime val, byte[] buf, int off) Encode ajava.time.LocalTime
to a byte array, requiring 4 bytes.byte[]
encodeLong
(long val) Encode a long value as a byte array of 8 bytes.byte[]
encodeShort
(int val) Encode a short value as a byte array of lengthDatatypeCoder.sizeOfShort()
.void
encodeShort
(int val, byte[] buf, int off) byte[]
encodeShort
(short val) Encode a short value as a byte array of lengthDatatypeCoder.sizeOfShort()
.final byte[]
encodeString
(String val) Encode aString
value as a byte array using the encoding of this datatype coder.final boolean
final DatatypeCoder
forEncodingDefinition
(EncodingDefinition encodingDefinition) Return a derived datatype coder that applies the supplied encoding definition for string conversions.static DefaultDatatypeCoder
forEncodingFactory
(IEncodingFactory encodingFactory) Returns an instance ofDefaultDatatypeCoder
for an encoding factory.final Encoding
final EncodingDefinition
final IEncodingFactory
final int
hashCode()
protected byte[]
networkOrder
(byte[] buf) Returnsbuf
as an array in network byte order.int
The size of an encoded short in this data type coder.unwrap()
Unwrap this datatype coder to its parent (or itself).
-
Constructor Details
-
DefaultDatatypeCoder
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
Returns an instance ofDefaultDatatypeCoder
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 interfaceDatatypeCoder
- Returns:
- size of an encoded short (either
2
or4
bytes)
-
encodeShort
public byte[] encodeShort(short val) Description copied from interface:DatatypeCoder
Encode a short value as a byte array of lengthDatatypeCoder.sizeOfShort()
.- Specified by:
encodeShort
in interfaceDatatypeCoder
- 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 lengthDatatypeCoder.sizeOfShort()
.- Specified by:
encodeShort
in interfaceDatatypeCoder
- 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 intobuf
starting at offsetoff
forDatatypeCoder.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 interfaceDatatypeCoder
- Parameters:
val
- value to be encodedbuf
- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort()
), nevernull
off
- offset to start encoding
-
decodeShort
public short decodeShort(byte[] buf) Description copied from interface:DatatypeCoder
Decode a short value frombuf
from the firstDatatypeCoder.sizeOfShort()
bytes.- Specified by:
decodeShort
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort()
)- Returns:
- short value from
buf
, or0
whenbuf
isnull
-
decodeShort
public short decodeShort(byte[] buf, int off) Description copied from interface:DatatypeCoder
- Specified by:
decodeShort
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort()
), nevernull
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 interfaceDatatypeCoder
- Parameters:
val
- value to be encoded- Returns:
val
encoded as a byte array, or0
whenbuf
isnull
-
encodeInt
public void encodeInt(int val, byte[] buf, int off) Description copied from interface:DatatypeCoder
Encode an int value intobuf
starting at indexoff
for 4 bytes.- Specified by:
encodeInt
in interfaceDatatypeCoder
- Parameters:
val
- value to be encodedbuf
- byte array of sufficient size, nevernull
off
- offset to start encoding
-
decodeInt
public int decodeInt(byte[] buf) Description copied from interface:DatatypeCoder
Decode an int value frombuf
from the first 4 bytes.- Specified by:
decodeInt
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size- Returns:
- int value decoded from
buf
, or0
whenbuf
isnull
-
decodeInt
public int decodeInt(byte[] buf, int off) Description copied from interface:DatatypeCoder
Decode an int value frombuf
starting at offsetoff
for 4 bytes.- Specified by:
decodeInt
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size, nevernull
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 interfaceDatatypeCoder
- 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 frombuf
from the first 8 bytes.- Specified by:
decodeLong
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size, or0
whenbuf
isnull
- 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 interfaceDatatypeCoder
- 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 frombuf
from the first 4 bytes.- Specified by:
decodeFloat
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size, or0
whenbuf
isnull
- 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 interfaceDatatypeCoder
- 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 frombuf
from the first 8 bytes.- Specified by:
decodeDouble
in interfaceDatatypeCoder
- Parameters:
buf
- byte array of sufficient size, or0
whenbuf
isnull
- Returns:
- double value decoded from
buf
-
encodeString
Description copied from interface:DatatypeCoder
Encode aString
value as a byte array using the encoding of this datatype coder.- Specified by:
encodeString
in interfaceDatatypeCoder
- Parameters:
val
- value to be encoded- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
createWriter
Description copied from interface:DatatypeCoder
Creates a writer wrapping an input stream.- Specified by:
createWriter
in interfaceDatatypeCoder
- Parameters:
out
- output stream- Returns:
- writer applying the encoding of this datatype when writing
-
decodeString
Description copied from interface:DatatypeCoder
Decode aString
frombuf
using the encoding of this datatype coder.- Specified by:
decodeString
in interfaceDatatypeCoder
- Parameters:
buf
- byte array to be decoded- Returns:
String
decoded frombuf
, ornull
ifbuf
isnull
-
createReader
Description copied from interface:DatatypeCoder
Creates a reader wrapping an input stream.- Specified by:
createReader
in interfaceDatatypeCoder
- 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 frombuf
from the first byte.- Specified by:
decodeBoolean
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) 1 bytes- Returns:
false
when 0,true
for all other values, orfalse
ifbuf
isnull
-
encodeBoolean
public byte[] encodeBoolean(boolean val) Description copied from interface:DatatypeCoder
Encodes boolean as a byte array of 1 byte.- Specified by:
encodeBoolean
in interfaceDatatypeCoder
- Parameters:
val
- value to encode- Returns:
true
as 1,false
as 0.
-
decodeLocalTime
Description copied from interface:DatatypeCoder
Decodejava.time.LocalTime
frombuf
from the first 4 bytes.- Specified by:
decodeLocalTime
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 4 bytes- Returns:
LocalTime
decoded frombuf
, ornull
ifbuf
isnull
-
decodeLocalTime
Description copied from interface:DatatypeCoder
Decodejava.time.LocalTime
frombuf
from the 4 bytes starting atoff
.- Specified by:
decodeLocalTime
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 4 bytes fromoff
, nevernull
off
- offset of the time value inbuf
- Returns:
LocalTime
decoded frombuf
-
encodeLocalTime
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalTime
as a byte array of 4 bytes.- Specified by:
encodeLocalTime
in interfaceDatatypeCoder
- Parameters:
val
- value to encode- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
encodeLocalTime
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalTime
to a byte array, requiring 4 bytes.- Specified by:
encodeLocalTime
in interfaceDatatypeCoder
- Parameters:
val
- value to encodebuf
- byte array with at least 4 bytes starting atoff
, nevernull
off
- offset of the time value inbuf
-
decodeLocalDate
Description copied from interface:DatatypeCoder
Decodejava.time.LocalDate
frombuf
from the first 4 bytes.- Specified by:
decodeLocalDate
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 4 bytes- Returns:
LocalDate
decoded frombuf
, ornull
ifbuf
isnull
-
decodeLocalDate
Description copied from interface:DatatypeCoder
Decodejava.time.LocalDate
frombuf
from the 4 bytes starting atoff
.- Specified by:
decodeLocalDate
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 4 bytes fromoff
, nevernull
off
- offset of the time value inbuf
- Returns:
LocalDate
decoded frombuf
-
encodeLocalDate
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalDate
as a byte array of 4 bytes.- Specified by:
encodeLocalDate
in interfaceDatatypeCoder
- Parameters:
val
- value to encode- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
encodeLocalDate
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalDate
to a byte array, requiring 4 bytes.- Specified by:
encodeLocalDate
in interfaceDatatypeCoder
- Parameters:
val
- value to encodebuf
- byte array with at least 4 bytes starting atoff
, nevernull
off
- offset of the date value inbuf
-
decodeLocalDateTime
Description copied from interface:DatatypeCoder
Decodejava.time.LocalDateTime
frombuf
from the first 8 bytes.- Specified by:
decodeLocalDateTime
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 8 bytes- Returns:
LocalDateTime
decoded frombuf
, ornull
ifbuf
isnull
-
decodeLocalDateTime
Description copied from interface:DatatypeCoder
Decodejava.time.LocalDateTime
frombuf
from the 8 bytes starting atoff
.- Specified by:
decodeLocalDateTime
in interfaceDatatypeCoder
- Parameters:
buf
- (expected) at least 8 bytes fromoff
, nevernull
off
- offset of the datetime value inbuf
- Returns:
LocalDateTime
decoded frombuf
-
encodeLocalDateTime
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalDateTime
as a byte array of 8 bytes.- Specified by:
encodeLocalDateTime
in interfaceDatatypeCoder
- Parameters:
val
- value to encode- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
encodeLocalDateTime
Description copied from interface:DatatypeCoder
Encode ajava.time.LocalDateTime
to a byte array, requiring 8 bytes.- Specified by:
encodeLocalDateTime
in interfaceDatatypeCoder
- Parameters:
val
- value to encodebuf
- byte array with at least 8 bytes starting atoff
, nevernull
off
- offset of the datetime value inbuf
-
networkOrder
protected byte[] networkOrder(byte[] buf) Returnsbuf
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
Description copied from interface:DatatypeCoder
Decodes a decimal64 from a byte array of 8 bytes.- Specified by:
decodeDecimal64
in interfaceDatatypeCoder
- Parameters:
buf
- data to decode (expects exactly 8 bytes)- Returns:
- Decimal64 decoded from
buf
, ornull
ifbuf
isnull
-
encodeDecimal64
Description copied from interface:DatatypeCoder
Encodes a decimal64 as a byte array of 8 bytes.- Specified by:
encodeDecimal64
in interfaceDatatypeCoder
- Parameters:
val
- value to be encoded- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
decodeDecimal128
Description copied from interface:DatatypeCoder
Decodes a decimal128 from a byte array of 16 bytes.- Specified by:
decodeDecimal128
in interfaceDatatypeCoder
- Parameters:
buf
- data to decode (expects exactly 16 bytes)- Returns:
- Decimal128 decoded from
buf
, ornull
ifbuf
isnull
-
encodeDecimal128
Description copied from interface:DatatypeCoder
Encodes a decimal128 as a byte array of 16 bytes.- Specified by:
encodeDecimal128
in interfaceDatatypeCoder
- Parameters:
val
- value to be encoded- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
decodeInt128
Description copied from interface:DatatypeCoder
Decodes a BigInteger from a byte array of 16 bytes (int128 format).- Specified by:
decodeInt128
in interfaceDatatypeCoder
- Parameters:
buf
- data to decode (expects exactly 16 bytes)- Returns:
- BigInteger decoded from
buf
, ornull
ifval
isnull
-
encodeInt128
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 interfaceDatatypeCoder
- Parameters:
val
- value to be encoded- Returns:
val
encoded as a byte array, ornull
ifval
isnull
-
getEncodingFactory
- Specified by:
getEncodingFactory
in interfaceDatatypeCoder
- Returns:
- encoding factory.
-
getEncodingDefinition
- Specified by:
getEncodingDefinition
in interfaceDatatypeCoder
- Returns:
- encoding definition used by this datatype coder for string conversions.
-
getEncoding
- Specified by:
getEncoding
in interfaceDatatypeCoder
- Returns:
- encoding used by this datatype coder for string conversions.
-
forEncodingDefinition
Description copied from interface:DatatypeCoder
Return a derived datatype coder that applies the supplied encoding definition for string conversions.- Specified by:
forEncodingDefinition
in interfaceDatatypeCoder
- Parameters:
encodingDefinition
- encoding definition- Returns:
- derived datatype coder (this instance, if encoding definition is the same)
-
unwrap
Description copied from interface:DatatypeCoder
Unwrap this datatype coder to its parent (or itself).- Specified by:
unwrap
in interfaceDatatypeCoder
- Returns:
- parent of this datatype code, or itself if it has no parent.
-
equals
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 interfaceDatatypeCoder
- Overrides:
equals
in classObject
- Parameters:
o
- object to compare to- Returns:
true
if other is an equivalent datatype coder.
-
hashCode
public final int hashCode()- Specified by:
hashCode
in interfaceDatatypeCoder
- Overrides:
hashCode
in classObject
-