UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.collection
Class DataBuffer

java.lang.Object
  |
  +--edu.berkeley.guir.lib.collection.DataBuffer

public class DataBuffer
extends Object

Holds a buffer of bytes that can be resized, compacted, inserted into, removed, sliced, and diced. Useful for networking, file access, and other systems-level operations. As Chris Long astutely pointed out, this is the class to use when interfacing with C.

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.0-1.0.0, Jun 29 1999, JH
               Created class
             - GUIRLib-v1.2-1.0.0, Jun 22 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.3-1.0.0, Aug 11 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
 

Since:
JDK 1.1.6
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
(jasonh@cs.berkeley.edu)

Constructor Summary
DataBuffer()
          Create a new, empty DataBuffer.
DataBuffer(byte[] buf)
          Create a DataBuffer using the specified buffer.
DataBuffer(byte[] buf, int len)
          Create a DataBuffer using the specified buffer and the specified length.
DataBuffer(int len)
          Create a new, empty DataBuffer with the specified buffer size.
DataBuffer(String str)
          Create a new DataBuffer containing this String.
 
Method Summary
 void append(byte bb)
          Append a single byte to this current DataBuffer.
 void append(byte[] insertbuf)
          Append the specified buffer to the end.
 void append(byte[] insertbuf, int off, int len)
          Append the specified buffer to the end.
 void append(DataBuffer buf)
          Append a DataBuffer to this current DataBuffer.
 void appendToFile(String strFileName)
          Append the buffer to the specified file, creating the file if it does not exist.
 byte at(int index)
          Retrieve the byte at the specified position.
 void clear()
          Clear out the data in the buffer by setting the length to 0.
 void compact()
          Compact the data buffer to the current size of the data.
 void copyBytes(byte[] buf, int len)
          Copy the bytes in the specified buffer over to this DataBuffer.
 void fill()
          Fill the entire buffer with byte value 0.
 void fill(byte bb)
          Fill the entire buffer with the specified byte value.
 byte[] getBytes()
          Get a reference to the data buffer containing the data.
 int getLength()
          Get the length of the data.
 int index(DataBuffer searchbuf)
          Find the index of the specified buffer.
 int index(int start, DataBuffer searchbuf, int offset, int len)
          Find the index of the specified buffer.
 int indexOf(byte[] searchbuf)
          Find the index of the specified buffer.
 int indexOf(char ch)
          Find the index of the first instance of the specified character.
 int indexOf(int start, byte[] searchbuf, int offset, int len)
          Find the index of the specified buffer.
 int indexOf(int start, char ch)
          Find the index of the first instance of the specified character.
 int indexOf(int start, String str)
          Find the index of the specified buffer.
 int indexOf(int start, String str, int offset, int len)
          Find the index of the specified buffer.
 int indexOf(String str)
          Find the index of the specified buffer.
 void insert(int pos, byte[] insertbuf)
          Insert the entire buffer insertbuf at the specified position.
 void insert(int pos, byte[] insertbuf, int off, int len)
          Insert the specified buffer at the specified position.
 void insert(int pos, DataBuffer buf)
          Insert the entire DataBuffer buf at the specified position.
 void insert(int pos, DataBuffer buf, int off, int len)
          Insert the specified buffer at the specified position.
 void insert(int pos, int len)
          Insert len number of spaces (with value 0) at position pos.
 void insert(int pos, String str)
          Insert the specified String at the specified position.
 void readFromFile(String strFileName)
          Restore the buffer with data from the specified file.
 void remove(int pos, int len)
          Remove len number of bytes at position pos.
 void replace(String[] strFindArray, String strReplace)
          Find any String in strFindArray and replace it with strReplace.
 void replace(String strFind, String strReplace)
          Find all instances of a String, and replace these instances with another String.
 void resize(int size)
          Resize the data buffer size.
 void setBytes(byte[] buf, int len)
          Set the bytes contained in this DataBuffer.
 void setLength(int len)
          Set the length of the data.
 String toString()
          Convert the data to a String printable format.
 void writeToFile(String strFileName)
          Save the buffer as a file, overwriting the file if necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DataBuffer

public DataBuffer()
Create a new, empty DataBuffer.

DataBuffer

public DataBuffer(int len)
Create a new, empty DataBuffer with the specified buffer size.
Parameters:
len - is the length of the DataBuffer to create.

DataBuffer

public DataBuffer(byte[] buf)
Create a DataBuffer using the specified buffer.
Parameters:
buf - is the array to use in this DataBuffer.

DataBuffer

public DataBuffer(byte[] buf,
                  int len)
Create a DataBuffer using the specified buffer and the specified length.
Parameters:
buf - is the array to use in this DataBuffer.
len - is the length of the bytes to use from the buffer. Ensure that len is less than or equal to buf.length.

DataBuffer

public DataBuffer(String str)
Create a new DataBuffer containing this String.
Parameters:
str - is the String to copy into the DataBuffer.
Method Detail

writeToFile

public void writeToFile(String strFileName)
                 throws IOException
Save the buffer as a file, overwriting the file if necessary.
Parameters:
strFileName - is the name of the file to save the buffer to.
Throws:
IOException - on an I/O error, most likely a file write error.

appendToFile

public void appendToFile(String strFileName)
                  throws IOException
Append the buffer to the specified file, creating the file if it does not exist.
Parameters:
strFileName - is the name of the file to append to.
Throws:
IOException - on an I/O error, most likely a file write error.

readFromFile

public void readFromFile(String strFileName)
                  throws IOException,
                         FileNotFoundException
Restore the buffer with data from the specified file. Overwrites what is currently in the buffer. Can only read the first Integer.MAX_VALUE bytes from a file.
Parameters:
strFileName - is the file to read from.
Throws:
IOException - on an I/O error, most likely either file does not exist or there is a file read error.

getBytes

public byte[] getBytes()
Get a reference to the data buffer containing the data. You must ensure that the length of the data is correct if any changes are made!
Returns:
a reference to the only copy of the data.

getLength

public int getLength()
Get the length of the data.
Returns:
the length of the data.

indexOf

public int indexOf(String str)
Find the index of the specified buffer.
Parameters:
str - is the String to search for.

indexOf

public int indexOf(int start,
                   String str)
Find the index of the specified buffer.
Parameters:
str - is the String to search for.
start - is the start position in this databuffer to search from.

indexOf

public int indexOf(int start,
                   String str,
                   int offset,
                   int len)
Find the index of the specified buffer.
Parameters:
start - is the start position in this databuffer to search from.
str - is the String to search for.
offset - is where in searchbuf to start searching from.
len - is the number of bytes to search through in searchbuf.

index

public int index(DataBuffer searchbuf)
Find the index of the specified buffer.
Parameters:
searchbuf - is the bytes to search for.

index

public int index(int start,
                 DataBuffer searchbuf,
                 int offset,
                 int len)
Find the index of the specified buffer.
Parameters:
start - is the start position in this databuffer to search from.
searchbuf - is the bytes to search for.
offset - is where in searchbuf to start searching from.
len - is the number of bytes to search through in searchbuf.

indexOf

public int indexOf(byte[] searchbuf)
Find the index of the specified buffer.
Parameters:
searchbuf - is the bytes to search for.

indexOf

public int indexOf(int start,
                   byte[] searchbuf,
                   int offset,
                   int len)
Find the index of the specified buffer. Currently a brute-force search, should switch to Boyer-Moore or something similar one day.
Parameters:
start - is the start position in this databuffer to search from.
searchbuf - is the bytes to search for.
offset - is where in searchbuf to start searching from.
len - is the number of bytes to search through in searchbuf.

indexOf

public int indexOf(int start,
                   char ch)
Find the index of the first instance of the specified character.
Parameters:
start - is the start position in this databuffer to search from.
ch - is the char to search for.

indexOf

public int indexOf(char ch)
Find the index of the first instance of the specified character.
Parameters:
ch - is the char to search for.

at

public final byte at(int index)
Retrieve the byte at the specified position.
Parameters:
index - is the location to retrieve.

replace

public void replace(String[] strFindArray,
                    String strReplace)
Find any String in strFindArray and replace it with strReplace. Note that this does not work entirely correctly if a substring of strReplace is in strFindArray. This is an O(N^2) operation.

replace

public void replace(String strFind,
                    String strReplace)
Find all instances of a String, and replace these instances with another String. This is an O(N) operation.
Parameters:
strFind - is the String to find.
strReplace - is the String to replace strFind with.

clear

public void clear()
Clear out the data in the buffer by setting the length to 0. Use method fill() to put bogus values in the buffer.

fill

public void fill()
Fill the entire buffer with byte value 0.

fill

public void fill(byte bb)
Fill the entire buffer with the specified byte value. This can be useful when debugging, as you can insert a value that is obviously wrong.
Parameters:
bb - is the byte to put in the buffer.

resize

public void resize(int size)
Resize the data buffer size.
Parameters:
size - is the new size to change the buffer to. If it is smaller than the current length of the data, then it will default to the current length of the data.

compact

public void compact()
Compact the data buffer to the current size of the data.

append

public void append(byte[] insertbuf)
Append the specified buffer to the end.
Parameters:
insertbuf - is the buffer to insert.

append

public void append(byte[] insertbuf,
                   int off,
                   int len)
Append the specified buffer to the end.
Parameters:
insertbuf - is the buffer to insert.
off - is the offset in insertbuf to copy from.
len - is the amount to insert from insertbuf.

append

public void append(DataBuffer buf)
Append a DataBuffer to this current DataBuffer.
Parameters:
buf - is the DataBuffer to append.

append

public void append(byte bb)
Append a single byte to this current DataBuffer.
Parameters:
bb - is the byte to append.

insert

public void insert(int pos,
                   int len)
Insert len number of spaces (with value 0) at position pos. This is a slow operation.
Parameters:
pos - is the position to insert spaces at.
len - is the number of spaces to insert.

insert

public void insert(int pos,
                   byte[] insertbuf,
                   int off,
                   int len)
Insert the specified buffer at the specified position. This is a slow operation.
Parameters:
pos - is the position to start inserting at.
insertbuf - is the buffer to insert.
off - is the offset in insertbuf to copy from.
len - is the amount to insert from insertbuf.

insert

public void insert(int pos,
                   String str)
Insert the specified String at the specified position. This is a slow operation.
Parameters:
pos - is the position to start inserting at.
str - is the String to insert.

insert

public void insert(int pos,
                   byte[] insertbuf)
Insert the entire buffer insertbuf at the specified position. This is a slow operation.
Parameters:
pos - is the position to start inserting at.
insertbuf - is the buffer to insert.

insert

public void insert(int pos,
                   DataBuffer buf)
Insert the entire DataBuffer buf at the specified position. This is a slow operation.
Parameters:
pos - is the position to start inserting at.
insertbuf - is the buffer to insert.

insert

public void insert(int pos,
                   DataBuffer buf,
                   int off,
                   int len)
Insert the specified buffer at the specified position. This is a slow operation.
Parameters:
pos - is the position to start inserting at.
buf - is the DataBuffer to insert.
off - is the offset in insertbuf to copy from.
len - is the amount to insert from insertbuf.

remove

public void remove(int pos,
                   int len)
Remove len number of bytes at position pos. This is a slow operation.
Parameters:
pos - is the position to start removing from (starting with 0, inclusive).
len - is the length of data to remove.

setBytes

public void setBytes(byte[] buf,
                     int len)
Set the bytes contained in this DataBuffer.
Parameters:
buf - is the data buffer.
len - is the length of the data. Must be a nonnegative value.

copyBytes

public void copyBytes(byte[] buf,
                      int len)
Copy the bytes in the specified buffer over to this DataBuffer.
Parameters:
buf - is the data buffer.
len - is the length of the data. Must be a nonnegative value.

setLength

public void setLength(int len)
Set the length of the data.
Parameters:
len - is the length of the data. Must be a nonnegative value.

toString

public String toString()
Convert the data to a String printable format.
Overrides:
toString in class Object

Copyright Information