Class ConstantCodec

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

public class ConstantCodec extends Object implements Codec<byte[]>, Inspectable<byte[]>
A codec for constant byte sequences such as magic numbers, version bytes, and protocol signatures.

On encode, always writes the expected bytes (ignoring the input value). On decode, reads and validates that the bytes match the expected value, throwing CodecException on mismatch.

  • Method Details

    • encode

      public EncodeResult encode(byte[] value, OutputStream output) throws IOException
      Encodes the given value and writes it to the output stream.

      Ignores the input value and always writes the expected constant bytes.

      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:
      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:
      CodecException - if the decoded bytes do not match the expected constant
      EOFException - if the stream ends before the required bytes are read
      IOException - if an I/O error occurs during decoding
    • inspect

      public Object inspect(byte[] value)
      Returns the expected constant bytes regardless of the input value.
      Specified by:
      inspect in interface Inspectable<byte[]>
      Parameters:
      value - the decoded value to inspect
      Returns:
      a structured representation (Map, List, or scalar)