com.bigdata.io
Class AbstractFixedByteArrayBuffer

java.lang.Object
  extended by com.bigdata.io.AbstractFixedByteArrayBuffer
All Implemented Interfaces:
IByteArraySlice, IDataRecord, IFixedDataRecord
Direct Known Subclasses:
FixedByteArrayBuffer

public abstract class AbstractFixedByteArrayBuffer
extends Object
implements IFixedDataRecord

Efficient absolute get/put operations on a slice of a byte[]. This class is not thread-safe under mutation because the operations are not atomic. Concurrent operations on the same region of the slice can reveal partial updates. This class is abstract. A concrete implementation need only implement IByteArraySlice.array() and an appropriate constructor. This allows for use cases where the backing byte[] is extensible. E.g., a fixed slice onto an extensible ByteArrayBuffer.

Version:
$Id: AbstractFixedByteArrayBuffer.java 2265 2009-10-26 12:51:06Z thompsonbry $
Author:
Bryan Thompson

Constructor Summary
protected AbstractFixedByteArrayBuffer(int off, int len)
          Protected constructor used to create a slice.
 
Method Summary
 ByteBuffer asByteBuffer()
          Wraps the data in the slice within a ByteBuffer (does NOT copy the data).
 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(int pos)
          Absolute get for reading a byte value.
 DataInputBuffer getDataInput()
          Return an input stream that will read from the slice.
 double getDouble(int pos)
           
 float getFloat(int pos)
           
 it.unimi.dsi.io.InputBitStream getInputBitStream()
          Return a bit stream that will read from the slice.
 int getInt(int pos)
           
 long getLong(int pos)
           
 short getShort(int pos)
           
 int len()
          The length of the slice in the IByteArraySlice.array().
 int off()
          The start of the slice in the IByteArraySlice.array().
 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 dstoff, byte[] src, int srcoff, int srclen)
          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(int pos, byte v)
          Absolute put method for writing a byte value.
 void putDouble(int pos, double d)
           
 void putFloat(int pos, float f)
           
 void putInt(int pos, int v)
           
 void putLong(int pos, long v)
           
 void putShort(int pos, short v)
           
protected  boolean rangeCheck(int aoff, int alen)
          Verify that an operation starting at the specified offset into the slice and having the specified length is valid against the slice.
 boolean setBit(long bitIndex, boolean value)
          Set the value of a bit.
 AbstractFixedByteArrayBuffer slice(int aoff, int alen)
          Return a slice of the backing buffer.
 byte[] toByteArray()
          Return a copy of the data in the slice.
static FixedByteArrayBuffer wrap(byte[] array)
          A slice wrapping the entire array.
 void writeOn(DataOutput out)
          Write the slice on the output stream.
 void writeOn(OutputStream os)
          Write the slice on the output stream.
 void writeOn(OutputStream os, int aoff, int alen)
          Write part of the slice on the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.bigdata.io.IByteArraySlice
array
 

Constructor Detail

AbstractFixedByteArrayBuffer

protected AbstractFixedByteArrayBuffer(int off,
                                       int len)
Protected constructor used to create a slice. The caller is responsible for verifying that the slice is valid for the backing byte[] buffer.

Parameters:
off - The offset of the start of the slice.
len - The length of the slice.
Method Detail

off

public final int off()
Description copied from interface: IByteArraySlice
The start of the slice in the IByteArraySlice.array().

Specified by:
off in interface IByteArraySlice

len

public final int len()
Description copied from interface: IByteArraySlice
The length of the slice in the IByteArraySlice.array().

Specified by:
len in interface IByteArraySlice

wrap

public static FixedByteArrayBuffer wrap(byte[] array)
A slice wrapping the entire array.

Parameters:
array - The array.

rangeCheck

protected boolean rangeCheck(int aoff,
                             int alen)
Verify that an operation starting at the specified offset into the slice and having the specified length is valid against the slice.

Parameters:
aoff - The offset into the slice.
alen - The #of bytes to be addressed starting from that offset.
Returns:
true.
Throws:
IllegalArgumentException - if the operation is not valid.

put

public final void put(int pos,
                      byte[] b)
Description copied from interface: IDataRecord
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.

Specified by:
put in interface IDataRecord
Parameters:
pos - The starting position within the slice defined by this buffer.
b - The source data.

put

public final void put(int dstoff,
                      byte[] src,
                      int srcoff,
                      int srclen)
Description copied from interface: IDataRecord
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.

Specified by:
put in interface IDataRecord
Parameters:
dstoff - The offset into the slice to which the data will be copied.
src - The source data.
srcoff - The offset of the 1st byte in the source data to be copied.
srclen - The #of bytes to be copied.

get

public final void get(int srcoff,
                      byte[] dst)
Description copied from interface: IDataRecord
Absolute bulk get copies dst.length bytes from the specified offset into the slice defined by this buffer into the caller's array.

Specified by:
get in interface IDataRecord
Parameters:
srcoff - The offset into the slice of the first byte to be copied.
dst - The array into which the data will be copied.

get

public final void get(int srcoff,
                      byte[] dst,
                      int dstoff,
                      int dstlen)
Description copied from interface: IDataRecord
Absolute bulk get copies the specified slice of bytes from this buffer into the specified slice of the caller's array.

Specified by:
get in interface IDataRecord
Parameters:
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.

putByte

public final void putByte(int pos,
                          byte v)
Description copied from interface: IDataRecord
Absolute put method for writing a byte value.

Specified by:
putByte in interface IDataRecord
Parameters:
pos - The offset into the slice.
v - The value.

getByte

public final byte getByte(int pos)
Description copied from interface: IDataRecord
Absolute get for reading a byte value.

Specified by:
getByte in interface IDataRecord
Parameters:
pos - The offset into the slice.
Returns:
The byte value at that offset.

putShort

public final void putShort(int pos,
                           short v)
Specified by:
putShort in interface IDataRecord

getShort

public final short getShort(int pos)
Specified by:
getShort in interface IDataRecord

putInt

public final void putInt(int pos,
                         int v)
Specified by:
putInt in interface IDataRecord

getInt

public final int getInt(int pos)
Specified by:
getInt in interface IDataRecord

putFloat

public final void putFloat(int pos,
                           float f)
Specified by:
putFloat in interface IDataRecord

getFloat

public final float getFloat(int pos)
Specified by:
getFloat in interface IDataRecord

putLong

public final void putLong(int pos,
                          long v)
Specified by:
putLong in interface IDataRecord

putDouble

public final void putDouble(int pos,
                            double d)
Specified by:
putDouble in interface IDataRecord

getLong

public final long getLong(int pos)
Specified by:
getLong in interface IDataRecord

getDouble

public final double getDouble(int pos)
Specified by:
getDouble in interface IDataRecord

getBit

public final boolean getBit(long bitIndex)
Description copied from interface: IDataRecord
Get the value of a bit.

Specified by:
getBit in interface IDataRecord
Parameters:
bitIndex - The index of the bit, counting from the first bit position in the slice.
Returns:
The value of the bit.

setBit

public final boolean setBit(long bitIndex,
                            boolean value)
Description copied from interface: IDataRecord
Set the value of a bit.

Specified by:
setBit in interface IDataRecord
Parameters:
bitIndex - The index of the bit, counting from the first bit position in the slice.
Returns:
The old value of the bit.

toByteArray

public final byte[] toByteArray()
Description copied from interface: IDataRecord
Return a copy of the data in the slice.

Specified by:
toByteArray in interface IDataRecord
Returns:
A new array containing data in the slice.
See Also:
IDataRecord.asByteBuffer()

asByteBuffer

public final ByteBuffer asByteBuffer()
Description copied from interface: IDataRecord
Wraps the data in the slice within a ByteBuffer (does NOT copy the data).

Specified by:
asByteBuffer in interface IDataRecord
Returns:
A ByteBuffer encapsulating a reference to the data in the slice.

slice

public AbstractFixedByteArrayBuffer slice(int aoff,
                                          int alen)
Description copied from interface: IDataRecord
Return a slice of the backing buffer.

Specified by:
slice in interface IDataRecord
Parameters:
aoff - The starting offset of the slice into this slice.
alen - The #of bytes in the slice.
Returns:
The slice.

getDataInput

public DataInputBuffer getDataInput()
Description copied from interface: IFixedDataRecord
Return an input stream that will read from the slice.

Specified by:
getDataInput in interface IFixedDataRecord

getInputBitStream

public it.unimi.dsi.io.InputBitStream getInputBitStream()
Description copied from interface: IFixedDataRecord
Return a bit stream that will read from the slice.

Note: You DO NOT need to close this stream since it is backed by a byte[]. In fact, InputBitStream.close() when backed by a byte[] appears to have relatively high overhead, which is weird.

Specified by:
getInputBitStream in interface IFixedDataRecord

writeOn

public final void writeOn(OutputStream os)
                   throws IOException
Description copied from interface: IFixedDataRecord
Write the slice on the output stream.

Specified by:
writeOn in interface IFixedDataRecord
Parameters:
os - The output stream.
Throws:
IOException

writeOn

public final void writeOn(DataOutput out)
                   throws IOException
Description copied from interface: IFixedDataRecord
Write the slice on the output stream.

Specified by:
writeOn in interface IFixedDataRecord
Throws:
IOException

writeOn

public final void writeOn(OutputStream os,
                          int aoff,
                          int alen)
                   throws IOException
Description copied from interface: IFixedDataRecord
Write part of the slice on the output stream.

Specified by:
writeOn in interface IFixedDataRecord
Parameters:
os - The output stream.
Throws:
IOException


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.