com.amx.duet.tools.lang
Class ByteBuffer

java.lang.Object
  extended bycom.amx.duet.tools.lang.ByteBuffer

public class ByteBuffer
extends java.lang.Object

This is similar to the Java StringBuffer class except that it deals with data at the byte level.

Since:
AMXTools 1.0.0
Version:
1.1.0

Constructor Summary
ByteBuffer()
          Initializes a newly created byte[] object
ByteBuffer(byte[] original)
          Initializes a newly created byte[] object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
ByteBuffer(byte[] value, int offset, int size)
          Allocates a new byte[] that contains characters from a subarray of the byte array argument.
ByteBuffer(char[] value)
          Allocates a new byte[] so that it represents the sequence of characters currently contained in the character array argument.
ByteBuffer(int b)
          Initializes a newly created byte[] object so that it represents a single byte as the argument.
ByteBuffer(java.lang.String value)
          Allocates a new byte[] so that it represents the sequence of characters currently contained in the String argument.
 
Method Summary
 void append(byte[] value)
           
 void append(byte[] value, int offset, int size)
           
 void append(char[] value)
           
 void append(int b)
           
 void append(java.lang.String value)
           
 byte byteAt(int index)
          Return a byte at the specified index
 char charAt(int index)
          Return a character at the specified index
 void clear()
          Clear all of the contents of the ByteBuffer
 ByteBuffer delete()
          Removes the entire contents of this byte buffer from beginning to end.
 ByteBuffer delete(int len)
          Removes the characters in a substring of this ByteBuffer up to the specified length.
 ByteBuffer delete(int start, int end)
          Removes the characters in a substring of this ByteBuffer.
 boolean endsWith(java.lang.String str)
          Tests if this byte buffer ends with the specified suffix.
 boolean getBoolean(int index)
          Return the boolean value at the specified index
 char getChar(int index)
          Return the char value at the specified index
 double getDouble(int index)
          Return the double value at the specified index
 float getFloat(int index)
          Return the float value at the specified index
 int getInt(int index)
          Return the int value at the specified index
 long getLong(int index)
          Return the long value at the specified index
 short getShort(int index)
          Return the short value at the specified index
 java.lang.String getString(int index, int length)
          Return the double value at the specified index
 int indexof(byte[] value)
           
 int indexof(byte[] value, int fromIndex)
           
 int indexof(char[] value)
           
 int indexof(char[] value, int fromIndex)
           
 int indexof(int value)
          Returns the index within this string of the first occurrence of the specified byte.
 int indexof(int value, int fromIndex)
           
 int indexof(java.lang.String value)
          Returns the index within this ByteBuffer of the first occurrence of the specified substring.
 int indexof(java.lang.String value, int fromIndex)
           
 int length()
          Returns the current size of the buffer.
 ByteBuffer replace(byte[][] pattern, byte[][] replace)
          Replaces a sequence of patterns with a sequence of replace values.
 ByteBuffer replace(byte[] pattern, byte[] replace)
          Removes every instance of the passed in pattern from the ByteBuffer and replaces them with the replace pattern.
 ByteBuffer replace(int start, int end, byte[] value)
          Replaces the bytes in a substring of this ByteBuffer with bytes in the specified byte[].
 ByteBuffer replace(int start, int end, byte[][] pattern, byte[][] replace)
          Replaces a sequence of patterns with a sequence of replace values.
 ByteBuffer replace(int start, int end, byte[] pattern, byte[] replace)
          Removes every instance of the passed in pattern from the ByteBuffer and replaces them with the replace pattern.
 boolean startsWith(java.lang.String str)
          Tests if this byte buffer starts with the specified prefix.
 ByteBuffer substring(int start)
          Returns a new ByteBuffer that contains a subsequence of bytes currently contained in this ByteBuffer.The substring begins at the specified index and extends to the end of the ByteBuffer.
 ByteBuffer substring(int start, int end)
          Returns a new ByteBuffer that contains a subsequence of characters currently contained in this ByteBuffer.
 byte[] toByteArray()
           
static byte[] toBytes(boolean value)
          Converts a byte value to a byte array.
static byte[] toBytes(byte value)
          Converts a byte value to a byte array.
static byte[] toBytes(char value)
          Converts a char value to a byte array.
static byte[] toBytes(double value)
          Converts a double value to a byte array.
static byte[] toBytes(float value)
          Converts a float value to a byte array.
static byte[] toBytes(int value)
          Converts an int value to a byte array.
static byte[] toBytes(long value)
          Converts a long value to a byte array.
static byte[] toBytes(short value)
          Converts a short value to a byte array.
static byte[] toBytes(java.lang.String value)
          Converts a String value to a byte array.
 char[] toCharArray()
           
 java.lang.String toHex()
          Generates an ASCII hex string from the stored byte array.
 java.lang.String toString()
          Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 int UByteAt(int index)
          Return an 8-bit unsigned byte at the specified index
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteBuffer

public ByteBuffer()
Initializes a newly created byte[] object


ByteBuffer

public ByteBuffer(int b)
Initializes a newly created byte[] object so that it represents a single byte as the argument.

Parameters:
b - the initial value of the byte array

ByteBuffer

public ByteBuffer(byte[] original)
Initializes a newly created byte[] object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary.

Parameters:
original - a byte[].

ByteBuffer

public ByteBuffer(byte[] value,
                  int offset,
                  int size)
Allocates a new byte[] that contains characters from a subarray of the byte array argument. The offset argument is the index of the first character of the subarray and the size argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the byte array does not affect the newly created byte array.

Parameters:
value - array that is the source of bytes.
offset - the initial offset.
size - the length.

ByteBuffer

public ByteBuffer(java.lang.String value)
Allocates a new byte[] so that it represents the sequence of characters currently contained in the String argument. The contents of the string are copied; subsequent modification of the string does not affect the newly created byte array.

Parameters:
value - the initial value of the byte array

ByteBuffer

public ByteBuffer(char[] value)
Allocates a new byte[] so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created byte array.

Parameters:
value - the initial value of the byte array
Method Detail

append

public void append(int b)
Parameters:
b -

append

public void append(byte[] value)
Parameters:
value -

append

public void append(byte[] value,
                   int offset,
                   int size)
Parameters:
value -
offset -
size -

append

public void append(java.lang.String value)
Parameters:
value -

append

public void append(char[] value)
Parameters:
value -

byteAt

public byte byteAt(int index)
Return a byte at the specified index

Parameters:
index -
Returns:

UByteAt

public int UByteAt(int index)
Return an 8-bit unsigned byte at the specified index

Parameters:
index -
Returns:

charAt

public char charAt(int index)
Return a character at the specified index

Parameters:
index -
Returns:

getBoolean

public boolean getBoolean(int index)
Return the boolean value at the specified index

Parameters:
index -
Returns:

getChar

public char getChar(int index)
Return the char value at the specified index

Parameters:
index -
Returns:

getShort

public short getShort(int index)
Return the short value at the specified index

Parameters:
index -
Returns:

getInt

public int getInt(int index)
Return the int value at the specified index

Parameters:
index -
Returns:

getLong

public long getLong(int index)
Return the long value at the specified index

Parameters:
index -
Returns:

getFloat

public float getFloat(int index)
Return the float value at the specified index

Parameters:
index -
Returns:

getDouble

public double getDouble(int index)
Return the double value at the specified index

Parameters:
index -
Returns:

getString

public java.lang.String getString(int index,
                                  int length)
Return the double value at the specified index

Parameters:
index -
length -
Returns:

toByteArray

public byte[] toByteArray()
Returns:

toCharArray

public char[] toCharArray()
Returns:

toString

public java.lang.String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Returns:
String representation of a byte array

toHex

public java.lang.String toHex()
Generates an ASCII hex string from the stored byte array. ex. {0x1a, 0x1b, 0x1c} returns "1A1B1C"

Returns:
ASCII hex representation of a byte array

length

public int length()
Returns the current size of the buffer.

Returns:
the number of valid bytes in the byte array.

clear

public void clear()
Clear all of the contents of the ByteBuffer


indexof

public int indexof(int value)
Returns the index within this string of the first occurrence of the specified byte. If a byte with value value occurs in the byte sequence represented by this ByteBuffer object, then the index of the first such occurrence is returned -- that is, the smallest value k such that:
 this.byteAt(k) == value
 
is true. If no such byte occurs in this ByteBuffer, then -1 is returned.

Parameters:
value - a byte.
Returns:
the index of the first occurrence of the byte in the byte sequence represented by this object, or -1 if the byte does not occur.

indexof

public int indexof(int value,
                   int fromIndex)
Parameters:
value -
fromIndex -
Returns:

indexof

public int indexof(byte[] value)
Parameters:
value -
Returns:

indexof

public int indexof(byte[] value,
                   int fromIndex)
Parameters:
value -
fromIndex -
Returns:

indexof

public int indexof(char[] value)
Parameters:
value -
Returns:

indexof

public int indexof(char[] value,
                   int fromIndex)
Parameters:
value -
fromIndex -
Returns:

indexof

public int indexof(java.lang.String value)
Returns the index within this ByteBuffer of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
 this.startsWith(str, k)
 
is true.

Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
java.lang.NullPointerException - if str is null.
See Also:
String.indexOf(java.lang.String)

indexof

public int indexof(java.lang.String value,
                   int fromIndex)
Parameters:
value -
fromIndex -
Returns:

startsWith

public boolean startsWith(java.lang.String str)
Tests if this byte buffer starts with the specified prefix.

Parameters:
str - the prefix
Returns:
true if the character sequence represented by the argument is a prefix of the character sequence represented by this byte buffer; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this ByteBuffer object as determined by the equals(Object) method.

endsWith

public boolean endsWith(java.lang.String str)
Tests if this byte buffer ends with the specified suffix.

Parameters:
str - the suffix.
Returns:
true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this ByteBuffer object as determined by the equals(Object) method.

delete

public ByteBuffer delete()
Removes the entire contents of this byte buffer from beginning to end.

Returns:
An empty byte buffer.

delete

public ByteBuffer delete(int len)
Removes the characters in a substring of this ByteBuffer up to the specified length. The substring starts from the beginning and extends to the character at index len - 1 or to the end of the ByteBuffer if no such character exists.

Parameters:
len - The number of characters to remove from beginning to len.
Returns:
This byte buffer.

delete

public ByteBuffer delete(int start,
                         int end)
Removes the characters in a substring of this ByteBuffer. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the ByteBuffer if no such character exists. If start is equal to end, no changes are made.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
This byte buffer.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

replace

public ByteBuffer replace(int start,
                          int end,
                          byte[] value)
Replaces the bytes in a substring of this ByteBuffer with bytes in the specified byte[]. The substring begins at the specified start and extends to the byte at index end - 1 or to the end of the ByteBuffer if no such byte exists. First the byte in the substring are removed and then the specified byte[] is inserted at start. (The ByteBuffer will be lengthened to accommodate the specified byte[] if necessary.)

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
value - Byte[] that will replace previous contents.
Returns:
This byte buffer.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

replace

public ByteBuffer replace(byte[] pattern,
                          byte[] replace)
Removes every instance of the passed in pattern from the ByteBuffer and replaces them with the replace pattern.

Parameters:
pattern - Pattern to remove from source.
replace - Pattern to replace removed pattern with.
Returns:
the newly replaced bytes

replace

public ByteBuffer replace(int start,
                          int end,
                          byte[] pattern,
                          byte[] replace)
Removes every instance of the passed in pattern from the ByteBuffer and replaces them with the replace pattern.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
pattern - Pattern to remove from source.
replace - Pattern to replace removed pattern with.
Returns:
the newly replaced bytes

replace

public ByteBuffer replace(byte[][] pattern,
                          byte[][] replace)
Replaces a sequence of patterns with a sequence of replace values.

Parameters:
pattern - Patterns to remove from source.
replace - Patterns to replace removed patterns with.
Returns:
the newly replaced bytes

replace

public ByteBuffer replace(int start,
                          int end,
                          byte[][] pattern,
                          byte[][] replace)
Replaces a sequence of patterns with a sequence of replace values.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
pattern - Patterns to remove from source.
replace - Patterns to replace removed patterns with.
Returns:
the newly replaced bytes

substring

public ByteBuffer substring(int start)
Returns a new ByteBuffer that contains a subsequence of bytes currently contained in this ByteBuffer.The substring begins at the specified index and extends to the end of the ByteBuffer.

Parameters:
start - The beginning index, inclusive.
Returns:
The new ByteBuffer.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is less than zero, or greater than the length of this ByteBuffer.

substring

public ByteBuffer substring(int start,
                            int end)
Returns a new ByteBuffer that contains a subsequence of characters currently contained in this ByteBuffer. The substring begins at the specified start and extends to the byte at index end - 1. An exception is thrown if

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
The new ByteBuffer.
Throws:
java.lang.StringIndexOutOfBoundsException - if start or end are negative or greater than length(), or start is greater than end.

toBytes

public static byte[] toBytes(boolean value)
Converts a byte value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(byte value)
Converts a byte value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(char value)
Converts a char value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(short value)
Converts a short value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(int value)
Converts an int value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(long value)
Converts a long value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(float value)
Converts a float value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(double value)
Converts a double value to a byte array.

Parameters:
value -
Returns:

toBytes

public static byte[] toBytes(java.lang.String value)
Converts a String value to a byte array.

Parameters:
value -
Returns:


Copyright © 2008 AMX LLC. All Rights Reserved.