Class UnsynchronizedBuffer

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class UnsynchronizedBuffer
    extends java.io.OutputStream
    A utility class for reading and writing bytes to byte buffers without synchronization. A reduced variant taken from Apache Accumulo. This class is not thread-safe by design. It is up to the caller to guarantee mutual exclusion as necessary.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getOffset()  
      long getSize()  
      static int nextArraySize​(int i)
      Determines what next array size should be by rounding up to next power of two.
      private void reserve​(int l)  
      void reset()
      Resets the internal pointer into the buffer.
      byte[] toArray()
      Gets (a copy of) the contents of this writer's buffer.
      void write​(byte[] bytes, int off, int length)
      Adds bytes to this writer's buffer.
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        close, flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • initialCapacity

        final int initialCapacity
      • offset

        int offset
      • data

        byte[] data
    • Constructor Detail

      • UnsynchronizedBuffer

        public UnsynchronizedBuffer()
        Creates a new writer.
      • UnsynchronizedBuffer

        public UnsynchronizedBuffer​(int initialCapacity)
        Creates a new writer.
        Parameters:
        initialCapacity - initial byte capacity
    • Method Detail

      • reserve

        private void reserve​(int l)
      • write

        public void write​(byte[] bytes,
                          int off,
                          int length)
        Adds bytes to this writer's buffer.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        bytes - byte array
        off - offset into array to start copying bytes
        length - number of bytes to add
        Throws:
        java.lang.IndexOutOfBoundsException - if off or length are invalid
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • toArray

        public byte[] toArray()
        Gets (a copy of) the contents of this writer's buffer.
        Returns:
        byte buffer contents
      • reset

        public void reset()
        Resets the internal pointer into the buffer.
      • getOffset

        public int getOffset()
        Returns:
        The current offset into the backing array.
      • getSize

        public long getSize()
        Returns:
        The current length of the backing array.
      • nextArraySize

        public static int nextArraySize​(int i)
        Determines what next array size should be by rounding up to next power of two.
        Parameters:
        i - current array size
        Returns:
        next array size
        Throws:
        java.lang.IllegalArgumentException - if i is negative