de.bb.util
Class ByteRef

java.lang.Object
  extended by de.bb.util.ByteRef
All Implemented Interfaces:
java.lang.Cloneable

public final class ByteRef
extends java.lang.Object
implements java.lang.Cloneable

A String like class to manipulate byte arrays. The most important differences to Strings:

So the user of ByteRef must handle them correctly. The result is a big speed gain in data parsing and handling.

For not yet or bad documented functions search the comment in the String class (or guess).

Author:
Stefan Franke

Constructor Summary
ByteRef()
          Initializes a newly created ByteRef object so that it represents an empty byte sequence.
ByteRef(byte[] b)
          Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the argument.
ByteRef(byte[] b, int _begin, int _end)
          Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the specified part of the argument.
ByteRef(java.lang.String s)
          Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the argument string.getBytes().
 
Method Summary
 void adjustLeft(int count)
          Move the left offset by count bytes.
 ByteRef append(ByteRef o)
          Append the specified ByteRef to this ByteRef.
 ByteRef append(java.lang.String s)
          Append the bytes of the String to this ByteRef.
 void assign(byte[] b, int _begin, int _end)
          Assigns the parameters physical byte array to this object.
 void assign(ByteRef o)
          Assigns the parameters physical byte array to this object.
 int charAt(int idx)
          Returns the byte at the specified position.
 java.lang.Object clone()
           
 int compareTo(ByteRef o)
          Compares this ByteRef's data to another ByteRef.
 void copy(byte[] b)
          Copy this data into the specified byte array.
 int copy(byte[] b, int off, int l)
          Copy a part of this data into the specified byte array at specified offset.
 boolean endsWith(ByteRef o)
          Tests whether this BytRef ends with the specified ByteRef.
 boolean endsWith(java.lang.String s)
          Tests whether this BytRef ends with the specified String.
 boolean equals(java.lang.Object _o)
          Compares this objects data to another object.
 boolean equalsIgnoreCase(java.lang.Object _o)
          Compares this objects data to another object ignoring upper and lower case.
 int hashCode()
          Compute a hashCode for use in Hashtable.
 int indexOf(ByteRef s)
          Returns the first index of the specified ByteRef's bytes.
 int indexOf(ByteRef s, int off)
          Returns the first index of the specified ByteRef's bytes.
 int indexOf(int z)
          Returns the first index of the specified byte.
 int indexOf(int z, int off)
          Returns the first index of the specified byte.
 int indexOf(java.lang.String s)
          Returns the first index of the specified String's bytes.
 int indexOf(java.lang.String s, int off)
          Returns the first index of the specified String's bytes.
 int lastIndexOf(int z)
          Returns the last index of the specified byte.
 int lastIndexOf(int z, int off)
          Returns the last index of the specified byte.
 int length()
          Return the length of thisByteRef object.
 ByteRef nextLine()
          Extracts the next available line from this object and returns that line as a new BytRef object.
 ByteRef nextWord()
          Extracts the next available word from this object and returns that word as a new BytRef object.
 ByteRef nextWord(int delim)
          Extracts the content from this object up to the delimiter and returns that as a new BytRef object.
 java.lang.String nextWordAsString(int delim)
          Extracts the content from this object up to the delimiter and returns that as a new BytRef object.
static ByteRef readLine(ByteRef br, java.io.InputStream is)
          Extracts the next line from the specified ByteRef br.
 int removeLeft()
          Remove the first byte and return it as int >= 0 or return -1; This is a self modification.
 ByteRef splitLeft(int stop)
          Returns a ByteRef starting from 0 to stop, and removes this part from the current ByteRef.
 ByteRef splitLeft(int start, int stop)
          Returns a ByteRef starting from start to stop, and removes this part from the current ByteRef.
 boolean startsWith(ByteRef o)
          Tests whether this BytRef starts with the specified ByteRef.
 boolean startsWith(java.lang.String s)
          Tests whether this BytRef starts with the specified String.
 ByteRef substring(int a)
          Returns a new BytRef object using only the specified part of this ByteRef, up to end of this ByteRef.
 ByteRef substring(int a, int z)
          Returns a new BytRef object using only the specified part of this ByteRef.
 byte[] toByteArray()
          Returns the ByteRefs data as a new byte array.
 int toInteger()
          Interpret the bytes as characters and convert them into an int value.
 ByteRef toLowerCase()
          Converts upper case letters into lower case letters.
 java.lang.String toString()
          Returns the ByteRefs data as a String.
 java.lang.String toString(int offset)
          Returns the ByteRefs data as a String starting from specified offset.
 ByteRef toUpperCase()
          Converts lower case letters into lower case letters.
 ByteRef trim()
          Remove all white spaces at begin and end.
 ByteRef trim(int ch)
          Remove all occurencies of the specified charater at begin and end.
 ByteRef update(java.io.InputStream is)
          Update this ByteRef with the availabledata of the specified input stream.
 void writeTo(java.io.OutputStream os)
          Write the bytes to the specified output stream.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteRef

public ByteRef()
Initializes a newly created ByteRef object so that it represents an empty byte sequence.


ByteRef

public ByteRef(java.lang.String s)
Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the argument string.getBytes().

Parameters:
s - a String.

ByteRef

public ByteRef(byte[] b)
Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the argument.

Parameters:
b - a byte array.

ByteRef

public ByteRef(byte[] b,
               int _begin,
               int _end)
Initializes a newly created ByteRef object so that it represents the same sequence of bytes as the argument; in other words, the newly created byte array is a copy of the specified part of the argument.

Parameters:
b - a byte array.
_begin - begin in the byte array
_end - end in the byte array
Method Detail

assign

public void assign(ByteRef o)
Assigns the parameters physical byte array to this object.

Parameters:
o - a ByteRef

assign

public final void assign(byte[] b,
                         int _begin,
                         int _end)
Assigns the parameters physical byte array to this object.

Parameters:
b - a byte array
_begin - begin in the byte array
_end - end in the byte array

nextLine

public final ByteRef nextLine()
Extracts the next available line from this object and returns that line as a new BytRef object. Unix and DOS EOLs are recognized correctly. MAC EOLs will fail at end of buffer!

Returns:
a new allocated ByteRef object with the next line, or null if no EOL was found.

nextWord

public final ByteRef nextWord()
Extracts the next available word from this object and returns that word as a new BytRef object. All bytes < 0x20 are treated as white spaces.

Returns:
a new allocated ByteRef object with the next word, or null at end of buffer.

nextWord

public final ByteRef nextWord(int delim)
Extracts the content from this object up to the delimiter and returns that as a new BytRef object. The current object is modified and contains the remaining content behind the delimiter.

Parameters:
delim - the used delimiter
Returns:
a new allocated ByteRef object with the next word, or null at end of buffer.

nextWordAsString

public final java.lang.String nextWordAsString(int delim)
Extracts the content from this object up to the delimiter and returns that as a new BytRef object. The current object is modified and contains the remaining content behind the delimiter.

Parameters:
delim - the used delimiter
Returns:
a new String object with the next word, or null at end of buffer.

length

public final int length()
Return the length of thisByteRef object.

Returns:
the length of thisByteRef object

copy

public final void copy(byte[] b)
Copy this data into the specified byte array.

Parameters:
b - a byte array

copy

public final int copy(byte[] b,
                      int off,
                      int l)
Copy a part of this data into the specified byte array at specified offset.
If there are not enough bytes to copy, only the available bytes are copied.

Parameters:
b - a byte array
off - the offset into b
l - the copied length
Returns:
an int representing the real count of copied bytes

append

public final ByteRef append(ByteRef o)
Append the specified ByteRef to this ByteRef. A new ByteRef is allocated and returned, to store the concatenation of both ByteRefs.

Parameters:
o - the appended ByteRef
Returns:
a new ByteRef object with the concatenation of both ByteRefs.

append

public final ByteRef append(java.lang.String s)
Append the bytes of the String to this ByteRef. A new ByteRef is allocated and returned, to store the concatenation.

Parameters:
s - the appended String
Returns:
a new ByteRef object with the concatenation.

toString

public final java.lang.String toString()
Returns the ByteRefs data as a String.

Overrides:
toString in class java.lang.Object
Returns:
the ByteRefs data as a String.

toString

public final java.lang.String toString(int offset)
Returns the ByteRefs data as a String starting from specified offset.

Parameters:
offset - starting from offset
Returns:
the ByteRefs data as a String.

toByteArray

public final byte[] toByteArray()
Returns the ByteRefs data as a new byte array.

Returns:
the ByteRefs data as a new byte array.

toUpperCase

public final ByteRef toUpperCase()
Converts lower case letters into lower case letters. Convertion is done in place!

Returns:
this

toLowerCase

public final ByteRef toLowerCase()
Converts upper case letters into lower case letters. Convertion is done in place!

Returns:
this

lastIndexOf

public final int lastIndexOf(int z)
Returns the last index of the specified byte. Search starts et end of data. Is same as br.lastIndexOf(z, br.length()).

Parameters:
z - the searched byte
Returns:
the last index of the specified byte, or -1 when not found.

lastIndexOf

public final int lastIndexOf(int z,
                             int off)
Returns the last index of the specified byte.

Parameters:
z - the searched byte
off - the offset (from begin) where search starts
Returns:
the last index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(int z)
Returns the first index of the specified byte. Search starts et begin of data. Is same as br.indexOf(z, 0).

Parameters:
z - the searched byte
Returns:
the first index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(int z,
                         int off)
Returns the first index of the specified byte. Search starts et begin of data. Is same as br.indexOf(z, 0).

Parameters:
z - the searched byte
off - the offset (from begin) where search starts
Returns:
the first index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(java.lang.String s)
Returns the first index of the specified String's bytes. Search starts et begin of data. Is same as br.indexOf(s, 0).

Parameters:
s - the searched String
Returns:
the first index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(java.lang.String s,
                         int off)
Returns the first index of the specified String's bytes. Search starts et begin of data. Is same as br.indexOf(s, 0).

Parameters:
s - the searched String
off - the offset (from begin) where search starts
Returns:
the first index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(ByteRef s,
                         int off)
Returns the first index of the specified ByteRef's bytes. Search starts et begin of data.

Parameters:
s - the searched ByteRef
off - the offset (from begin) where search starts
Returns:
the first index of the specified byte, or -1 when not found.

indexOf

public final int indexOf(ByteRef s)
Returns the first index of the specified ByteRef's bytes. Search starts et begin of data. Is same as br.indexOf(s, 0).

Parameters:
s - the searched ByteRef
Returns:
the first index of the specified byte, or -1 when not found.

substring

public final ByteRef substring(int a,
                               int z)
Returns a new BytRef object using only the specified part of this ByteRef.

Parameters:
a - begin of used bytes
z - end of used bytes
Returns:
a new BytRef object using only the specified part of this ByteRef.

substring

public final ByteRef substring(int a)
Returns a new BytRef object using only the specified part of this ByteRef, up to end of this ByteRef.

Parameters:
a - begin of used bytes
Returns:
a new BytRef object using only the specified part of this ByteRef.

charAt

public final int charAt(int idx)
Returns the byte at the specified position.

Parameters:
idx - index of the byte
Returns:
the byte at the specified position.

hashCode

public final int hashCode()
Compute a hashCode for use in Hashtable.

Overrides:
hashCode in class java.lang.Object
Returns:
a hashcode.

equals

public final boolean equals(java.lang.Object _o)
Compares this objects data to another object.

Overrides:
equals in class java.lang.Object
Parameters:
_o - an Object
Returns:
true, when other object is also a BytRef and it contains same data.

equalsIgnoreCase

public final boolean equalsIgnoreCase(java.lang.Object _o)
Compares this objects data to another object ignoring upper and lower case.

Parameters:
_o - an Object
Returns:
true, when other object is also a BytRef and it contains same data.

compareTo

public final int compareTo(ByteRef o)
Compares this ByteRef's data to another ByteRef.

Parameters:
o - a ByteRef object
Returns:
0, when other ByteRef contains same data, -1 when this is less and 1 when this is greater then the other ByteRef.

endsWith

public final boolean endsWith(ByteRef o)
Tests whether this BytRef ends with the specified ByteRef.

Parameters:
o - the other ByteRef where the end is tested against
Returns:
true if this BytRef ends with the specified ByteRef

endsWith

public final boolean endsWith(java.lang.String s)
Tests whether this BytRef ends with the specified String.

Parameters:
s - the other String where the end is tested against
Returns:
true if this BytRef ends with the specified String

startsWith

public final boolean startsWith(ByteRef o)
Tests whether this BytRef starts with the specified ByteRef.

Parameters:
o - the other ByteRef where the end is tested against
Returns:
true if this BytRef starts with the specified ByteRef

startsWith

public final boolean startsWith(java.lang.String s)
Tests whether this BytRef starts with the specified String.

Parameters:
s - the other String where the end is tested against
Returns:
true if this BytRef starts with the specified String

writeTo

public final void writeTo(java.io.OutputStream os)
                   throws java.io.IOException
Write the bytes to the specified output stream.

Parameters:
os - a output stream
Throws:
java.io.IOException - on io errors

trim

public final ByteRef trim(int ch)
Remove all occurencies of the specified charater at begin and end.

Parameters:
ch - a character value
Returns:
a new ByteRef with the trimed data.

trim

public final ByteRef trim()
Remove all white spaces at begin and end.

Returns:
a new ByteRef with the trimed data.

toInteger

public final int toInteger()
Interpret the bytes as characters and convert them into an int value.

Returns:
an int value

update

public final ByteRef update(java.io.InputStream is)
Update this ByteRef with the availabledata of the specified input stream. current object's data is also updated!.

Parameters:
is - an input stream
Returns:
this on success with old and appended new data, or null on failure.

readLine

public static final ByteRef readLine(ByteRef br,
                                     java.io.InputStream is)
Extracts the next line from the specified ByteRef br. If there is no complete line in br, the ByteRef is updated using the specified input stream. This is done until a complete line is read, or br.update(is) fails.

Parameters:
br - the ByteRef used as input buffer
is - a input stream
Returns:
a new ByteRef containing the next line, or null on EOS.

clone

public final java.lang.Object clone()
Overrides:
clone in class java.lang.Object

splitLeft

public final ByteRef splitLeft(int stop)
Returns a ByteRef starting from 0 to stop, and removes this part from the current ByteRef. This ByteRef contains only the content from stop to end. This is a self modifying method!!

Parameters:
stop - end position
Returns:
a ByteRef as substring(0, stop) but modifies this object.

splitLeft

public final ByteRef splitLeft(int start,
                               int stop)
Returns a ByteRef starting from start to stop, and removes this part from the current ByteRef. This ByteRef contains only the content from stop to end. This is a self modifying method!!

Parameters:
start - start position for returned ByteRef
stop - stop position for returned ByteRef
Returns:
a ByteRef as substring(start, stop) but modifies this object.

removeLeft

public final int removeLeft()
Remove the first byte and return it as int >= 0 or return -1; This is a self modification.

Returns:
the first byte and return it as int >= 0 or return -1;

adjustLeft

public final void adjustLeft(int count)
Move the left offset by count bytes. This is a self modification.

Parameters:
count - the count of bytes to remove from left side.