FileDocCategorySizeDatePackage
Utils.javaAPI DocphoneME MR2 API (J2ME)4881Wed May 02 18:00:26 BST 2007com.sun.midp.pki

Utils

public class Utils extends Object
This class implements miscellaneous utility methods including those used for conversion of BigIntegers to byte arrays, hexadecimal printing of byte arrays etc.

Fields Summary
private static char[]
hc
Hexadecimal digits.
Constructors Summary
Methods Summary
public static booleanbyteMatch(byte[] a, int aOff, byte[] b, int bOff, int len)
Checks if two byte arrays match.

param
a first byte array
param
aOff starting offset for comparison within a
param
b second byte array
param
bOff starting offset for comparison within b
param
len number of bytes to be compared
return
true if the sequence of len bytes in a starting at aOff matches those in b starting at bOff, false otherwise

        if ((a.length < aOff + len) ||
            (b.length < bOff + len)) return false;
        
        for (int i = 0; i < len; i++) {
            if (a[i + aOff] != b[i + bOff])
                return false;
        }

        return true;
    
public static java.lang.StringhexEncode(byte[] b, int off, int len)
Converts a subsequence of bytes in a byte array into a corresponding string of hexadecimal digits, each separated by a ":".

param
b byte array containing the bytes to be converted
param
off starting offset of the byte subsequence inside b
param
len number of bytes to be converted
return
a string of corresponding hexadecimal digits or an error string

    
                                                                        
             
        return new String(hexEncodeToChars(b, off, len));
    
public static java.lang.StringhexEncode(byte[] b)
Converts a byte array into a corresponding string of hexadecimal digits. This is equivalent to hexEncode(b, 0, b.length).

param
b byte array to be converted
return
corresponding hexadecimal string

        if (b == null)
            return ("");
        else 
            return hexEncode(b, 0, b.length);
    
public static char[]hexEncodeToChars(byte[] b, int off, int len)
Converts a subsequence of bytes in a byte array into a corresponding string of hexadecimal digits, each separated by a ":".

param
b byte array containing the bytes to be converted
param
off starting offset of the byte subsequence inside b
param
len number of bytes to be converted
return
a string of corresponding hexadecimal digits or an error string

        char[] r;
        int v;
        int i;
        int j;
        
        if ((b == null) || (len == 0)) {
            return new char[0];
        }

        if ((off < 0) || (len < 0)) {
            throw new ArrayIndexOutOfBoundsException();
        }

        if (len == 1) {
            r = new char[len * 2];
        } else {
            r = new char[(len * 3) - 1];
        }

        for (i = 0, j = 0; ; ) {
            v = b[off + i] & 0xff;
            r[j++] = hc[v >>> 4];
            r[j++] = hc[v & 0x0f];

            i++;
            if (i >= len) {
                break;
            }

            r[j++] = ':";
        }

        return r;
    
public static byte[]longToBytes(long n)
Converts a long value to a cooresponding 8-byte array starting with the most significant byte.

param
n 64-bit long integer value
return
a corresponding 8-byte array in network byte order

        byte[] b = new byte[8];
        
        for (int i = 0; i < 64; i += 8) {
            b[i >> 3] = (byte) ((n >> (56 - i)) & 0xff);
        }
        return b;