FileDocCategorySizeDatePackage
Util.javaAPI DocphoneME MR2 API (J2ME)5164Wed May 02 18:00:24 BST 2007com.sun.midp.crypto

Util

public class Util extends Object
This class defines some static utility methods.

Fields Summary
private static char[]
hc
Hexadecimal character digits (0-f).
Constructors Summary
Methods Summary
public static voidcheckBounds(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
Verifies that method parameters are correct. NOTE: This method accepts too big outputOffset values - cipher methods must throw ShortBufferException if there are output data.

param
input the input buffer
param
inputOffset the offset in input where the input starts
param
inputLen the input length
param
output the buffer for the result
param
outputOffset the offset in output where the result is stored
throws
IllegalArgumentException if parameters are wrong.


        if (inputLen != 0 && (input == null || inputOffset < 0 ||
                inputLen < 0 || inputOffset + inputLen > input.length)) {
            throw new IllegalArgumentException("input out of bounds");
        }

        if (output == null || outputOffset < 0) {
            throw new IllegalArgumentException("output out of bounds");
        }
    
public static byte[]cloneArray(byte[] array)
Creates a copy of byte array.

param
array the byte array to be copied
return
the copy of the array

        byte[] data = new byte[array.length];
        System.arraycopy(array, 0, data, 0, array.length);
        return data;
    
public static byte[]cloneSubarray(byte[] array, int offset, int len)
Creates a copy of byte array.

param
array the byte array to be copied
param
offset where to begin
param
len how many bytes to copy
return
the copy of the array

        byte[] data = new byte[len];
        System.arraycopy(array, 0, data, offset, len);
        return data;
    
public static intgetInt(byte[] data, int offset)
Returns 4 bytes from the buffer as integer value.

param
data data array
param
offset value offset
return
the value

        int res = 0;
        for (int i = 0; i < 4; i++) {
            res = (res << 8) | (data[offset + i] & 0xff);
        }
        return res;
    
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 {
	    char[] r = new char[b.length << 1];
	    int v;
	    for (int i = 0, j = 0; i < b.length; i++) {
		v = b[i] & 0xff;
		r[j++] = hc[v >>> 4];
		r[j++] = hc[v & 0x0f];
	    }
	    return (new String(r));
	}
    
public static byte[]unpackBytes(java.lang.String src)
Unpacks data from string representation.

param
src packed data
return
unpacked data


        byte[] data = src.getBytes();
        int srcLen, len;

        byte[] res = new byte[len =
                  ((srcLen = data.length) >> 3) * 7 + (srcLen & 7) - 1];

        int i = 0;
        for (int k = len; k < srcLen; k++) {
            int mask = data[k];
            for (int j = 0; j < 7 && i < len; j++, i++) {
                res[i] = ((mask & (1 << j)) == 0) ? data[i] :
                              (byte) (data[i] | 0x80);
            }
        }
        return res;
    
public static voidxorArrays(byte[] a, byte[] b)
Performs an XOR operation of arrays elements. Results are placed into the first array.

param
a the first array
param
b the second array

        for (int i = 0; i < a.length; i++) {
            a[i] ^= b[i];
        }