|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.OutputStream
com.bigdata.io.ByteArrayBuffer
public class ByteArrayBuffer
A view on a mutable byte[] that may be extended.
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.
| Field Summary | |
|---|---|
static int |
DEFAULT_INITIAL_CAPACITY
The default capacity of the buffer. |
protected static boolean |
INFO
|
protected static org.apache.log4j.Logger |
log
|
| Constructor Summary | |
|---|---|
ByteArrayBuffer()
Creates a buffer with an initial capacity of 1024 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. |
|
| Method Summary | |
|---|---|
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. |
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)
|
protected static int |
getNibbleLength(long v)
Return the #of non-zero nibbles, counting from the first non-zero nibble in the long value. |
it.unimi.dsi.io.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)
|
| Methods inherited from class java.io.OutputStream |
|---|
close, flush |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected static final org.apache.log4j.Logger log
protected static final boolean INFO
public static final int DEFAULT_INITIAL_CAPACITY
| Constructor Detail |
|---|
public ByteArrayBuffer()
1024 bytes.
The position and the read limit will zero. The capacity of the buffer
will be automatically extended as required.
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.
pos - The initial position.limit - The initial limit().buf - The backing byte[].| Method Detail |
|---|
public final byte[] array()
array in interface IByteArrayBufferarray in interface IByteArraySlicepublic final int off()
off in interface IByteArrayBufferoff in interface IByteArraySlicepublic final int len()
len in interface IByteArrayBufferlen 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 equivilent 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)
buffer may be grown by this operation but it will not be
truncated.
capacity - The minimum #of bytes in the buffer.public final int capacity()
IByteArrayBuffer
capacity in interface IByteArrayBufferprotected 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 put(int pos,
byte[] b)
IDataRecord
put in interface IDataRecordpos - 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 IDataRecordpos - 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)
IDataRecorddst.length bytes from the
specified offset into the slice defined by this buffer into the caller's
array.
get in interface IDataRecordsrcoff - 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 IDataRecordsrcoff - 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 IDataRecordpos - The offset into the slice.v - The value.public final byte getByte(int pos)
IDataRecord
getByte in interface IDataRecordpos - The offset into the slice.
public final void putShort(int pos,
short v)
putShort in interface IDataRecordpublic final short getShort(int pos)
getShort in interface IDataRecord
public final void putInt(int pos,
int v)
putInt in interface IDataRecordpublic final int getInt(int pos)
getInt in interface IDataRecord
public final void putFloat(int pos,
float f)
putFloat in interface IDataRecordpublic 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 IDataRecordpublic final long getLong(int pos)
getLong in interface IDataRecordpublic final double getDouble(int pos)
getDouble in interface IDataRecordpublic final byte[] toByteArray()
toByteArray in interface IDataRecordasByteBuffer()flip() to set the pos to zero while leaving
the read limit alone. that is at odds with the rest of the relative
api but there is a lot of use of this method already. perhaps it
can be moved to the DataOutputBuffer where it would share
semantics with ByteArrayOutputStream.toByteArray()?public final int remaining()
remaining in interface IByteArrayBufferpublic final int pos()
pos in interface IByteArrayBufferpublic 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 IByteArrayBufferpublic final void flip()
public ByteArrayBuffer reset()
public final void ensureFree(int len)
buffer may be grown by this operation but it will not be
truncated.
This operation is equivalent to
ensureCapacity(this.len + len)and the latter is often used as an optimization.
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 IDataRecordByteBuffer 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 put(byte[] b)
b - The byte[].
public final void put(byte[] b,
int off,
int len)
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.ByteBuffer?public final byte getByte()
IndexOutOfBoundsException - if the position is greater than or equal to the limit.ByteBuffer?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.
protected static final int getNibbleLength(long v)
0L is considered to be one
nibble for our purposes.
v - The long value.
public final int packShort(short v)
v - The unsigned short integer.
public final void write(int b)
throws IOException
write in class OutputStreamIOException
public final void write(byte[] b)
throws IOException
write in class OutputStreamIOException
public final void write(byte[] b,
int off,
int len)
throws IOException
write in class OutputStreamIOException
public long position()
throws IOException
position in interface it.unimi.dsi.fastutil.io.RepositionableStreamIOException
public void position(long v)
throws IOException
position in interface it.unimi.dsi.fastutil.io.RepositionableStreamIOExceptionpublic final boolean getBit(long bitIndex)
IDataRecord
getBit in interface IDataRecordbitIndex - 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 IDataRecordbitIndex - 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 IDataRecordoff - The starting offset into the backing buffer of the slice.len - The length of that slice.
public it.unimi.dsi.io.OutputBitStream getOutputBitStream()
pos().
OutputBitStream wrapping a byte[] will not auto-extend.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||