Class BitByteOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, BitOut
    Direct Known Subclasses:
    MemorySBOS

    public class BitByteOutputStream
    extends BitOutputStream
    An implementation of BitOutputStream that does no buffering. The numbers are written into a byte starting from the most significant bit (i.e, left to right). This class does not use any buffering, it relies on the underlying OutputStream to decide when to flush to disk. It is used for indexing, when keeping many compressed streams in memory (MemoryOutputStream) and a two-level buffering is unnecessary (and slower).
    Author:
    Roi Blanco
    • Field Detail

      • byteToWrite

        protected byte byteToWrite
        A byte to write to the stream.
    • Constructor Detail

      • BitByteOutputStream

        public BitByteOutputStream​(java.io.OutputStream is)
                            throws java.io.IOException
        Constructs an instance of the class for a given stream
        Parameters:
        is - java.io.OutputStream the underlying input stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • BitByteOutputStream

        public BitByteOutputStream​(java.lang.String filename)
                            throws java.io.IOException
        Constructs an instance of the class for a given filename
        Parameters:
        filename - java.lang.String the name of the underlying file
        Throws:
        java.io.IOException - if an I/O error occurs
      • BitByteOutputStream

        public BitByteOutputStream​(java.io.File file)
                            throws java.io.IOException
        Constructs an instance of the class for a given file
        Parameters:
        file - java.io.File the underlying file
        Throws:
        java.io.IOException - if an I/O error occurs
      • BitByteOutputStream

        public BitByteOutputStream()
        Empty constructor, used for subclassing
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes the stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class BitOutputStream
        Throws:
        java.io.IOException - if an I/O error occurs
      • getByteOffset

        public long getByteOffset()
        Returns the byte offset of the stream. It corresponds to the position of the byte in which the next bit will be written.
        Specified by:
        getByteOffset in interface BitOut
        Overrides:
        getByteOffset in class BitOutputStream
        Returns:
        the byte offset in the stream.
      • getBitOffset

        public byte getBitOffset()
        Returns the bit offset in the last byte. It corresponds to the position in which the next bit will be written.
        Specified by:
        getBitOffset in interface BitOut
        Overrides:
        getBitOffset in class BitOutputStream
        Returns:
        the bit offset in the stream.
      • writeUnary

        public int writeUnary​(int x)
                       throws java.io.IOException
        Writes an integer x using unary encoding. The encoding is a sequence of x -1 zeros and 1 one: 1, 01, 001, 0001, etc .. This method is not failsafe, it doesn't check if the argument is 0 or negative.
        Specified by:
        writeUnary in interface BitOut
        Overrides:
        writeUnary in class BitOutputStream
        Parameters:
        x - the number to write
        Returns:
        the number of bits written
        Throws:
        java.io.IOException - if an I/O error occurs.
      • writeInt

        public int writeInt​(int x,
                            int len)
                     throws java.io.IOException
        Writes an integer x into the underlying OutputStream. First, it checks if it fits into the current byte we are using for writing, and then it writes as many bytes as necessary
        Specified by:
        writeInt in interface BitOut
        Overrides:
        writeInt in class BitOutputStream
        Parameters:
        x - the int to write
        len - length of the int in bits
        Returns:
        the number of bits written
        Throws:
        java.io.IOException - if an I/O error occurs.
      • pad

        public void pad()
                 throws java.io.IOException
        Fills the remaining bits of the current byte with 0s
        Throws:
        java.io.IOException
      • getByteToWrite

        public byte getByteToWrite()
        Returns:
        the actual byte it is being written.