001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.links;
028:
029: import java.util.Random;
030:
031: /**
032: * Various shared utilities for Links unit testing.
033: */
034: public class Utils {
035:
036: /**
037: * Sleeps for the indicated number of milliseconds.
038: */
039: public static void sleep(long ms) {
040: try {
041: Thread.sleep(ms);
042: } catch (InterruptedException ignore) {
043: }
044: }
045:
046: /**
047: * Utility method to fill in a byte array with some reasonable data,
048: * derived from the string passed as an argument.
049: */
050: public static byte[] extractBytes(String str) {
051: int len = str.length();
052: byte[] data = new byte[len];
053:
054: for (int i = 0; i < len; i++) {
055: data[i] = (byte) (str.charAt(i) & 0xff);
056: }
057:
058: return data;
059: }
060:
061: /**
062: * Compares two byte arrays for equality.
063: */
064: public static boolean bytesEqual(byte[] a, byte[] b) {
065: if (a == b) {
066: // handles null == null
067: return true;
068: }
069:
070: if (a == null || b == null) {
071: return false;
072: }
073:
074: // a and b both non-null
075:
076: if (a.length != b.length) {
077: return false;
078: }
079:
080: for (int i = 0; i < a.length; i++) {
081: if (a[i] != b[i]) {
082: return false;
083: }
084: }
085:
086: return true;
087: }
088:
089: /**
090: * Compares a subrange of byte array a to the entirety of b.
091: */
092: public static boolean bytesEqual(byte[] a, int off, int len,
093: byte[] b) {
094: byte[] sub = new byte[len];
095: System.arraycopy(a, off, sub, 0, len);
096: return bytesEqual(sub, b);
097: }
098:
099: /**
100: * Fills a byte array with random data.
101: */
102: public static void fillRandom(byte[] ba) {
103: Random rand = new Random();
104: int randInt = 0; // current set of random data
105:
106: for (int i = 0; i < ba.length; i++) {
107: int r = i % 4;
108: if (r == 0) {
109: randInt = rand.nextInt();
110: }
111: ba[i] = (byte) ((randInt >> (8 * r)) & 0xFF);
112: }
113: }
114:
115: /**
116: * Prints out the ASCII representation of a number without allocating any
117: * memory.
118: */
119: static void p(int intval) {
120: int val = intval;
121: int power = 1000000000;
122:
123: while (power > 0) {
124: System.out.write(((val / power) % 10) + 48);
125: power /= 10;
126: }
127:
128: System.out.write(10);
129: System.out.flush();
130: }
131:
132: /**
133: * Fills memory and returns an object that refers to all of what was
134: * allocated. The intent is that all subsequent allocations will cause
135: * OutOfMemoryError until the reference to the returned object is
136: * discarded.
137: */
138: public static Object fillMemory() {
139: byte[][] arrays = new byte[100][];
140: int size = 1 << 30;
141: int idx = 0;
142:
143: while (true) {
144: try {
145: while (true) {
146: arrays[idx++] = new byte[size];
147: // p(size);
148: }
149: } catch (OutOfMemoryError oome) {
150: if (size == 0) {
151: break;
152: } else {
153: size /= 2;
154: }
155: }
156: }
157: return arrays;
158: }
159:
160: /**
161: * Private constructor to prevent creation on instances.
162: */
163: private Utils() {
164: }
165:
166: public static native void forceGC();
167:
168: public static native int[] getFreedRendezvousPoints();
169:
170: public static native int getRefCount(Link link);
171: }
|