danger.util
Class ByteArray

java.lang.Object
  |
  +--danger.util.ByteArray

public final class ByteArray
extends java.lang.Object

Utilities for accessing instances of byte[]. Note: Many of these methods are performed in native code on a device.


Field Summary
static char UNICODE_REPLACEMENT_CHARACTER
          standard Unicode replacement character for bogus data
 
Method Summary
static int compareTo(byte[] bytesA, int offsetA, int lenA, byte[] bytesB, int offsetB, int lenB)
          Compare two ranges of bytes in arrays, returning one of {-1, 0, 1} to indicate whether (respectively) the first is "less than," "equal to," or "greater than" the second.
static boolean contains(byte[] bytes, int offset, int len, byte[] key)
           
static boolean containsIgnoreCase(byte[] bytes, int offset, int len, byte[] keyUpperCase)
           
static int decodeUtf8Char(byte[] bytes, int index, int maxIndex)
          Decode a UTF-8 character from the given byte array at the given index, but not using bytes at or beyond the given maximum index.
static boolean equals(byte[] bytesA, byte[] bytesB)
          Compare two instances of byte[] for equality.
static int readInt(byte[] bytes, int offset)
          Read an int out of a byte[] at the given offset, in little-endian form (that is, low-order byte first).
static int readShort(byte[] bytes, int offset)
          Read a short out of a byte[] at the given offset, in little-endian form (that is, low-order byte first).
static boolean startsWith(byte[] bytes, int offset, int len, byte[] key)
           
static boolean startsWithIgnoreCase(byte[] bytes, int offset, int len, byte[] key)
           
static void writeInt(byte[] bytes, int offset, int value)
          Write an int to byte[] at the given offset, in little-endian form (that is, low-order byte first).
static void writeShort(byte[] bytes, int offset, int value)
          Write a short to byte[] at the given offset, in little-endian form (that is, low-order byte first).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNICODE_REPLACEMENT_CHARACTER

public static final char UNICODE_REPLACEMENT_CHARACTER
standard Unicode replacement character for bogus data
Method Detail

readInt

public static int readInt(byte[] bytes,
                          int offset)
Read an int out of a byte[] at the given offset, in little-endian form (that is, low-order byte first).
Parameters:
bytes - non-null; the array to read from
offset - the offset to start reading at
Returns:
the decoded int
Throws:
ArrayIndexOutOfBoundsException - thrown if offset is out of range
java.lang.NullPointerException - thrown of bytes is passed as null

readShort

public static int readShort(byte[] bytes,
                            int offset)
Read a short out of a byte[] at the given offset, in little-endian form (that is, low-order byte first).
Parameters:
bytes - non-null; the array to read from
offset - the offset to start reading at
Returns:
the decoded short
Throws:
ArrayIndexOutOfBoundsException - thrown if offset is out of range
java.lang.NullPointerException - thrown of bytes is passed as null

writeInt

public static void writeInt(byte[] bytes,
                            int offset,
                            int value)
Write an int to byte[] at the given offset, in little-endian form (that is, low-order byte first). If the offset is out of range, then an exception is thrown and the array is left unaltered.
Parameters:
bytes - non-null; the array to store into
offset - the offset to start storing at
value - the value to store
Throws:
ArrayIndexOutOfBoundsException - thrown if offset is out of range
java.lang.NullPointerException - thrown of bytes is passed as null

writeShort

public static void writeShort(byte[] bytes,
                              int offset,
                              int value)
Write a short to byte[] at the given offset, in little-endian form (that is, low-order byte first). If the offset is out of range, then an exception is thrown and the array is left unaltered.
Parameters:
bytes - non-null; the array to store into
offset - the offset to start storing at
value - the value to store
Throws:
ArrayIndexOutOfBoundsException - thrown if offset is out of range
java.lang.NullPointerException - thrown of bytes is passed as null

compareTo

public static int compareTo(byte[] bytesA,
                            int offsetA,
                            int lenA,
                            byte[] bytesB,
                            int offsetB,
                            int lenB)
Compare two ranges of bytes in arrays, returning one of {-1, 0, 1} to indicate whether (respectively) the first is "less than," "equal to," or "greater than" the second. If one of the arrays is passed as null, then it is considered to be "less than" the other, unless the other one is also null, in which case the two are considered equal. Otherwise, corresponding bytes are compared, starting from early offsets, up to the smaller of the two lengths passed. Whichever array contains a byte that is smaller than the other is considered "less than" the other. If all bytes compared are equal, then the range with the smaller length is considered "less than" the other. If the two lengths are the same, then the ranges are considered "equal."
Parameters:
bytesA - null-ok; an array to compare
offsetA - the offset to start at in bytesA
lenA - the number of bytes in the range in bytesA
bytesB - null-ok; another array to compare
offsetB - the offset to start at in bytesB
lenB - the number of bytes in the range in bytesB
Returns:
one of {-1, 0, 1} to indicate whether (respectively) the first range is "less than," "equal to," or "greater than" the second
Throws:
ArrayIndexOutOfBoundsException - thrown if the arguments end up demanding that an out-of-range element be accessed

equals

public static boolean equals(byte[] bytesA,
                             byte[] bytesB)
Compare two instances of byte[] for equality. They are considered equal if either: both are null; both are non-null, of the same length, and with equal contents.
Parameters:
bytesA - null-ok; an array to compare
bytesB - null-ok; an array to compare it to
Returns:
true if and only if the two arrays are considered "equal" to each other

startsWith

public static boolean startsWith(byte[] bytes,
                                 int offset,
                                 int len,
                                 byte[] key)

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(byte[] bytes,
                                           int offset,
                                           int len,
                                           byte[] key)

contains

public static boolean contains(byte[] bytes,
                               int offset,
                               int len,
                               byte[] key)

containsIgnoreCase

public static boolean containsIgnoreCase(byte[] bytes,
                                         int offset,
                                         int len,
                                         byte[] keyUpperCase)

decodeUtf8Char

public static int decodeUtf8Char(byte[] bytes,
                                 int index,
                                 int maxIndex)
Decode a UTF-8 character from the given byte array at the given index, but not using bytes at or beyond the given maximum index. The return value comes in two 16-bit halves; the lower half is the decoded character, and the upper half is the number of bytes consumed.

Note: This decodes in exactly the same way that String decodes, including error cases.

Parameters:
bytes - non-null; bytes to decode from
index - the start index for the character
maxIndex - the limit of decoding (exclusive)
Returns:
(bytesConsumed << 16) | decodedCharacter


Danger Confidential. Copyright © 2000-2003, Danger Inc.