net.sourceforge.jcapi.util
Class ByteArray

java.lang.Object
  |
  +--net.sourceforge.jcapi.util.ByteArray
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class ByteArray
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

ByteArray encapsulates an array of bytes. It offers several methods for byte-arrays useful to read or generate datastreams.

© 2002 Holger Gräfe

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Contact to author: holger.graefe@gmx.de

Project website: http://jcapi.sourceforge.net

See Also:
Serialized Form

Constructor Summary
ByteArray()
          creates an empty ByteArray.
ByteArray(byte[] ba)
          creates an ByteArray "around" the given array of bytes.
ByteArray(ByteArray BA, int beginIndex)
          creates an ByteArray with a copy the last elements of another ByteArray.
ByteArray(ByteArray BA, int beginIndex, int count)
          creates an ByteArray with a copy of some elements of another ByteArray.
ByteArray(int count)
          creates an ByteArray with the specified number of elements.
ByteArray(java.lang.String s)
          creates an ByteArray with the byte array presentation of the given string.
 
Method Summary
 byte byteAt(int index)
          returns the byte at the given index.
 java.lang.Object clone()
           
static int compare(byte[] ba1, byte[] ba2)
          compares two arrays of bytes.
 ByteArray concat(byte[] ba)
          creates a new ByteArray of a copy of this and the given bytes.
 ByteArray concat(ByteArray BA)
          creates a new ByteArray of a copy of this and the given ByteArray.
static void copyBytes(byte[] source, byte[] target)
          copies all bytes from one array to another.
static void copyBytes(byte[] source, byte[] target, int targetIndex)
          copies all bytes from one array to another.
static void copyBytes(byte[] source, int srcIndex, int count, byte[] target)
          copies selected bytes from one array to the first bytes of another.
static void copyBytes(byte[] source, int srcIndex, int count, byte[] target, int targetIndex)
          copies selected bytes from one array to another.
 boolean equals(java.lang.Object BA)
           
 byte[] getBytes()
          returns the byte array capsulated by this ByteArray (as reference).
static byte[] getBytes(byte[] source, int srcIndex)
          returns a new array with selected bytes from the given array.
static byte[] getBytes(byte[] source, int srcIndex, int count)
          returns a new array with selected bytes from the given array.
static int getHighOrderInt(byte[] source, int srcIndex, int count)
          returns the numerical value of the big endian byte[]-presentation.
static long getHighOrderLong(byte[] source, int srcIndex, int count)
          returns the numerical value of the big endian byte[]-presentation.
static int getLowOrderInt(byte[] source, int srcIndex, int count)
          returns the numerical value of the little endian byte[]-presentation.
static long getLowOrderLong(byte[] source, int srcIndex, int count)
          returns the numerical value of the little endian byte[]-presentation.
 int hashCode()
           
static java.lang.String hexString(byte[] binArray)
          generates an hexadecimal presentation of a byte array.
static java.lang.String hexString(byte[] binArray, int beginIndex, int endIndex)
          generates an hexadecimal presentation of a byte array.
static java.lang.String hexString(byte[] binArray, int beginIndex, int endIndex, java.lang.String separator)
          generates an hexadecimal presentation of a byte array.
static java.lang.String hexString(byte[] binArray, java.lang.String separator)
          generates an hexadecimal presentation of a byte array.
static byte[] highOrderBytes(long value)
          returns the big endian byte[4]-presentation of a numerical value.
static byte[] highOrderBytes(long lvalue, int numBytes)
          returns the big endian byte[]-presentation of a numerical value.
 int highOrderIntAt(int beginIndex)
          returns the numerical value of the big endian byte[4]-presentation.
 int highOrderIntAt(int beginIndex, int count)
          returns the numerical value of the big endian byte[]-presentation.
 int indexOf(byte searchByte)
          searches the occurance of a byte inside this ByteArray.
 int indexOf(byte[] search)
          searches the occurance of a byte array inside this ByteArray.
 int indexOf(byte[] search, int beginIndex)
          searches the occurance of a byte array inside this ByteArray.
 int indexOf(java.lang.String search)
          searches the occurance of the bytes of the given string inside this ByteArray.
 int indexOf(java.lang.String search, int beginIndex)
          searches the occurance of the bytes of the given string inside this ByteArray.
 int lastIndexOf(byte searchByte)
          searches the last occurance of a byte inside this ByteArray.
 int lastIndexOf(byte[] search)
          searches the last occurance of a byte array inside this ByteArray.
 int lastIndexOf(byte[] search, int beginIndex)
          searches the last occurance of a byte array inside this ByteArray.
 int lastIndexOf(java.lang.String search)
          searches the last occurance of the bytes of the given string inside this ByteArray.
 int lastIndexOf(java.lang.String search, int beginIndex)
          searches the last occurance of the bytes of the given string inside this ByteArray.
 int length()
          returns the number of bytes in this ByteArray
static byte[] lowOrderBytes(long value)
          returns the little endian byte[4]-presentation of a numerical value.
static byte[] lowOrderBytes(long lvalue, int numBytes)
          returns the little endian byte[]-presentation of a numerical value.
 int lowOrderIntAt(int beginIndex)
          returns the numerical value of the little endian byte[4]-presentation.
 int lowOrderIntAt(int beginIndex, int count)
          returns the numerical value of the little endian byte[]-presentation.
static java.lang.String octalString(byte[] binArray, int beginIndex, int endIndex, java.lang.String separator)
          generates an octal presentation of a byte array.
static byte[] parseHex(java.lang.String hex)
          parses a string of hexadecimal digits into an new byte array.
static int search(byte[] byteArray, byte[] searchArray)
          searches the occurance of a byte array inside another.
static int search(byte[] byteArray, byte[] searchArray, int beginIndex)
          searches the occurance of a byte array inside another.
static int search(byte[] byteArray, byte[] searchArray, int beginIndex, boolean reverse)
          searches the occurance of a byte array inside another.
 java.lang.String toHexString()
          generates an hexadecimal presentation of the ByteArray.
 java.lang.String toHexString(java.lang.String separator)
          generates an hexadecimal presentation of the ByteArray.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteArray

public ByteArray()
creates an empty ByteArray.

ByteArray

public ByteArray(int count)
creates an ByteArray with the specified number of elements.
Parameters:
count - number of elements

ByteArray

public ByteArray(byte[] ba)
creates an ByteArray "around" the given array of bytes.
Parameters:
ba - array of bytes. This is not copied but referenced as object.

ByteArray

public ByteArray(java.lang.String s)
creates an ByteArray with the byte array presentation of the given string.
Parameters:
s - the string to get the bytes from.

ByteArray

public ByteArray(ByteArray BA,
                 int beginIndex,
                 int count)
creates an ByteArray with a copy of some elements of another ByteArray.
Parameters:
BA - the ByteArray to get the bytes from.
beginIndex - the first byte to be copied.
count - the number of bytes to be copied.

ByteArray

public ByteArray(ByteArray BA,
                 int beginIndex)
creates an ByteArray with a copy the last elements of another ByteArray.
Parameters:
BA - the ByteArray to get the bytes from.
beginIndex - the first byte to be copied.
Method Detail

hexString

public static java.lang.String hexString(byte[] binArray,
                                         int beginIndex,
                                         int endIndex,
                                         java.lang.String separator)
generates an hexadecimal presentation of a byte array. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Parameters:
binArray - the byte array
beginIndex - first byte to be written to output string
endIndex - first byte not to be written
separator - string to be inserted between the hexadecimal presentation of the bytes
Returns:
lower case hexadecimal string

octalString

public static java.lang.String octalString(byte[] binArray,
                                           int beginIndex,
                                           int endIndex,
                                           java.lang.String separator)
generates an octal presentation of a byte array. This method uses a 'fast' implemenatation instead of calling Integer.toOctalString()
Parameters:
binArray - the byte array
beginIndex - first byte to be written to output string
endIndex - first byte not to be written
separator - string to be inserted between the octal presentation of the bytes
Returns:
octal string

hexString

public static java.lang.String hexString(byte[] binArray,
                                         int beginIndex,
                                         int endIndex)
generates an hexadecimal presentation of a byte array. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Parameters:
binArray - the byte array
beginIndex - first byte to be written to output string
endIndex - first byte not to be written
Returns:
lower case hexadecimal string

hexString

public static java.lang.String hexString(byte[] binArray,
                                         java.lang.String separator)
generates an hexadecimal presentation of a byte array. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Parameters:
binArray - the byte array
separator - string to be inserted between the hexadecimal presentation of the bytes
Returns:
lower case hexadecimal string

hexString

public static java.lang.String hexString(byte[] binArray)
generates an hexadecimal presentation of a byte array. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Parameters:
binArray - the byte array
Returns:
lower case hexadecimal string

lowOrderBytes

public static byte[] lowOrderBytes(long lvalue,
                                   int numBytes)
returns the little endian byte[]-presentation of a numerical value. This means the first byte in the array to hold the bits 2^0 to 2^7 of the value and so on.
Parameters:
value - the numeric value to be splitted in bytes
numBytes - the number of bytes in the resulting array. It is not checked if the given value 'fits' this array.
Returns:
the byte-array

lowOrderBytes

public static byte[] lowOrderBytes(long value)
returns the little endian byte[4]-presentation of a numerical value. This means the first byte in the array to hold the bits 2^0 to 2^7 of the value and so on.
Parameters:
value - the numeric value to be splitted in 4 bytes
Returns:
the array of 4 bytes

highOrderBytes

public static byte[] highOrderBytes(long lvalue,
                                    int numBytes)
returns the big endian byte[]-presentation of a numerical value. This means the first byte in the array to hold value>>(8*(numBytes-1)) and so on.
Parameters:
value - the numeric value to be splitted in bytes
numBytes - the number of bytes in the resulting array. It is not checked if the given value 'fits' this array.
Returns:
the byte-array

highOrderBytes

public static byte[] highOrderBytes(long value)
returns the big endian byte[4]-presentation of a numerical value. This means the first byte in the array to hold value>>24 and so on.
Parameters:
value - the numeric value to be splitted in 4 bytes
Returns:
the array of 4 bytes

copyBytes

public static void copyBytes(byte[] source,
                             int srcIndex,
                             int count,
                             byte[] target,
                             int targetIndex)
copies selected bytes from one array to another. Source and target may be the same array, even if there is an intersection area.
Parameters:
source - the source array
srcIndex - the index of the first byte in the source array
count - the number of bytes to be copied
target - the target array
targetIndex - the index where the first byte is to be placed in the target array

copyBytes

public static void copyBytes(byte[] source,
                             byte[] target,
                             int targetIndex)
copies all bytes from one array to another. Source and target may be the same array, even if there is an intersection area.
Parameters:
source - the source array
target - the target array
targetIndex - the index where the first byte is to be placed in the target array

copyBytes

public static void copyBytes(byte[] source,
                             int srcIndex,
                             int count,
                             byte[] target)
copies selected bytes from one array to the first bytes of another. Source and target may be the same array, even if there is an intersection area.
Parameters:
source - the source array
srcIndex - the index of the first byte in the source array
count - the number of bytes to be copied
target - the target array

copyBytes

public static void copyBytes(byte[] source,
                             byte[] target)
copies all bytes from one array to another.
Parameters:
source - the source array
target - the target array

getBytes

public static byte[] getBytes(byte[] source,
                              int srcIndex,
                              int count)
returns a new array with selected bytes from the given array.
Parameters:
source - the source array
srcIndex - the index of the first byte in the source array
count - the number of bytes to be copied
Returns:
a new array with the selected bytes

getBytes

public static byte[] getBytes(byte[] source,
                              int srcIndex)
returns a new array with selected bytes from the given array.
Parameters:
source - the source array
srcIndex - the index of the first byte in the source array
Returns:
a new array with the selected bytes

getHighOrderInt

public static int getHighOrderInt(byte[] source,
                                  int srcIndex,
                                  int count)
returns the numerical value of the big endian byte[]-presentation. This means the first byte in the array to be interpreted as returnValue>>(8*(numBytes-1)) and so on.
Parameters:
source - the array containig the big endian presentation
srcIndex - the index of the first (most significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

getLowOrderInt

public static int getLowOrderInt(byte[] source,
                                 int srcIndex,
                                 int count)
returns the numerical value of the little endian byte[]-presentation. This means the first byte in the array to be interpreted as the bits 2^0 to 2^7 of the return value and so on.
Parameters:
source - the array containig the little endian presentation
srcIndex - the index of the first (least significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

getHighOrderLong

public static long getHighOrderLong(byte[] source,
                                    int srcIndex,
                                    int count)
returns the numerical value of the big endian byte[]-presentation. This means the first byte in the array to be interpreted as returnValue>>(8*(numBytes-1)) and so on.
Parameters:
source - the array containig the big endian presentation
srcIndex - the index of the first (most significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

getLowOrderLong

public static long getLowOrderLong(byte[] source,
                                   int srcIndex,
                                   int count)
returns the numerical value of the little endian byte[]-presentation. This means the first byte in the array to be interpreted as the bits 2^0 to 2^7 of the return value and so on.
Parameters:
source - the array containig the little endian presentation
srcIndex - the index of the first (least significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

parseHex

public static byte[] parseHex(java.lang.String hex)
parses a string of hexadecimal digits into an new byte array. The string may be upper or lower case; white spaces at the beginning or end will be ignored.
Parameters:
hexString - the string of hexadecimal digits
Returns:
the array of bytes

compare

public static int compare(byte[] ba1,
                          byte[] ba2)
compares two arrays of bytes.
Parameters:
ba1 - the first byte array
ba2 - the second byte array
Returns:
the index of the first difference, i.e. length of arrays if equal.

search

public static int search(byte[] byteArray,
                         byte[] searchArray,
                         int beginIndex,
                         boolean reverse)
searches the occurance of a byte array inside another. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
byteArray - the array to search in
searchArray - the array to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
reverse - true for reverse search
Returns:
the position of the first (reverse=false) or last (reverse=true) occurance of the searched bytes or -1 for no match.

search

public static int search(byte[] byteArray,
                         byte[] searchArray,
                         int beginIndex)
searches the occurance of a byte array inside another. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
byteArray - the array to search in
searchArray - the array to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

search

public static int search(byte[] byteArray,
                         byte[] searchArray)
searches the occurance of a byte array inside another. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
byteArray - the array to search in
searchArray - the array to search for. If this is empty the result will be 0.
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

length

public int length()
returns the number of bytes in this ByteArray
Returns:
the number of bytes.

toHexString

public java.lang.String toHexString()
generates an hexadecimal presentation of the ByteArray. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Returns:
lower case hexadecimal string

toHexString

public java.lang.String toHexString(java.lang.String separator)
generates an hexadecimal presentation of the ByteArray. The characters 'a' to 'f' are written in lower case (Interger.valueOf(String s,int radix) compatible). This method uses a 'fast' implemenatation instead of calling Integer.toHexString()
Parameters:
separator - string to be inserted between the hexadecimal presentation of the bytes
Returns:
lower case hexadecimal string

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getBytes

public byte[] getBytes()
returns the byte array capsulated by this ByteArray (as reference). This doesn't copy the array, i.e. changes to the returned array will change the ByteArray object!
Returns:
the array of bytes.

concat

public ByteArray concat(ByteArray BA)
creates a new ByteArray of a copy of this and the given ByteArray.
Parameters:
BA - the bytes of this ByteArray will be put at the end of the new ByteArray
Returns:
the new ByteArray.

concat

public ByteArray concat(byte[] ba)
creates a new ByteArray of a copy of this and the given bytes.
Parameters:
ba - this bytes will be put at the end of the new ByteArray
Returns:
the new ByteArray.

byteAt

public byte byteAt(int index)
returns the byte at the given index.
Parameters:
index - the position of the byte to be returned.
Returns:
the byte at the given index.

indexOf

public int indexOf(byte[] search)
searches the occurance of a byte array inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the array to search for. If this is empty the result will be 0.
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

indexOf

public int indexOf(java.lang.String search)
searches the occurance of the bytes of the given string inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the string to search for. If this is empty the result will be 0.
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

indexOf

public int indexOf(byte[] search,
                   int beginIndex)
searches the occurance of a byte array inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the array to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

indexOf

public int indexOf(java.lang.String search,
                   int beginIndex)
searches the occurance of the bytes of the given string inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the string to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
Returns:
the position of the first occurance of the searched bytes or -1 for no match.

indexOf

public int indexOf(byte searchByte)
searches the occurance of a byte inside this ByteArray. This implements the native algorithm.
Parameters:
searchByte - the byte to search for.
Returns:
the position of the first occurance of the searched byte or -1 for no match.

lastIndexOf

public int lastIndexOf(byte[] search)
searches the last occurance of a byte array inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the array to search for. If this is empty the result will be 0.
Returns:
the position of the last occurance of the searched bytes or -1 for no match.

lastIndexOf

public int lastIndexOf(java.lang.String search)
searches the last occurance of the bytes of the given string inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the string to search for. If this is empty the result will be 0.
Returns:
the position of the last occurance of the searched bytes or -1 for no match.

lastIndexOf

public int lastIndexOf(byte[] search,
                       int beginIndex)
searches the last occurance of a byte array inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the array to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
Returns:
the position of the last occurance of the searched bytes or -1 for no match.

lastIndexOf

public int lastIndexOf(java.lang.String search,
                       int beginIndex)
searches the last occurance of the bytes of the given string inside this ByteArray. This implements the algorithm of Knuth-Morris-Pratt.
Parameters:
search - the string to search for. If this is empty the result will be the value of beginIndex.
beginIndex - the position in byteArray to start search
Returns:
the position of the last occurance of the searched bytes or -1 for no match.

lastIndexOf

public int lastIndexOf(byte searchByte)
searches the last occurance of a byte inside this ByteArray. This implements the native algorithm.
Parameters:
searchByte - the byte to search for.
Returns:
the position of the last occurance of the searched byte or -1 for no match.

lowOrderIntAt

public int lowOrderIntAt(int beginIndex)
returns the numerical value of the little endian byte[4]-presentation. This means the first selected byte to be interpreted as the bits 2^0 to 2^7 of the return value and so on.
Parameters:
beginIndex - the index of the first (least significant) byte
Returns:
the integer value

lowOrderIntAt

public int lowOrderIntAt(int beginIndex,
                         int count)
returns the numerical value of the little endian byte[]-presentation. This means the first selected byte to be interpreted as the bits 2^0 to 2^7 of the return value and so on.
Parameters:
beginIndex - the index of the first (least significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

highOrderIntAt

public int highOrderIntAt(int beginIndex)
returns the numerical value of the big endian byte[4]-presentation. This means the first selected byte to be interpreted as returnValue>>(8*(numBytes-1)) and so on.
Parameters:
beginIndex - the index of the first (most significant) byte
Returns:
the integer value

highOrderIntAt

public int highOrderIntAt(int beginIndex,
                          int count)
returns the numerical value of the big endian byte[]-presentation. This means the first selected byte to be interpreted as returnValue>>(8*(numBytes-1)) and so on.
Parameters:
beginIndex - the index of the first (most significant) byte
count - the number of bytes to be interpreted
Returns:
the integer value

equals

public boolean equals(java.lang.Object BA)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

clone

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