Package 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
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new writer.UnsynchronizedBuffer(int initialCapacity) Creates a new writer. -
Method Summary
Modifier and TypeMethodDescriptionintlonggetSize()static intnextArraySize(int i) Determines what next array size should be by rounding up to next power of two.voidreset()Resets the internal pointer into the buffer.byte[]toArray()Gets (a copy of) the contents of this writer's buffer.voidwrite(byte[] bytes, int off, int length) Adds bytes to this writer's buffer.voidwrite(int b) Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
Constructor Details
-
UnsynchronizedBuffer
public UnsynchronizedBuffer()Creates a new writer. -
UnsynchronizedBuffer
public UnsynchronizedBuffer(int initialCapacity) Creates a new writer.- Parameters:
initialCapacity- initial byte capacity
-
-
Method Details
-
write
public void write(byte[] bytes, int off, int length) Adds bytes to this writer's buffer.- Overrides:
writein classOutputStream- Parameters:
bytes- byte arrayoff- offset into array to start copying byteslength- number of bytes to add- Throws:
IndexOutOfBoundsException- if off or length are invalid
-
write
- Specified by:
writein classOutputStream- 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
-