org.apache.calcite.avatica.util

## Class UnsynchronizedBuffer

- java.lang.Object
- - java.io.OutputStream
  - - org.apache.calcite.avatica.util.UnsynchronizedBuffer

All Implemented Interfaces:
  Closeable, Flushable, AutoCloseable

    public class UnsynchronizedBuffer
    extends 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.

### Field Summary

Fields
`(package private) byte[]` `data`
`(package private) int` `initialCapacity`
`private static int` `LARGE_BUFFER_SIZE`
`(package private) int` `offset`

### Constructor Summary

Constructors
Constructor and Description
`UnsynchronizedBuffer()` Creates a new writer.
`UnsynchronizedBuffer(int initialCapacity)` Creates a new writer.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`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, write`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### LARGE_BUFFER_SIZE

    private static final int LARGE_BUFFER_SIZE

See Also:
  Constant Field Values

#### 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 `OutputStream`
Parameters:
  `bytes` - byte array
  `off` - offset into array to start copying bytes
  `length` - number of bytes to add
Throws:
  `IndexOutOfBoundsException` - if off or length are invalid

#### write

    public void write(int b)
               throws IOException

Specified by:
  `write` in class `OutputStream`
Throws:
  `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:
  `IllegalArgumentException` - if i is negative