Class ByteOrderOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.tccc.kos.commons.util.ByteOrderOutputStream
All Implemented Interfaces:
ByteOrderOutput, Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
BinaryMsgOutputStream

public class ByteOrderOutputStream extends FilterOutputStream implements ByteOrderOutput
An output stream that writes objects in little or big endian format.
Since:
1.0
Version:
2022-08-31
  • Constructor Details

    • ByteOrderOutputStream

      public ByteOrderOutputStream(ByteOrder endian, OutputStream out)
      Creates a new wrapper around the specified output stream.
      Parameters:
      endian - the endian of the stream
      out - the output stream to wrap
  • Method Details

    • getInnerStream

      public OutputStream getInnerStream()
      Return the inner output stream.
      Returns:
      the inner output stream this stream wraps
    • setInnerStream

      public void setInnerStream(OutputStream out)
      Set the inner output stream. Useful if the output stream wasn't known at the time the constructor was called.
      Parameters:
      out - the output stream
    • write

      public void write(int b) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a byte.
      Specified by:
      write in interface ByteOrderOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the byte to write
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Description copied from interface: ByteOrderOutput
      Write the specified number of bytes from the array starting at the specified offset.
      Specified by:
      write in interface ByteOrderOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the data to write
      off - offset to start from
      len - number of bytes to write
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException
    • writeBoolean

      public void writeBoolean(boolean v) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a boolean as a one byte value of 0 or 1.
      Specified by:
      writeBoolean in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeByte

      public void writeByte(int v) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a single byte to the output stream.
      Specified by:
      writeByte in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeShort

      public void writeShort(int v) throws IOException
      Description copied from interface: ByteOrderOutput
      Writes a short to the output stream.
      Specified by:
      writeShort in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeInt

      public void writeInt(int v) throws IOException
      Description copied from interface: ByteOrderOutput
      Writes an int to the output stream.
      Specified by:
      writeInt in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeInt24

      public void writeInt24(int v) throws IOException
      Description copied from interface: ByteOrderOutput
      Writes the lower 24 bits of an int to the output stream.
      Specified by:
      writeInt24 in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeLong

      public void writeLong(long v) throws IOException
      Description copied from interface: ByteOrderOutput
      Writes a long to the output stream.
      Specified by:
      writeLong in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeFloat

      public void writeFloat(float v) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a float to the output stream.
      Specified by:
      writeFloat in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeDouble

      public void writeDouble(double v) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a double to the output stream.
      Specified by:
      writeDouble in interface ByteOrderOutput
      Parameters:
      v - the value to write
      Throws:
      IOException - if there is an underlying error
    • writeCString

      public void writeCString(String str, int maxLen) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a string as an ascii encoded, null terminated C string. If the string is longer than maxLen then an exception will be thrown. If the string is shorter than the string, the difference will be zero padded. This makes it easy to write strings to fixed size buffers on the native side of things.
      Specified by:
      writeCString in interface ByteOrderOutput
      Parameters:
      str - the string to write
      maxLen - the max length of the string
      Throws:
      IOException
    • writeCString

      public void writeCString(String str, int maxLen, byte pad) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a string as an ascii encoded C string. The string is NOT null terminated. If the string is longer than maxLen then an exception will be thrown. If the string is shorter than the string, the difference will be padded with desired padding byte. This makes it easy to write strings to fixed size buffers on the native side of things.
      Specified by:
      writeCString in interface ByteOrderOutput
      Parameters:
      str - the string to write
      maxLen - the max length of the string
      pad - the padding byte to use
      Throws:
      IOException
    • writeCString

      public void writeCString(String str) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a variable length string as an ascii encoded C string. The string is null terminated. It is up to the caller to make sure the length is not a problem.
      Specified by:
      writeCString in interface ByteOrderOutput
      Parameters:
      str - the string to write
      Throws:
      IOException
    • writeStringWithLen

      public void writeStringWithLen(String str) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a utf-8 string preceded by a 16 bit length in bytes (not characters).
      Specified by:
      writeStringWithLen in interface ByteOrderOutput
      Parameters:
      str - the string to write
      Throws:
      IOException
    • writeShortStringWithLen

      public void writeShortStringWithLen(String str) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a short utf-8 string preceded by an 8 bit length in bytes (not characters).
      Specified by:
      writeShortStringWithLen in interface ByteOrderOutput
      Parameters:
      str - the string to write
      Throws:
      IOException
    • writeShortStringWithLen

      public void writeShortStringWithLen(String str, int maxChars) throws IOException
      Description copied from interface: ByteOrderOutput
      Write a short utf-8 string preceded by an 8 bit length in bytes (not characters), limited to the specified number of characters (not bytes).
      Specified by:
      writeShortStringWithLen in interface ByteOrderOutput
      Parameters:
      str - the string to write
      Throws:
      IOException
    • write

      public void write(byte[] b, int len) throws IOException
      Description copied from interface: ByteOrderOutput
      Write the specified array of bytes and then pad with zeros to the specified length.
      Specified by:
      write in interface ByteOrderOutput
      Parameters:
      b - the bytes to write
      len - the length to pad to
      Throws:
      IOException
    • size

      public int size()
      Return the number of bytes written so far.
      Returns:
      the number of bytes written so far