Class JBBPBitOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.igormaznitsa.jbbp.io.JBBPBitOutputStream
All Implemented Interfaces:
JBBPCountableBitStream, Closeable, Flushable, AutoCloseable

public class JBBPBitOutputStream extends FilterOutputStream implements JBBPCountableBitStream
The Filter allows to write bit by bit into an output stream and count the written byte number. The Class is not a thread-safe one.
Since:
1.0
  • Constructor Details

    • JBBPBitOutputStream

      public JBBPBitOutputStream(OutputStream out)
      A Constructor. The Default LSB0 bit mode will be used for a bit writing operations.
      Parameters:
      out - the output stream to be filtered.
    • JBBPBitOutputStream

      public JBBPBitOutputStream(OutputStream out, JBBPBitOrder order)
      A Constructor.
      Parameters:
      out - an output stream to be filtered.
      order - a bit writing mode to used for writing operations.
      See Also:
  • Method Details

    • getBitOrder

      public JBBPBitOrder getBitOrder()
      Get the bit mode for writing operations.
      Specified by:
      getBitOrder in interface JBBPCountableBitStream
      Returns:
      the bit order for reading operations.
      See Also:
    • writeShort

      public void writeShort(int value, JBBPByteOrder byteOrder) throws IOException
      Write a signed short value into the output stream.
      Parameters:
      value - a value to be written. Only two bytes will be written.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      See Also:
    • writeInt

      public void writeInt(int value, JBBPByteOrder byteOrder) throws IOException
      Write an integer value into the output stream.
      Parameters:
      value - a value to be written into the output stream.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      See Also:
    • writeUInt

      public void writeUInt(long value, JBBPByteOrder byteOrder) throws IOException
      Write an unsigned integer value into the output stream.
      Parameters:
      value - a value to be written into the output stream.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      2.0.4
      See Also:
    • writeFloat

      public void writeFloat(float value, JBBPByteOrder byteOrder) throws IOException
      Write a float value into the output stream.
      Parameters:
      value - a value to be written into the output stream.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
      See Also:
    • writeLong

      public void writeLong(long value, JBBPByteOrder byteOrder) throws IOException
      Write a long value into the output stream.
      Parameters:
      value - a value to be written into the output stream.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      See Also:
    • writeDouble

      public void writeDouble(double value, JBBPByteOrder byteOrder) throws IOException
      Write a double value into the output stream.
      Parameters:
      value - a value to be written into the output stream.
      byteOrder - the byte order of the value bytes to be used for writing.
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
      See Also:
    • getCounter

      public long getCounter()
      Get number of bytes written into the output stream.
      Specified by:
      getCounter in interface JBBPCountableBitStream
      Returns:
      the long value contains number of bytes written into the stream
    • getBitBuffer

      public int getBitBuffer()
      Get the inside bit buffer value.
      Specified by:
      getBitBuffer in interface JBBPCountableBitStream
      Returns:
      the inside bit buffer value
    • getBufferedBitsNumber

      public int getBufferedBitsNumber()
      Get the number of bits cached in the inside bit buffer.
      Specified by:
      getBufferedBitsNumber in interface JBBPCountableBitStream
      Returns:
      the number of cached bits in the bit buffer
    • flush

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

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • writeBits

      public void writeBits(int value, JBBPBitNumber bitNumber) throws IOException
      Write bits into the output stream.
      Parameters:
      value - the value which bits will be written in the output stream
      bitNumber - number of bits from the value to be written, must be in 1..8
      Throws:
      IOException - it will be thrown for transport errors
      IllegalArgumentException - it will be thrown for wrong bit number
    • align

      public void align(long alignByteNumber) throws IOException
      Write padding bytes to align the stream counter for the border.
      Parameters:
      alignByteNumber - the alignment border
      Throws:
      IOException - it will be thrown for transport errors
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException
    • write

      public void write(int value) throws IOException
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • writeBytes

      public void writeBytes(byte[] array, int length, JBBPByteOrder byteOrder) throws IOException
      Write number of items from byte array into stream
      Parameters:
      array - array, must not be null
      length - number of items to be written, if -1 then whole array
      byteOrder - order of bytes, if LITTLE_ENDIAN then array will be reversed
      Throws:
      IOException - it will be thrown if any transport error
      Since:
      1.3.0
      See Also:
    • resetCounter

      public void resetCounter()
      Reset the byte counter for the stream. The Inside bit buffer will be reset also.
      Specified by:
      resetCounter in interface JBBPCountableBitStream
    • writeString

      public void writeString(String value, JBBPByteOrder order) throws IOException
      Write string in UTF8 format into stream. the byte order in saved char data will be BIG_ENDIAN Format: PREFIX(FF=null | 0=empty | 0x8packedLength) LENGTH[packedLength] DATA_ARRAY[LENGTH]
      Parameters:
      value - string to be written, can be null
      order - order of bytes in written data (it doesn't affect encoded UTF8 array)
      Throws:
      IOException - i twill be thrown if transport error
      Since:
      1.4.0
    • writeStringArray

      public void writeStringArray(String[] value, JBBPByteOrder order) throws IOException
      Write array of strings in stream in UTF8 format the byte order in saved char data will be BIG_ENDIAN
      Parameters:
      value - array to be written, must not be null but can contain null values
      order - byte order to write char data, must not be null
      Throws:
      IOException - it will be thrown for transport errors
      Since:
      1.4.0
      See Also: