public class DataBuffer
extends java.lang.Object
DataBuffer maintains three independent buffers for storing double,
integer, and Object data. Data is added to each using a put()
operation and can be read back using a get()
operation. Each
buffer has a size and an offset. When data is added using
put()
, it is added at the end of the buffer and the size is
increased. When data is read back using put()
, it is read from
the current offset location, and the offset is then increased. The offset
is not allowed to increase beyond the current size. The put()
and get()
operations therefore act like inset and remove
operations for a queue. However, unlike a queue, the get()
operation does not actually remove data; it simply advances the offset.
Other set()
and peek()
operations allow data to be
set and read within the buffer without affecting its offset or size.
As an example, we consider storing and retriveing information about a vector. The store operation might look like this:
saveVector (DataBuffer data) {
data.zput (size); // store vector size as an integer
for (int i=0; i<size; i++) {
data.dout (vector.get(i)); // store vector data as doubles
}
}
while the restore operation migth look like this:
loadVector (DataBuffer data) {
int size = data.zget (); // get the vector size
vector = new VectorNd(size);
for (int i=0; i<size; i++) {
vector.set (i, data.dget()); // restore the vector data
}
}
Internally, each buffer is implementing as an array, whose length defines the current buffer capacity. When the size increases beyond the capacity, the array is resized automatically and the capacity is increased. Attempting to move the offset beyond the current size will cause an excpetion to be thrown. The buffer structure is shown in the following diagram, in which entries marked with 'x' contain actual data.
|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | offset-+ size-+ capacity-+
Constructor and Description |
---|
DataBuffer() |
DataBuffer(int dcap,
int zcap) |
DataBuffer(int dcap,
int zcap,
int ocap)
Creates a new DataBuffer with specified capacities for its double,
integer, and Object buffers.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
double[] |
dbuffer()
Returns the current array used to store the double buffer.
|
void |
dEnsureCapacity(int cap)
Ensures that the double buffer has a specified capacity.
|
boolean |
dequals(DataBuffer data)
Returns true if the double buffer contents of this DataBuffer
and another DataBuffer are equal.
|
double |
dget()
Returns the double value at the current double buffer offset, and
increases the offset.
|
int |
doffset()
Returns the current double buffer offset.
|
double |
dpeek()
Returns the double value at the current double buffer offset, but does
not increase the offset.
|
double |
dpeek(int i)
Returns the double value at a specified location
i . |
void |
dput(double d)
Adds a double to the double buffer, increasing its size.
|
void |
dset(int i,
double d)
Overwrites a value in the double buffer at a specified location
i . |
void |
dsetOffset(int off)
Sets the double buffer offset.
|
void |
dsetSize(int size)
Resets the size of the double buffer.
|
int |
dsize()
Returns the amount of data in the double buffer.
|
void |
dskip(int n)
Advances the double buffer offset forward by
n . |
boolean |
equals(DataBuffer data) |
boolean |
equals(DataBuffer data,
boolean printFailPoint) |
java.lang.Object[] |
obuffer()
Returns the current array used to store the Object buffer.
|
void |
oEnsureCapacity(int cap)
Ensures that the Object buffer has a specified capacity.
|
boolean |
oequals(DataBuffer data)
Returns true if the Object buffer contents of this DataBuffer
and another DataBuffer are equal.
|
java.lang.Object |
oget()
Returns the Object value at the current Object buffer offset, and
increases the offset.
|
int |
ooffset()
Returns the current Object buffer offset.
|
java.lang.Object |
opeek()
Returns the Object value at the current Object buffer offset, but does
not increase the offset.
|
java.lang.Object |
opeek(int i)
Returns the Object value at a specified location
i . |
void |
oput(java.lang.Object o)
Adds a Object to the Object buffer, increasing its size.
|
void |
oputs(java.util.Collection<? extends java.lang.Object> objs)
Adds a collection of Objects to the Object buffer, increasing its size.
|
void |
oset(int i,
java.lang.Object o)
Overwrites a value in the Object buffer at a specified location
i . |
void |
osetOffset(int off)
Sets the Object buffer offset.
|
void |
osetSize(int size)
Resets the size of the Object buffer.
|
int |
osize()
Returns the amount of data in the Object buffer.
|
void |
oskip(int n)
Advances the Object buffer offset forward by
n . |
void |
putData(DataBuffer data,
int numd,
int numz) |
void |
resetOffsets() |
void |
set(DataBuffer data) |
void |
setBuffersAndOffsets(DataBuffer data) |
void |
setSize(int dsize,
int zsize) |
int[] |
zbuffer()
Returns the current array used to store the integer buffer.
|
void |
zEnsureCapacity(int cap)
Ensures that the integer buffer has a specified capacity.
|
boolean |
zequals(DataBuffer data)
Returns true if the integer buffer contents of this DataBuffer
and another DataBuffer are equal.
|
int |
zget()
Returns the integer value at the current integer buffer offset, and
increases the offset.
|
int |
zoffset()
Returns the current integer buffer offset.
|
int |
zpeek()
Returns the integer value at the current integer buffer offset, but does
not increase the offset.
|
int |
zpeek(int i)
Returns the integer value at a specified location
i . |
void |
zput(int z)
Adds a integer to the integer buffer, increasing its size.
|
void |
zset(int i,
int z)
Overwrites a value in the integer buffer at a specified location
i . |
void |
zsetOffset(int off)
Sets the integer buffer offset.
|
void |
zsetSize(int size)
Resets the size of the integer buffer.
|
int |
zsize()
Returns the amount of data in the integer buffer.
|
void |
zskip(int n)
Advances the integer buffer offset forward by
n . |
public DataBuffer()
public DataBuffer(int dcap, int zcap, int ocap)
public DataBuffer(int dcap, int zcap)
public int dsize()
public void dput(double d)
d
- double to addpublic void dset(int i, double d)
i
. If the location is outside the range 0
to
dsize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite datad
- new data value to setpublic void dsetSize(int size)
size
- new size for the double bufferpublic double dget()
dsize()
, an exception is thrown.public double dpeek()
dsize()
, an exception is thrown.public double dpeek(int i)
i
.
If the location is outside the range 0
to
dsize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void dskip(int n)
n
.
If this causes the offset to exceed dsize()
,
an exception is thrown.n
- amount to advance the double bufferpublic int doffset()
public void dsetOffset(int off)
0
to dsize()-1
, inclusive, an exception
is thrown.off
- new double buffer offsetpublic boolean dequals(DataBuffer data)
public void dEnsureCapacity(int cap)
cap
- request double buffer capacitypublic double[] dbuffer()
public int zsize()
public void zput(int z)
z
- integer to addpublic void zset(int i, int z)
i
. If the location is outside the range 0
to
zsize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite dataz
- new data value to setpublic void zsetSize(int size)
size
- new size for the integer bufferpublic int zget()
zsize()
, an exception is thrown.public int zpeek()
zsize()
, an exception is thrown.public int zpeek(int i)
i
.
If the location is outside the range 0
to
zsize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void zskip(int n)
n
.
If this causes the offset to exceed zsize()
,
an exception is thrown.n
- amount to advance the integer bufferpublic int zoffset()
public void zsetOffset(int off)
0
to zsize()-1
, inclusive, an exception
is thrown.off
- new integer buffer offsetpublic boolean zequals(DataBuffer data)
public void zEnsureCapacity(int cap)
cap
- request integer buffer capacitypublic int[] zbuffer()
public int osize()
public void oput(java.lang.Object o)
o
- Object to addpublic void oputs(java.util.Collection<? extends java.lang.Object> objs)
objs
- Objects to addpublic void oset(int i, java.lang.Object o)
i
. If the location is outside the range 0
to
osize()-1
, inclusive, an exception is thrown.i
- index at which to overwrite datao
- new data value to setpublic void osetSize(int size)
null
s. If the size is decreased,
the offset will be adjusted to enusre that is does not exceed the size.size
- new size for the Object bufferpublic java.lang.Object oget()
osize()
, an exception is thrown.public java.lang.Object opeek()
osize()
, an exception is thrown.public java.lang.Object opeek(int i)
i
.
If the location is outside the range 0
to
osize()-1
, inclusive, an exception is thrown.i
- index at which to obtain the datai
public void oskip(int n)
n
.
If this causes the offset to exceed osize()
,
an exception is thrown.n
- amount to advance the Object bufferpublic int ooffset()
public void osetOffset(int off)
0
to osize()-1
, inclusive, an exception
is thrown.off
- new Object buffer offsetpublic boolean oequals(DataBuffer data)
public void oEnsureCapacity(int cap)
cap
- request Object buffer capacitypublic java.lang.Object[] obuffer()
public void setSize(int dsize, int zsize)
public void clear()
public void set(DataBuffer data)
public boolean equals(DataBuffer data)
public boolean equals(DataBuffer data, boolean printFailPoint)
public void resetOffsets()
public void setBuffersAndOffsets(DataBuffer data)
public void putData(DataBuffer data, int numd, int numz)