Package io.bytestreams.codec.core
Class BinaryNumberCodec<V extends Number>
java.lang.Object
io.bytestreams.codec.core.BinaryNumberCodec<V>
- Type Parameters:
V- theNumbertype this codec handles
- All Implemented Interfaces:
Codec<V>
Codec for
Numbers encoded as fixed-length big-endian binary.
Example usage:
Codec<Integer> int32 = Codecs.int32();
Codec<Long> uint32 = Codecs.uint32();
-
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 short values (0 to 65535).
-
Method Details
-
ofInt
Creates a codec for signed integer values (-2147483648 to 2147483647).- Returns:
- a new codec for signed integers
-
ofLong
Creates a codec for signed long values (-9223372036854775808 to 9223372036854775807).- Returns:
- a new codec for signed longs
-
ofShort
Creates a codec for signed short values (-32768 to 32767).- Returns:
- a new codec for signed shorts
-
ofDouble
Creates a codec for double values (IEEE 754 double-precision, 8 bytes).- Returns:
- a new codec for doubles
-
ofFloat
Creates a codec for float values (IEEE 754 single-precision, 4 bytes).- Returns:
- a new codec for floats
-
ofUnsignedByte
Creates a codec for unsigned byte values (0 to 255).- Returns:
- a new codec for unsigned bytes
-
ofUnsignedShort
Creates a codec for unsigned short values (0 to 65535).- Returns:
- a new codec for unsigned shorts
-
ofUnsignedInt
Creates a codec for unsigned integer values (0 to 4294967295).- 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
-