Class BinaryNumberCodec<V extends Number>
- Type Parameters:
V- theNumbertype this codec handles
- All Implemented Interfaces:
Codec<V>
Numbers encoded as fixed-length binary, big-endian unless
withOrder(ByteOrder) says otherwise.
Example usage:
Codec<Integer> int32 = Codecs.int32();
Codec<Long> uint32 = Codecs.uint32();
Codec<Long> uint32le = Codecs.uint32(ByteOrder.LITTLE_ENDIAN);
-
Method Summary
Modifier and TypeMethodDescriptiondecode(InputStream input) Decodes a value from the input stream.encode(V value, OutputStream output) Encodes the given value and writes it to the output stream.static BinaryNumberCodec<Double> ofDouble()Creates a codec for double values (IEEE 754 double-precision, 8 bytes).static BinaryNumberCodec<Float> ofFloat()Creates a codec for float values (IEEE 754 single-precision, 4 bytes).static BinaryNumberCodec<Integer> ofInt()Creates a codec for signed integer values (-2147483648 to 2147483647).static BinaryNumberCodec<Long> ofLong()Creates a codec for signed long values (-9223372036854775808 to 9223372036854775807).static BinaryNumberCodec<Short> ofShort()Creates a codec for signed short values (-32768 to 32767).static BinaryNumberCodec<Integer> Creates a codec for unsigned byte values (0 to 255).static BinaryNumberCodec<Long> Creates a codec for unsigned integer values (0 to 4294967295).static BinaryNumberCodec<Integer> Creates a codec for unsigned three-byte values (0 to 16777215).static BinaryNumberCodec<Integer> Creates a codec for unsigned short values (0 to 65535).Returns a codec that reads and writes the same value in the given byte order.
-
Method Details
-
withOrder
Returns a codec that reads and writes the same value in the given byte order.Single-byte codecs are unaffected, since there is no order to choose.
- Parameters:
order- the byte order to use- Returns:
- a codec equivalent to this one but using
order - Throws:
NullPointerException- if order is null
-
ofInt
Creates a codec for signed integer values (-2147483648 to 2147483647).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for signed integers
-
ofLong
Creates a codec for signed long values (-9223372036854775808 to 9223372036854775807).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for signed longs
-
ofShort
Creates a codec for signed short values (-32768 to 32767).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for signed shorts
-
ofDouble
Creates a codec for double values (IEEE 754 double-precision, 8 bytes).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for doubles
-
ofFloat
Creates a codec for float values (IEEE 754 single-precision, 4 bytes).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for floats
-
ofUnsignedByte
Creates a codec for unsigned byte values (0 to 255).Byte order does not apply to a single byte.
- Returns:
- a new codec for unsigned bytes
-
ofUnsignedShort
Creates a codec for unsigned short values (0 to 65535).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for unsigned shorts
-
ofUnsignedMedium
Creates a codec for unsigned three-byte values (0 to 16777215).Three bytes has no
ByteBufferaccessor and no Java primitive, so the bytes are assembled by hand. The buffer's ownorderdecides which end the most significant byte goes, which is what makeswithOrder(ByteOrder)work here as it does for the widths the buffer handles natively.Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for unsigned three-byte values
-
ofUnsignedInt
Creates a codec for unsigned integer values (0 to 4294967295).Big-endian; use
withOrder(ByteOrder)for little-endian.- Returns:
- a new codec for unsigned integers
-
encode
Encodes the given value and writes it to the output stream.- Specified by:
encodein interfaceCodec<V extends Number>- Parameters:
value- the value to encodeoutput- the output stream to write the encoded bytes to- Returns:
- the encode result containing logical count and bytes written
- Throws:
IllegalArgumentException- if the value is outside the valid range for unsigned typesIOException- if an I/O error occurs during encoding
-
decode
Decodes a value from the input stream.- Specified by:
decodein interfaceCodec<V extends Number>- Parameters:
input- the input stream to read the encoded bytes from- Returns:
- the decoded value
- Throws:
EOFException- if the stream ends before the required bytes are readIOException- if an I/O error occurs during decoding
-