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 binary, big-endian unless withOrder(ByteOrder) says otherwise.

Example usage:

Codec<Integer> int32 = Codecs.int32();
Codec<Long> uint32 = Codecs.uint32();
Codec<Long> uint32le = Codecs.uint32(ByteOrder.LITTLE_ENDIAN);
  • Method Details

    • withOrder

      public BinaryNumberCodec<V> withOrder(ByteOrder order)
      Returns a codec that reads and writes the same value in the given byte order.

      Single-byte codecs are unaffected, since there is no order to choose.

      Parameters:
      order - the byte order to use
      Returns:
      a codec equivalent to this one but using order
      Throws:
      NullPointerException - if order is null
    • ofInt

      public static BinaryNumberCodec<Integer> ofInt()
      Creates a codec for signed integer values (-2147483648 to 2147483647).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for signed integers
    • ofLong

      public static BinaryNumberCodec<Long> ofLong()
      Creates a codec for signed long values (-9223372036854775808 to 9223372036854775807).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for signed longs
    • ofShort

      public static BinaryNumberCodec<Short> ofShort()
      Creates a codec for signed short values (-32768 to 32767).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      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).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for doubles
    • ofFloat

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

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for floats
    • ofUnsignedByte

      public static BinaryNumberCodec<Integer> ofUnsignedByte()
      Creates a codec for unsigned byte values (0 to 255).

      Byte order does not apply to a single byte.

      Returns:
      a new codec for unsigned bytes
    • ofUnsignedShort

      public static BinaryNumberCodec<Integer> ofUnsignedShort()
      Creates a codec for unsigned short values (0 to 65535).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for unsigned shorts
    • ofUnsignedMedium

      public static BinaryNumberCodec<Integer> ofUnsignedMedium()
      Creates a codec for unsigned three-byte values (0 to 16777215).

      Three bytes has no ByteBuffer accessor and no Java primitive, so the bytes are assembled by hand. The buffer's own order decides which end the most significant byte goes, which is what makes withOrder(ByteOrder) work here as it does for the widths the buffer handles natively.

      Big-endian; use withOrder(ByteOrder) for little-endian.

      Returns:
      a new codec for unsigned three-byte values
    • ofUnsignedInt

      public static BinaryNumberCodec<Long> ofUnsignedInt()
      Creates a codec for unsigned integer values (0 to 4294967295).

      Big-endian; use withOrder(ByteOrder) for little-endian.

      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