Package io.bytestreams.codec.core
Class BinaryCodec
java.lang.Object
io.bytestreams.codec.core.BinaryCodec
- All Implemented Interfaces:
Codec<byte[]>
A codec for fixed-length binary data.
-
Constructor Summary
ConstructorsConstructorDescriptionBinaryCodec(int length) Creates a new binary codec with the specified fixed length. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decode(InputStream input) Decodes a value from the input stream.encode(byte[] value, OutputStream output) Encodes the given value and writes it to the output stream.
-
Constructor Details
-
BinaryCodec
public BinaryCodec(int length) Creates a new binary codec with the specified fixed length.- Parameters:
length- the expected length of byte arrays (must be non-negative)- Throws:
IllegalArgumentException- if length is negative
-
-
Method Details
-
encode
Encodes the given value and writes it to the output stream.- Specified by:
encodein interfaceCodec<byte[]>- 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 byte array length does not match the expected lengthIOException- if an I/O error occurs during encoding
-
decode
Decodes a value from the input stream.- Specified by:
decodein interfaceCodec<byte[]>- 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
-