public class ByteArrayBuffer extends OutputStream implements IByteArrayBuffer, it.unimi.dsi.fastutil.io.RepositionableStream, LongPacker.IByteBuffer
Note: The backing byte[] slice always has an offset
of ZERO (0) and a length
equal to the capacity of
the backing byte[]. The length
is automatically
extended iff the backing buffer is extended.
Note: This class implements OutputStream
so that it may be wrapped by
an OutputBitStream
. Likewise it implements
RepositionableStream
so that you can rewind the
OutputBitStream
.
Note: The concept of limit()
and capacity()
are slightly
different than for ByteBuffer
since the limit and the capacity may be
transparently extended on write. However, neither the limit nor the capacity
will be extended on read (this prevents you from reading bad data). The
relative put operations always set the limit to the position as a
post-condition of the operation. The absolute get/put operations ignore the
limit entirely.
This class is NOT thread-safe for mutation. Not only is the underlying
FixedByteArrayBuffer
not thread-safe for mutation, but the operation
which replaces the slice(int, int)
when the capacity of the backing buffer
must be extended is not atomic.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_INITIAL_CAPACITY
The default capacity of the buffer.
|
Constructor and Description |
---|
ByteArrayBuffer()
Creates a buffer with an initial capacity of
128 bytes.
|
ByteArrayBuffer(int initialCapacity)
Creates a buffer with the specified initial capacity.
|
ByteArrayBuffer(int pos,
int limit,
byte[] buf)
Create a new buffer backed by the given array.
|
Modifier and Type | Method and Description |
---|---|
void |
advancePosAndLimit(int len)
Relative method advances the position and the limit by len bytes
(this simulates a relative put method, but does not write any
data).
|
ByteArrayBuffer |
append(byte b)
Append a byte into the managed byte[].
|
ByteArrayBuffer |
append(byte[] b)
Append data into the managed byte[].
|
ByteArrayBuffer |
append(byte[] b,
int off,
int len)
Append data into the managed byte[].
|
byte[] |
array()
The backing byte[] WILL be transparently replaced if the buffer capacity
is extended.
|
ByteBuffer |
asByteBuffer()
Wraps up a reference to the data in a
ByteBuffer between the
position and the limit. |
protected static int |
assertNonNegative(String msg,
int v)
Throws exception unless the value is non-negative.
|
int |
capacity()
The capacity of the buffer.
|
void |
clear()
Clears the buffer reference to
null . |
int |
copy(ByteBuffer src)
Relative copy of data into this buffer.
|
int |
copyAll(ByteArrayBuffer src)
Relative copy data from the origin (offset ZERO) of the
source buffer up to its read limit into this buffer.
|
int |
copyRest(ByteArrayBuffer src)
Relative copy data from the current position of the
source buffer up to its read limit into this buffer.
|
void |
ensureCapacity(int capacity)
Ensure that the buffer capacity is a least capacity total bytes.
|
void |
ensureFree(int len)
Ensure that at least len bytes are free in the buffer.
|
void |
ensureFree(int pos,
int len)
Ensure that at least len bytes are free in the buffer starting at
pos.
|
protected int |
extend(int required)
Return the new capacity for the buffer (default is always large enough
and will normally double the buffer capacity each time it overflows).
|
void |
flip()
Sets the position to zero but leaves the read limit at the old position.
|
void |
get(int srcoff,
byte[] dst)
Absolute bulk get copies
dst.length bytes from the
specified offset into the slice defined by this buffer into the caller's
array. |
void |
get(int srcoff,
byte[] dst,
int dstoff,
int dstlen)
Absolute bulk get copies the specified slice of bytes from this
buffer into the specified slice of the caller's array.
|
boolean |
getBit(long bitIndex)
Get the value of a bit.
|
byte |
getByte()
Relative get method for reading a byte value.
|
byte |
getByte(int pos)
Absolute get for reading a byte value.
|
double |
getDouble() |
double |
getDouble(int pos) |
float |
getFloat() |
float |
getFloat(int pos) |
int |
getInt() |
int |
getInt(int pos) |
long |
getLong() |
long |
getLong(int pos) |
OutputBitStream |
getOutputBitStream()
Return a bit stream which will write on this buffer.
|
short |
getShort() |
short |
getShort(int pos) |
int |
len()
The length of the slice is always the capacity of the backing byte[].
|
int |
limit()
The read limit (there is no write limit on the buffer since the capacity
will be automatically extended on overflow).
|
int |
mark()
Sets the mark.
|
int |
off()
The offset of the slice into the backing byte[] is always zero.
|
int |
packLong(long v)
Packs a non-negative long value into the minimum #of bytes in which the
value can be represented and writes those bytes onto the buffer.
|
int |
packShort(short v)
Packs a non-negative short value into one or two bytes and writes them on
the buffer.
|
int |
pos()
The current position in the buffer.
|
int |
pos(int pos)
Set the position in the buffer (does not change the limit).
|
long |
position() |
void |
position(long v) |
void |
put(byte[] b)
Relative put method for writing a byte[] on the buffer.
|
void |
put(byte[] b,
int off,
int len)
Relative put method for writing a byte[] on the buffer.
|
void |
put(int pos,
byte[] b)
Absolute bulk put copies all bytes in the caller's array into this
buffer starting at the specified position within the slice defined by
this buffer.
|
void |
put(int pos,
byte[] b,
int off,
int len)
Absolute bulk put copies the specified slice of bytes from the
caller's array into this buffer starting at the specified position within
the slice defined by this buffer.
|
void |
putByte(byte v)
Relative put method for writing a byte value.
|
void |
putByte(int pos,
byte v)
Absolute put method for writing a byte value.
|
void |
putDouble(double v) |
void |
putDouble(int pos,
double d) |
void |
putFloat(float v) |
void |
putFloat(int pos,
float f) |
void |
putInt(int v) |
void |
putInt(int pos,
int v) |
void |
putLong(int pos,
long v) |
void |
putLong(long v) |
void |
putShort(int pos,
short v) |
void |
putShort(short v) |
int |
remaining()
The #of bytes remaining in the buffer for relative read operations (limit -
pos).
|
ByteArrayBuffer |
reset()
Prepares the buffer for new data by resetting the position and limit to
zero.
|
int |
rewind()
Rewinds the buffer to the mark.
|
boolean |
setBit(long bitIndex,
boolean value)
Set the value of a bit.
|
int |
skip(int nbytes)
Skip forward or backward the specified number of bytes.
|
AbstractFixedByteArrayBuffer |
slice(int off,
int len)
Return a slice of the backing buffer.
|
byte[] |
toByteArray()
Return a copy of the data written on the buffer (the bytes in [0:pos]).
|
byte[] |
trim()
Trims the backing byte[] to an exact fit by making a copy of the data in
the buffer and returns the old byte[].
|
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
close, flush
public static final int DEFAULT_INITIAL_CAPACITY
public ByteArrayBuffer()
public ByteArrayBuffer(int initialCapacity)
initialCapacity
- The initial capacity.public ByteArrayBuffer(int pos, int limit, byte[] buf)
Note: The buffer reference is used directly rather than making a copy of the data.
public final byte[] array()
array
in interface IByteArrayBuffer
array
in interface IByteArraySlice
public final int off()
off
in interface IByteArrayBuffer
off
in interface IByteArraySlice
public final int len()
len
in interface IByteArrayBuffer
len
in interface IByteArraySlice
protected static int assertNonNegative(String msg, int v)
msg
- The exception message.v
- The value.IllegalArgumentException
- unless the value is non-negative.public final void ensureFree(int pos, int len)
buffer
may be grown by this operation but
it will not be truncated.
This operation is equivalent to
ensureCapacity(pos + len)and the latter is often used as an optimization.
pos
- The position in the buffer.len
- The minimum #of free bytes.public void ensureCapacity(int capacity)
ensureCapacity
in interface IManagedByteArray
capacity
- The minimum #of bytes in the buffer.public final int capacity()
IByteArrayBuffer
capacity
in interface IByteArrayBuffer
capacity
in interface IManagedByteArray
protected int extend(int required)
required
- The minimum required capacity.public final byte[] trim()
off()
and limit()
are copied into the exact fit byte[].
Note: A slice(int, int)
with a view of this
ByteArrayBuffer
will continue to have a view onto the backing
buffer associated with this instance. This works because
SliceImpl#array()
delegates to the outer
array()
method. This makes it possible to
trim()
a ByteArrayBuffer
on which you have
slice(int, int)
s, while maintaining the validity of those
slices.
public final void clear()
null
.public final void put(int pos, byte[] b)
IDataRecord
put
in interface IDataRecord
pos
- The starting position within the slice defined by this buffer.b
- The source data.public final void put(int pos, byte[] b, int off, int len)
IDataRecord
put
in interface IDataRecord
pos
- The offset into the slice to which the data will be copied.b
- The source data.off
- The offset of the 1st byte in the source data to be copied.len
- The #of bytes to be copied.public final void get(int srcoff, byte[] dst)
IDataRecord
dst.length
bytes from the
specified offset into the slice defined by this buffer into the caller's
array.get
in interface IDataRecord
srcoff
- The offset into the slice of the first byte to be copied.dst
- The array into which the data will be copied.public final void get(int srcoff, byte[] dst, int dstoff, int dstlen)
IDataRecord
get
in interface IDataRecord
srcoff
- The offset into the slice defined by this buffer of the first
byte to be copied.dst
- The array into which the data will be copied.dstoff
- The offset of the first byte in that array onto which the data
will be copied.dstlen
- The #of bytes to be copied.public final void putByte(int pos, byte v)
IDataRecord
putByte
in interface IDataRecord
pos
- The offset into the slice.v
- The value.public final byte getByte(int pos)
IDataRecord
getByte
in interface IDataRecord
pos
- The offset into the slice.public final void putShort(int pos, short v)
putShort
in interface IDataRecord
public final short getShort(int pos)
getShort
in interface IDataRecord
public final void putInt(int pos, int v)
putInt
in interface IDataRecord
public final int getInt(int pos)
getInt
in interface IDataRecord
public final void putFloat(int pos, float f)
putFloat
in interface IDataRecord
public final float getFloat(int pos)
getFloat
in interface IDataRecord
public final void putLong(int pos, long v)
putLong
in interface IDataRecord
public final void putDouble(int pos, double d)
putDouble
in interface IDataRecord
public final long getLong(int pos)
getLong
in interface IDataRecord
public final double getDouble(int pos)
getDouble
in interface IDataRecord
public final byte[] toByteArray()
toByteArray
in interface IByteArraySlice
asByteBuffer()
public final int remaining()
remaining
in interface IByteArrayBuffer
public final int pos()
pos
in interface IByteArrayBuffer
public final int pos(int pos)
pos
- The new position, must be in [0:capacity()
).IllegalArgumentException
- if pos is less than ZERO (0).IllegalArgumentException
- if pos is greater than or equal to the current
capacity of the buffer.public final int limit()
limit
in interface IByteArrayBuffer
public final void flip()
public ByteArrayBuffer reset()
reset
in interface IManagedByteArray
public final void ensureFree(int len)
IManagedByteArray
This operation is equivalent to
ensureCapacity(len() + len)and the latter is often used as an optimization.
ensureFree
in interface IManagedByteArray
len
- The minimum #of free bytes.public final int mark()
public final int rewind()
pos()
.public final int copy(ByteBuffer src)
src
- The source.public final int copyRest(ByteArrayBuffer src)
src
- The source buffer.copyAll(ByteArrayBuffer)
public final int copyAll(ByteArrayBuffer src)
src
- The source buffer.copyRest(ByteArrayBuffer)
public final ByteBuffer asByteBuffer()
ByteBuffer
between the
position and the limit.asByteBuffer
in interface IDataRecord
ByteBuffer
encapsulating a reference to the data in the
current buffer. The data will be overwritten if reset()
is invoked followed by any operations that write on the buffer.public final void advancePosAndLimit(int len)
len
- The #of bytes to advance (non-negative).public final void put(byte[] b)
b
- The byte[].public final void put(byte[] b, int off, int len)
put
in interface LongPacker.IByteBuffer
b
- The byte[].off
- The offset of the first byte in b to be written on the
buffer.len
- The #of bytes in b to be written on the buffer.public final void putByte(byte v)
v
- The value.public final byte getByte()
IndexOutOfBoundsException
- if the position is greater than or equal to the limit.public final void putShort(short v)
public final short getShort()
public final void putInt(int v)
public final int getInt()
public final void putFloat(float v)
public final float getFloat()
public final void putLong(long v)
public final long getLong()
public final void putDouble(double v)
public final double getDouble()
public final int packLong(long v)
[0|1|2|3|4|5|6|7] 1 - - - nbytes = 8, clear high bit and interpret this plus the next 7 bytes as a long. 0 1 1 1 nbytes = 7, clear high nibble and interpret this plus the next 6 bytes as a long. 0 1 1 0 nbytes = 6, clear high nibble and interpret this plus the next 5 bytes as a long. 0 1 0 1 nbytes = 5, clear high nibble and interpret this plus the next 4 bytes as a long. 0 1 0 0 nbytes = 4, clear high nibble and interpret this plus the next 3 bytes as a long. 0 0 1 1 nbytes = 3, clear high nibble and interpret this plus the next 3 bytes as a long. 0 0 1 0 nbytes = 2, clear high nibble and interpret this plus the next byte as a long. 0 0 0 1 nbytes = 1, clear high nibble. value is the low nibble.
v
- The unsigned long value.public final int packShort(short v)
v
- The unsigned short integer.public final void write(int b)
write
in class OutputStream
public final void write(byte[] b)
write
in class OutputStream
public ByteArrayBuffer append(byte b)
IManagedByteArray
append
in interface IManagedByteArray
b
- The byte.public ByteArrayBuffer append(byte[] b)
IManagedByteArray
append
in interface IManagedByteArray
b
- The source data.public final void write(byte[] b, int off, int len)
write
in class OutputStream
public ByteArrayBuffer append(byte[] b, int off, int len)
IManagedByteArray
append
in interface IManagedByteArray
b
- The source data.off
- The offset of the first byte to be copied.len
- The #of bytes to be copied.public long position() throws IOException
position
in interface it.unimi.dsi.fastutil.io.RepositionableStream
IOException
public void position(long v) throws IOException
position
in interface it.unimi.dsi.fastutil.io.RepositionableStream
IOException
public final boolean getBit(long bitIndex)
IDataRecord
getBit
in interface IDataRecord
bitIndex
- The index of the bit, counting from the first bit position in
the slice.public final boolean setBit(long bitIndex, boolean value)
IDataRecord
setBit
in interface IDataRecord
bitIndex
- The index of the bit, counting from the first bit position in
the slice.public int skip(int nbytes)
nbytes
- The #of bytes to skip (MAY be negative).public AbstractFixedByteArrayBuffer slice(int off, int len)
array()
, even when the buffer is extended and
the array reference is replaced. The pos()
and limit()
are ignored by this method.slice
in interface IDataRecord
off
- The starting offset into the backing buffer of the slice.len
- The length of that slice.public OutputBitStream getOutputBitStream()
pos()
.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.