Class BinaryNumberCodec<V extends Number>

java.lang.Object
io.bytestreams.codec.core.BinaryNumberCodec<V>
Type Parameters:
V - the Number type this codec handles
All Implemented Interfaces:
Codec<V>

public class BinaryNumberCodec<V extends Number> extends Object implements 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 Details

    • ofInt

      public static BinaryNumberCodec<Integer> ofInt()
      Creates a codec for signed integer values (-2147483648 to 2147483647).
      Returns:
      a new codec for signed integers
    • ofLong

      public static BinaryNumberCodec<Long> ofLong()
      Creates a codec for signed long values (-9223372036854775808 to 9223372036854775807).
      Returns:
      a new codec for signed longs
    • ofShort

      public static BinaryNumberCodec<Short> ofShort()
      Creates a codec for signed short values (-32768 to 32767).
      Returns:
      a new codec for signed shorts
    • ofDouble

      public static BinaryNumberCodec<Double> ofDouble()
      Creates a codec for double values (IEEE 754 double-precision, 8 bytes).
      Returns:
      a new codec for doubles
    • ofFloat

      public static BinaryNumberCodec<Float> ofFloat()
      Creates a codec for float values (IEEE 754 single-precision, 4 bytes).
      Returns:
      a new codec for floats
    • ofUnsignedByte

      public static BinaryNumberCodec<Integer> ofUnsignedByte()
      Creates a codec for unsigned byte values (0 to 255).
      Returns:
      a new codec for unsigned bytes
    • ofUnsignedShort

      public static BinaryNumberCodec<Integer> ofUnsignedShort()
      Creates a codec for unsigned short values (0 to 65535).
      Returns:
      a new codec for unsigned shorts
    • ofUnsignedInt

      public static BinaryNumberCodec<Long> ofUnsignedInt()
      Creates a codec for unsigned integer values (0 to 4294967295).
      Returns:
      a new codec for unsigned integers
    • encode

      public EncodeResult encode(V value, OutputStream output) throws IOException
      Encodes the given value and writes it to the output stream.
      Specified by:
      encode in interface Codec<V extends Number>
      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 value is outside the valid range for unsigned types
      IOException - if an I/O error occurs during encoding
    • decode

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