001: /*
002: * @(#)Utils.java 1.15 02/07/24 @(#)
003: *
004: * Copyright (c) 2000-2002 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL
006: * Use is subject to license terms.
007: */
008:
009: package com.sun.portal.kssl;
010:
011: import java.math.*;
012:
013: /**
014: * This class implements miscellaneous utility methods including
015: * those used for event logging, conversion of BigIntegers to
016: * byte arrays, hexadecimal printing of byte arrays etc.
017: */
018: class Utils {
019: // Debug levels
020: /** Logging critical messages (5). */
021: static final byte LOG_CRIT = 5;
022: /** Logging error messages (4). */
023: static final byte LOG_ERR = 4;
024: /** Logging warning messages (3). */
025: static final byte LOG_WARN = 3;
026: /** Logging notice messages (2). */
027: static final byte LOG_NOTICE = 2;
028: /** Logging information messages (1). */
029: static final byte LOG_INFO = 1;
030: /** Logging debug messages (0). */
031: static final byte LOG_DEBUG = 0;
032:
033: // Only log messages with level greater than this are displayed
034: /** Current debug logging level (default = LOG_WARN). */
035: private static byte logLevel = LOG_WARN;
036: /** Hexadecimal digits. */
037: private static char[] hc = { '0', '1', '2', '3', '4', '5', '6',
038: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
039:
040: /**
041: * Converts a subsequence of bytes in a byte array into a
042: * corresponding string of hexadecimal digits, each separated by a ":".
043: *
044: * @param b byte array containing the bytes to be converted
045: * @param off starting offset of the byte subsequence inside b
046: * @param len number of bytes to be converted
047: * @return a string of corresponding hexadecimal digits or
048: * an error string
049: */
050: static String hexEncode(byte[] b, int off, int len) {
051: return new String(hexEncodeToChars(b, off, len));
052: }
053:
054: /**
055: * Converts a subsequence of bytes in a byte array into a
056: * corresponding string of hexadecimal digits, each separated by a ":".
057: *
058: * @param b byte array containing the bytes to be converted
059: * @param off starting offset of the byte subsequence inside b
060: * @param len number of bytes to be converted
061: * @return a string of corresponding hexadecimal digits or
062: * an error string
063: */
064: static char[] hexEncodeToChars(byte[] b, int off, int len) {
065: char[] r;
066: int v;
067: int i;
068: int j;
069:
070: if ((b == null) || (len == 0)) {
071: return new char[0];
072: }
073:
074: if ((off < 0) || (len < 0)) {
075: throw new ArrayIndexOutOfBoundsException();
076: }
077:
078: if (len == 1) {
079: r = new char[len * 2];
080: } else {
081: r = new char[(len * 3) - 1];
082: }
083:
084: for (i = 0, j = 0;;) {
085: v = b[off + i] & 0xff;
086: r[j++] = hc[v >>> 4];
087: r[j++] = hc[v & 0x0f];
088:
089: i++;
090: if (i >= len) {
091: break;
092: }
093:
094: r[j++] = ':';
095: }
096:
097: return r;
098: }
099:
100: /**
101: * Converts a byte array into a corresponding string of hexadecimal
102: * digits. This is equivalent to hexEncode(b, 0, b.length).
103: * <P />
104: * @param b byte array to be converted
105: * @return corresponding hexadecimal string
106: */
107: static String hexEncode(byte[] b) {
108: if (b == null)
109: return ("");
110: else
111: return hexEncode(b, 0, b.length);
112: }
113:
114: /**
115: * Converts a long value to a cooresponding 8-byte array
116: * starting with the most significant byte.
117: * <P />
118: * @param n 64-bit long integer value
119: * @return a corresponding 8-byte array in network byte order
120: */
121: static byte[] longToBytes(long n) {
122: byte[] b = new byte[8];
123:
124: for (int i = 0; i < 64; i += 8) {
125: b[i >> 3] = (byte) ((n >> (56 - i)) & 0xff);
126: }
127: return b;
128: }
129:
130: /**
131: * Checks if two byte arrays match.
132: * <P />
133: * @param a first byte array
134: * @param aOff starting offset for comparison within a
135: * @param b second byte array
136: * @param bOff starting offset for comparison within b
137: * @param len number of bytes to be compared
138: * @return true if the sequence of len bytes in a starting at
139: * aOff matches those in b starting at bOff, false otherwise
140: */
141: static boolean byteMatch(byte[] a, int aOff, byte[] b, int bOff,
142: int len) {
143: if ((a.length < aOff + len) || (b.length < bOff + len))
144: return false;
145:
146: for (int i = 0; i < len; i++) {
147: if (a[i + aOff] != b[i + bOff])
148: return false;
149: }
150:
151: return true;
152: }
153:
154: /**
155: * Logs the specified string (without a trailing new-line
156: * character) if the level specified equals or exceeds the
157: * logging threshold level.
158: * <P />
159: * @param lev indicates criticality of message to be logged
160: * @param msg string containing message to be logged
161: */
162: static void log(byte lev, String msg) {
163: if (lev < logLevel)
164: return;
165:
166: try {
167: System.out.print(msg);
168: } catch (Exception e) {
169: System.out.println("log() caught <" + e + ">");
170: }
171: }
172:
173: /**
174: * Logs the specified string (with a trailing new-line
175: * character) if the level specified equals or exceeds the
176: * logging threshold level.
177: * <P />
178: * @param lev indicates criticality of message to be logged
179: * @param msg string containing message to be logged
180: */
181: static void logln(byte lev, String msg) {
182: if (lev < logLevel)
183: return;
184:
185: try {
186: System.out.println(msg);
187: } catch (Exception e) {
188: System.out.println("logln() caught <" + e + ">");
189: }
190: }
191:
192: static byte[] bigInt2Bytes(BigInteger bi) {
193: return bi.toByteArray();
194: }
195: }
|