Source Code Cross Referenced for Utils.java in  » Portal » Open-Portal » com » sun » portal » kssl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.kssl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.