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

Utils

public class Utils extends Object
Various shared utilities for Links unit testing.

Fields Summary
Constructors Summary
private Utils()
Private constructor to prevent creation on instances.

    
Methods Summary
public static booleanbytesEqual(byte[] a, byte[] b)
Compares two byte arrays for equality.

        if (a == b) {
            // handles null == null
            return true;
        }

        if (a == null || b == null) {
            return false;
        }

        // a and b both non-null

        if (a.length != b.length) {
            return false;
        }

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

        return true;
    
public static booleanbytesEqual(byte[] a, int off, int len, byte[] b)
Compares a subrange of byte array a to the entirety of b.

        byte[] sub = new byte[len];
        System.arraycopy(a, off, sub, 0, len);
        return bytesEqual(sub, b);
    
public static byte[]extractBytes(java.lang.String str)
Utility method to fill in a byte array with some reasonable data, derived from the string passed as an argument.

        int len = str.length();
        byte[] data = new byte[len];

        for (int i = 0; i < len; i++) {
            data[i] = (byte)(str.charAt(i) & 0xff);
        }

        return data;
    
public static java.lang.ObjectfillMemory()
Fills memory and returns an object that refers to all of what was allocated. The intent is that all subsequent allocations will cause OutOfMemoryError until the reference to the returned object is discarded.

        byte[][] arrays = new byte[100][];
        int size = 1 << 30; 
        int idx = 0;

        while (true) {
            try {
                while (true) {
                    arrays[idx++] = new byte[size];
                    // p(size);
                }
            } catch (OutOfMemoryError oome) {
                if (size == 0) {
                    break;
                } else {
                    size /= 2;
                }
            }
        }
        return arrays;
    
public static voidfillRandom(byte[] ba)
Fills a byte array with random data.

        Random rand = new Random();
        int randInt = 0;   // current set of random data

        for (int i = 0; i < ba.length; i++) {
            int r = i % 4;
            if (r == 0) {
                randInt = rand.nextInt();
            }
            ba[i] = (byte)((randInt >> (8 * r)) & 0xFF);
        }
    
public static native voidforceGC()

public static native int[]getFreedRendezvousPoints()

public static native intgetRefCount(Link link)

static voidp(int intval)
Prints out the ASCII representation of a number without allocating any memory.

        int val = intval;
        int power = 1000000000;

        while (power > 0) {
            System.out.write(((val / power) % 10) + 48);
            power /= 10;
        }

        System.out.write(10);
        System.out.flush();
    
public static voidsleep(long ms)
Sleeps for the indicated number of milliseconds.

        try {
            Thread.sleep(ms);
        } catch (InterruptedException ignore) { }