Class BinaryCodec

java.lang.Object
io.bytestreams.codec.core.BinaryCodec
All Implemented Interfaces:
Codec<byte[]>

public class BinaryCodec extends Object implements Codec<byte[]>
A codec for fixed-length binary data.
  • 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

      public EncodeResult encode(byte[] value, OutputStream output) throws IOException
      Encodes the given value and writes it to the output stream.
      Specified by:
      encode in interface Codec<byte[]>
      Parameters:
      value - the value to encode
      output - 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 length
      IOException - if an I/O error occurs during encoding
    • decode

      public byte[] decode(InputStream input) throws IOException
      Decodes a value from the input stream.
      Specified by:
      decode in interface Codec<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 read
      IOException - if an I/O error occurs during decoding