Class ByteOrderInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.tccc.kos.commons.util.ByteOrderInputStream
All Implemented Interfaces:
ByteOrderInput, Closeable, AutoCloseable

public class ByteOrderInputStream extends FilterInputStream implements ByteOrderInput
An input stream that reads objects in little or big endian format.
Since:
1.0
Version:
2022-08-31
  • Constructor Details

    • ByteOrderInputStream

      public ByteOrderInputStream(ByteOrder endian, InputStream in)
      Create a wrapper around the specified input stream.
      Parameters:
      endian - the endian of the stream
      in - the specified input stream
  • Method Details

    • getInnerStream

      public InputStream getInnerStream()
      Return the inner input stream.
      Returns:
      the inner input stream this stream wraps
    • read

      public int read(byte[] b) throws IOException
      Description copied from interface: ByteOrderInput
      Read data into the byte array. This is not guaranteed to fill the array.
      Specified by:
      read in interface ByteOrderInput
      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the byte array to read into
      Returns:
      the actual number of bytes read
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Description copied from interface: ByteOrderInput
      Read the specified number of bytes of data into the array starting at the specified offset.
      Specified by:
      read in interface ByteOrderInput
      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the array to read data into
      off - the starting offset
      len - the number of bytes to read
      Returns:
      the actual number of bytes read
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b) throws IOException
      Description copied from interface: ByteOrderInput
      Read data into the byte array and only return if the array is full or there is an EOF in the data.
      Specified by:
      readFully in interface ByteOrderInput
      Parameters:
      b - the array to read into
      Throws:
      IOException - if there is an underlying exception
      EOFException - if EOF is reached
    • skipBytes

      public int skipBytes(int n) throws IOException
      Description copied from interface: ByteOrderInput
      Skip the specified number of bytes of input. Returns the total number of bytes skipped.
      Specified by:
      skipBytes in interface ByteOrderInput
      Parameters:
      n - the number of bytes to skip
      Returns:
      the number of byte skipped
      Throws:
      IOException - if there is an underlying exception
    • readBoolean

      public boolean readBoolean() throws IOException
      Description copied from interface: ByteOrderInput
      Read the next byte of input as a boolean value.
      Specified by:
      readBoolean in interface ByteOrderInput
      Returns:
      the boolean value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readByte

      public byte readByte() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next available byte.
      Specified by:
      readByte in interface ByteOrderInput
      Returns:
      the byte value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readUnsignedByte

      public int readUnsignedByte() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next byte as an unsigned byte.
      Specified by:
      readUnsignedByte in interface ByteOrderInput
      Returns:
      the unsigned byte value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readShort

      public short readShort() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next two bytes as a signed short.
      Specified by:
      readShort in interface ByteOrderInput
      Returns:
      the short value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readUnsignedShort

      public int readUnsignedShort() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next two bytes as an unsigned short.
      Specified by:
      readUnsignedShort in interface ByteOrderInput
      Returns:
      the unsigned short value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readInt

      public int readInt() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next four bytes as an int.
      Specified by:
      readInt in interface ByteOrderInput
      Returns:
      the int value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readLong

      public long readLong() throws IOException
      Description copied from interface: ByteOrderInput
      Return the next eight bytes as a long.
      Specified by:
      readLong in interface ByteOrderInput
      Returns:
      the long value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readFloat

      public float readFloat() throws IOException
      Description copied from interface: ByteOrderInput
      Read the next four bytes as a float.
      Specified by:
      readFloat in interface ByteOrderInput
      Returns:
      the float value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readDouble

      public double readDouble() throws IOException
      Description copied from interface: ByteOrderInput
      Read the next eight bytes as a double.
      Specified by:
      readDouble in interface ByteOrderInput
      Returns:
      the double value
      Throws:
      IOException - if there is an underlying exception or EOF
    • readCString

      public String readCString() throws IOException
      Description copied from interface: ByteOrderInput
      Read a C string from input, terminated by a null byte.
      Specified by:
      readCString in interface ByteOrderInput
      Returns:
      the C string converted to a string
      Throws:
      IOException - if there is an underlying exception or EOF
    • readEscapedCString

      public String readEscapedCString() throws IOException
      Description copied from interface: ByteOrderInput
      Read a C string from input, terminated by a null byte and then process it for escaped UTF-8 characters.
      Specified by:
      readEscapedCString in interface ByteOrderInput
      Returns:
      the converted string
      Throws:
      IOException - if there is an underlying exception or EOF
    • readCString

      public String readCString(int len) throws IOException
      Description copied from interface: ByteOrderInput
      Read a C string from input, terminated by a null byte and padded up to the specified size.
      Specified by:
      readCString in interface ByteOrderInput
      Returns:
      the C string converted to a string
      Throws:
      IOException - if there is an underlying exception or EOF
    • readString

      public String readString() throws IOException
      Description copied from interface: ByteOrderInput
      Read all remaining input as a string. This assumes the data in the buffer is not null terminated as a normal C string.
      Specified by:
      readString in interface ByteOrderInput
      Returns:
      the remaining bytes converted to a string
      Throws:
      IOException - if there is an underlying exception or EOF