- 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 ReaderCreates a reader wrapping an input stream.final WritercreateWriter(OutputStream out) Creates a writer wrapping an input stream.booleandecodeBoolean(byte[] buf) Decode a boolean frombuffrom 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.doubledecodeDouble(byte[] buf) Decode a double value frombuffrom the first 8 bytes.floatdecodeFloat(byte[] buf) Decode a float value frombuffrom the first 4 bytes.intdecodeInt(byte[] buf) Decode an int value frombuffrom the first 4 bytes.intdecodeInt(byte[] buf, int off) Decode an int value frombufstarting at offsetofffor 4 bytes.decodeInt128(byte[] buf) Decodes a BigInteger from a byte array of 16 bytes (int128 format).decodeLocalDate(byte[] buf) Decodejava.time.LocalDatefrombuffrom the first 4 bytes.decodeLocalDate(byte[] buf, int off) Decodejava.time.LocalDatefrombuffrom the 4 bytes starting atoff.decodeLocalDateTime(byte[] buf) Decodejava.time.LocalDateTimefrombuffrom the first 8 bytes.decodeLocalDateTime(byte[] buf, int off) Decodejava.time.LocalDateTimefrombuffrom the 8 bytes starting atoff.decodeLocalTime(byte[] buf) Decodejava.time.LocalTimefrombuffrom the first 4 bytes.decodeLocalTime(byte[] buf, int off) Decodejava.time.LocalTimefrombuffrom the 4 bytes starting atoff.longdecodeLong(byte[] buf) Decode a long value frombuffrom the first 8 bytes.shortdecodeShort(byte[] buf) Decode a short value frombuffrom the firstDatatypeCoder.sizeOfShort()bytes.shortdecodeShort(byte[] buf, int off) final StringdecodeString(byte[] buf) Decode aStringfrombufusing 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.voidencodeInt(int val, byte[] buf, int off) Encode an int value intobufstarting at indexofffor 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.LocalDateas a byte array of 4 bytes.voidencodeLocalDate(LocalDate val, byte[] buf, int off) Encode ajava.time.LocalDateto a byte array, requiring 4 bytes.byte[]Encode ajava.time.LocalDateTimeas a byte array of 8 bytes.voidencodeLocalDateTime(LocalDateTime val, byte[] buf, int off) Encode ajava.time.LocalDateTimeto a byte array, requiring 8 bytes.byte[]encodeLocalTime(LocalTime val) Encode ajava.time.LocalTimeas a byte array of 4 bytes.voidencodeLocalTime(LocalTime val, byte[] buf, int off) Encode ajava.time.LocalTimeto 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().voidencodeShort(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 aStringvalue as a byte array using the encoding of this datatype coder.final booleanfinal DatatypeCoderforEncodingDefinition(EncodingDefinition encodingDefinition) Return a derived datatype coder that applies the supplied encoding definition for string conversions.static DefaultDatatypeCoderforEncodingFactory(IEncodingFactory encodingFactory) Returns an instance ofDefaultDatatypeCoderfor an encoding factory.final Encodingfinal EncodingDefinitionfinal IEncodingFactoryfinal inthashCode()protected byte[]networkOrder(byte[] buf) Returnsbufas an array in network byte order.intThe 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 ofDefaultDatatypeCoderfor an encoding factory.- Parameters:
encodingFactory- encoding factory- Returns:
- datatype coder, this might be a cached instance
-
sizeOfShort
public int sizeOfShort()Description copied from interface:DatatypeCoderThe size of an encoded short in this data type coder.- Specified by:
sizeOfShortin interfaceDatatypeCoder- Returns:
- size of an encoded short (either
2or4bytes)
-
encodeShort
public byte[] encodeShort(short val) Description copied from interface:DatatypeCoderEncode a short value as a byte array of lengthDatatypeCoder.sizeOfShort().- Specified by:
encodeShortin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array
-
encodeShort
public byte[] encodeShort(int val) Description copied from interface:DatatypeCoderEncode a short value as a byte array of lengthDatatypeCoder.sizeOfShort().- Specified by:
encodeShortin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array
-
encodeShort
public void encodeShort(int val, byte[] buf, int off) Description copied from interface:DatatypeCoderEncode a short value intobufstarting at offsetoffforDatatypeCoder.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:
encodeShortin interfaceDatatypeCoder- Parameters:
val- value to be encodedbuf- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort()), nevernulloff- offset to start encoding
-
decodeShort
public short decodeShort(byte[] buf) Description copied from interface:DatatypeCoderDecode a short value frombuffrom the firstDatatypeCoder.sizeOfShort()bytes.- Specified by:
decodeShortin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort())- Returns:
- short value from
buf, or0whenbufisnull
-
decodeShort
public short decodeShort(byte[] buf, int off) Description copied from interface:DatatypeCoder- Specified by:
decodeShortin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size (warning: this is datatype coder specific, seeDatatypeCoder.sizeOfShort()), nevernulloff- offset to start decoding- Returns:
- short value from
buf
-
encodeInt
public byte[] encodeInt(int val) Description copied from interface:DatatypeCoderEncode an int value as a byte array of 4 bytes.- Specified by:
encodeIntin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array, or0whenbufisnull
-
encodeInt
public void encodeInt(int val, byte[] buf, int off) Description copied from interface:DatatypeCoderEncode an int value intobufstarting at indexofffor 4 bytes.- Specified by:
encodeIntin interfaceDatatypeCoder- Parameters:
val- value to be encodedbuf- byte array of sufficient size, nevernulloff- offset to start encoding
-
decodeInt
public int decodeInt(byte[] buf) Description copied from interface:DatatypeCoderDecode an int value frombuffrom the first 4 bytes.- Specified by:
decodeIntin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size- Returns:
- int value decoded from
buf, or0whenbufisnull
-
decodeInt
public int decodeInt(byte[] buf, int off) Description copied from interface:DatatypeCoderDecode an int value frombufstarting at offsetofffor 4 bytes.- Specified by:
decodeIntin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size, nevernulloff- offset to start decoding- Returns:
- int value decoded from
buf
-
encodeLong
public byte[] encodeLong(long val) Description copied from interface:DatatypeCoderEncode a long value as a byte array of 8 bytes.- Specified by:
encodeLongin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array
-
decodeLong
public long decodeLong(byte[] buf) Description copied from interface:DatatypeCoderDecode a long value frombuffrom the first 8 bytes.- Specified by:
decodeLongin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size, or0whenbufisnull- Returns:
- long value decoded from
buf
-
encodeFloat
public byte[] encodeFloat(float val) Description copied from interface:DatatypeCoderEncode a float value as a byte array of 4 bytes.- Specified by:
encodeFloatin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array
-
decodeFloat
public float decodeFloat(byte[] buf) Description copied from interface:DatatypeCoderDecode a float value frombuffrom the first 4 bytes.- Specified by:
decodeFloatin interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size, or0whenbufisnull- Returns:
- float value decoded from
buf
-
encodeDouble
public byte[] encodeDouble(double val) Description copied from interface:DatatypeCoderEncode a double value as a byte array of 8 bytes.- Specified by:
encodeDoublein interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array
-
decodeDouble
public double decodeDouble(byte[] buf) Description copied from interface:DatatypeCoderDecode a double value frombuffrom the first 8 bytes.- Specified by:
decodeDoublein interfaceDatatypeCoder- Parameters:
buf- byte array of sufficient size, or0whenbufisnull- Returns:
- double value decoded from
buf
-
encodeString
Description copied from interface:DatatypeCoderEncode aStringvalue as a byte array using the encoding of this datatype coder.- Specified by:
encodeStringin interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array, ornullifvalisnull
-
createWriter
Description copied from interface:DatatypeCoderCreates a writer wrapping an input stream.- Specified by:
createWriterin interfaceDatatypeCoder- Parameters:
out- output stream- Returns:
- writer applying the encoding of this datatype when writing
-
decodeString
Description copied from interface:DatatypeCoderDecode aStringfrombufusing the encoding of this datatype coder.- Specified by:
decodeStringin interfaceDatatypeCoder- Parameters:
buf- byte array to be decoded- Returns:
Stringdecoded frombuf, ornullifbufisnull
-
createReader
Description copied from interface:DatatypeCoderCreates a reader wrapping an input stream.- Specified by:
createReaderin 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:DatatypeCoderDecode a boolean frombuffrom the first byte.- Specified by:
decodeBooleanin interfaceDatatypeCoder- Parameters:
buf- (expected) 1 bytes- Returns:
falsewhen 0,truefor all other values, orfalseifbufisnull
-
encodeBoolean
public byte[] encodeBoolean(boolean val) Description copied from interface:DatatypeCoderEncodes boolean as a byte array of 1 byte.- Specified by:
encodeBooleanin interfaceDatatypeCoder- Parameters:
val- value to encode- Returns:
trueas 1,falseas 0.
-
decodeLocalTime
Description copied from interface:DatatypeCoderDecodejava.time.LocalTimefrombuffrom the first 4 bytes.- Specified by:
decodeLocalTimein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 4 bytes- Returns:
LocalTimedecoded frombuf, ornullifbufisnull
-
decodeLocalTime
Description copied from interface:DatatypeCoderDecodejava.time.LocalTimefrombuffrom the 4 bytes starting atoff.- Specified by:
decodeLocalTimein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 4 bytes fromoff, nevernulloff- offset of the time value inbuf- Returns:
LocalTimedecoded frombuf
-
encodeLocalTime
Description copied from interface:DatatypeCoderEncode ajava.time.LocalTimeas a byte array of 4 bytes.- Specified by:
encodeLocalTimein interfaceDatatypeCoder- Parameters:
val- value to encode- Returns:
valencoded as a byte array, ornullifvalisnull
-
encodeLocalTime
Description copied from interface:DatatypeCoderEncode ajava.time.LocalTimeto a byte array, requiring 4 bytes.- Specified by:
encodeLocalTimein interfaceDatatypeCoder- Parameters:
val- value to encodebuf- byte array with at least 4 bytes starting atoff, nevernulloff- offset of the time value inbuf
-
decodeLocalDate
Description copied from interface:DatatypeCoderDecodejava.time.LocalDatefrombuffrom the first 4 bytes.- Specified by:
decodeLocalDatein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 4 bytes- Returns:
LocalDatedecoded frombuf, ornullifbufisnull
-
decodeLocalDate
Description copied from interface:DatatypeCoderDecodejava.time.LocalDatefrombuffrom the 4 bytes starting atoff.- Specified by:
decodeLocalDatein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 4 bytes fromoff, nevernulloff- offset of the time value inbuf- Returns:
LocalDatedecoded frombuf
-
encodeLocalDate
Description copied from interface:DatatypeCoderEncode ajava.time.LocalDateas a byte array of 4 bytes.- Specified by:
encodeLocalDatein interfaceDatatypeCoder- Parameters:
val- value to encode- Returns:
valencoded as a byte array, ornullifvalisnull
-
encodeLocalDate
Description copied from interface:DatatypeCoderEncode ajava.time.LocalDateto a byte array, requiring 4 bytes.- Specified by:
encodeLocalDatein interfaceDatatypeCoder- Parameters:
val- value to encodebuf- byte array with at least 4 bytes starting atoff, nevernulloff- offset of the date value inbuf
-
decodeLocalDateTime
Description copied from interface:DatatypeCoderDecodejava.time.LocalDateTimefrombuffrom the first 8 bytes.- Specified by:
decodeLocalDateTimein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 8 bytes- Returns:
LocalDateTimedecoded frombuf, ornullifbufisnull
-
decodeLocalDateTime
Description copied from interface:DatatypeCoderDecodejava.time.LocalDateTimefrombuffrom the 8 bytes starting atoff.- Specified by:
decodeLocalDateTimein interfaceDatatypeCoder- Parameters:
buf- (expected) at least 8 bytes fromoff, nevernulloff- offset of the datetime value inbuf- Returns:
LocalDateTimedecoded frombuf
-
encodeLocalDateTime
Description copied from interface:DatatypeCoderEncode ajava.time.LocalDateTimeas a byte array of 8 bytes.- Specified by:
encodeLocalDateTimein interfaceDatatypeCoder- Parameters:
val- value to encode- Returns:
valencoded as a byte array, ornullifvalisnull
-
encodeLocalDateTime
Description copied from interface:DatatypeCoderEncode ajava.time.LocalDateTimeto a byte array, requiring 8 bytes.- Specified by:
encodeLocalDateTimein interfaceDatatypeCoder- Parameters:
val- value to encodebuf- byte array with at least 8 bytes starting atoff, nevernulloff- offset of the datetime value inbuf
-
networkOrder
protected byte[] networkOrder(byte[] buf) Returnsbufas an array in network byte order.If this is a big-endian coder,
bufshould 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
bufif this a big-endian coder, so the array is already network byte order)
-
decodeDecimal64
Description copied from interface:DatatypeCoderDecodes a decimal64 from a byte array of 8 bytes.- Specified by:
decodeDecimal64in interfaceDatatypeCoder- Parameters:
buf- data to decode (expects exactly 8 bytes)- Returns:
- Decimal64 decoded from
buf, ornullifbufisnull
-
encodeDecimal64
Description copied from interface:DatatypeCoderEncodes a decimal64 as a byte array of 8 bytes.- Specified by:
encodeDecimal64in interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array, ornullifvalisnull
-
decodeDecimal128
Description copied from interface:DatatypeCoderDecodes a decimal128 from a byte array of 16 bytes.- Specified by:
decodeDecimal128in interfaceDatatypeCoder- Parameters:
buf- data to decode (expects exactly 16 bytes)- Returns:
- Decimal128 decoded from
buf, ornullifbufisnull
-
encodeDecimal128
Description copied from interface:DatatypeCoderEncodes a decimal128 as a byte array of 16 bytes.- Specified by:
encodeDecimal128in interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array, ornullifvalisnull
-
decodeInt128
Description copied from interface:DatatypeCoderDecodes a BigInteger from a byte array of 16 bytes (int128 format).- Specified by:
decodeInt128in interfaceDatatypeCoder- Parameters:
buf- data to decode (expects exactly 16 bytes)- Returns:
- BigInteger decoded from
buf, ornullifvalisnull
-
encodeInt128
Description copied from interface:DatatypeCoderEncodes 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
IllegalArgumentExceptionis thrown.- Specified by:
encodeInt128in interfaceDatatypeCoder- Parameters:
val- value to be encoded- Returns:
valencoded as a byte array, ornullifvalisnull
-
getEncodingFactory
- Specified by:
getEncodingFactoryin interfaceDatatypeCoder- Returns:
- encoding factory.
-
getEncodingDefinition
- Specified by:
getEncodingDefinitionin interfaceDatatypeCoder- Returns:
- encoding definition used by this datatype coder for string conversions.
-
getEncoding
- Specified by:
getEncodingin interfaceDatatypeCoder- Returns:
- encoding used by this datatype coder for string conversions.
-
forEncodingDefinition
Description copied from interface:DatatypeCoderReturn a derived datatype coder that applies the supplied encoding definition for string conversions.- Specified by:
forEncodingDefinitionin interfaceDatatypeCoder- Parameters:
encodingDefinition- encoding definition- Returns:
- derived datatype coder (this instance, if encoding definition is the same)
-
unwrap
Description copied from interface:DatatypeCoderUnwrap this datatype coder to its parent (or itself).- Specified by:
unwrapin interfaceDatatypeCoder- Returns:
- parent of this datatype code, or itself if it has no parent.
-
equals
Description copied from interface:DatatypeCoderEquality: 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:
equalsin interfaceDatatypeCoder- Overrides:
equalsin classObject- Parameters:
o- object to compare to- Returns:
trueif other is an equivalent datatype coder.
-
hashCode
public final int hashCode()- Specified by:
hashCodein interfaceDatatypeCoder- Overrides:
hashCodein classObject
-