KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > utility > HexConverter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * HexConverter.java
26  *
27  * Create on March 3, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.utility;
31  
32
33 public class HexConverter
34 {
35     //
36
// This method is directly modeled on qqsp_Stream::dumpBytes
37
// and is used for similar purposes (e.g., CM Packet dumping)
38
//
39
private static final int AFTER_POS = 13; // one byte after position
40
private static final int HEX_START = AFTER_POS + 2;
41     private static final int WORDS_PER_LINE = 4;
42     private static final int CHARS_PER_WORD = 10;
43     private static final int CHAR_START = 56; // after 4 words of data
44

45     /**
46      * The simple version doesn't have a lot of formatting
47      */

48     public static String JavaDoc simpleFormatHexBytes (byte rawBytes[], int length)
49     {
50         int i;
51         String JavaDoc result = new String JavaDoc();
52         long data;
53
54         for (i = 0; i < length; i++)
55         {
56             data = (int)rawBytes[i] & 0xff;
57
58             if (i > 0)
59                 result = result.concat(" "); // NOI18N
60

61             if (Long.toHexString(data).length() == 1)
62                 result = result.concat("0"); // NOI18N
63
result = result.concat(Long.toHexString(data));
64         }
65
66         result = result.concat("\n"); // NOI18N
67
return result;
68     }
69         
70             
71
72     public static String JavaDoc formatHexBytes(byte rawBytes[], int length)
73     {
74         int i, j, bufPtr;
75         char []buf = new char[100];
76         int word = 0;
77         long data;
78         int startPos = 0;
79         String JavaDoc dataAsHex;
80         String JavaDoc resultString = new String JavaDoc();
81
82         // Clear out the buffer
83
for (j = 0; j < 100; j++)
84             buf[j] = ' ';
85
86         for (i = 0; i < length; i += 4)
87         {
88             // Print 4 bytes of data at a time.
89
bufPtr = (word * CHARS_PER_WORD) + HEX_START;
90
91             dataAsHex = new String JavaDoc();
92             for (j = 0; j < 4; j++)
93             {
94                 try
95                 {
96                     data = (int)rawBytes[i + j] & 0xff;
97                 }
98                 catch (ArrayIndexOutOfBoundsException JavaDoc e)
99                 {
100                     data = 0;
101                 }
102                 if (Long.toHexString(data).length() == 1)
103                     dataAsHex = dataAsHex.concat("0"); // NOI18N
104
dataAsHex = dataAsHex.concat(Long.toHexString(data));
105             }
106
107             for (j = 0; j < 8; j++)
108                 buf[bufPtr + j] = dataAsHex.charAt(j);
109
110             // Print the character translation at the end of the print line
111
bufPtr = CHAR_START + (word * 4);
112             for (j = 0; j < 4; j++)
113             {
114                 try
115                 {
116                     if (Character.isLetterOrDigit((char)rawBytes[i + j]))
117                         buf[bufPtr + j] = (char)rawBytes[i + j];
118                     else
119                         buf[bufPtr + j] = '.';
120                 }
121                 catch (ArrayIndexOutOfBoundsException JavaDoc e)
122                 {
123                     buf[bufPtr + j] = '_';
124                 }
125
126             }
127             word++;
128
129             // Print the line to the stream
130
if (i != 0 && (word % WORDS_PER_LINE) == 0)
131             {
132                 HexConverter.overlayOffsets(buf, startPos);
133
134                 startPos = i + 4;
135                 resultString = resultString.concat(new String JavaDoc(buf, 0,
136                                                CHAR_START + (word * 5)));
137                 resultString = resultString.concat("\n"); // NOI18N
138

139                 word = 0;
140
141                 // Clear out the buffer
142
for (j = 0; j < 100; j++)
143                     buf[j] = ' ';
144             }
145         }
146
147         // Print what remains of the last line
148
if (word != 0)
149         {
150             HexConverter.overlayOffsets(buf, startPos);
151
152             resultString = resultString.concat(new
153                                     String JavaDoc(buf, 0, CHAR_START + (word * 5)));
154         }
155
156         resultString = resultString.concat("\n"); // NOI18N
157

158         return resultString;
159     }
160
161     private static void overlayOffsets(char buf[], int startPos)
162     {
163         String JavaDoc startStr;
164         String JavaDoc startStrHex;
165         int j;
166
167         startStr = StringScanner.getIntStr(startPos);
168         startStrHex = Integer.toHexString(startPos);
169         for (j = 0; j < 6; j++)
170         {
171             if (j + startStr.length() >= 6)
172                 buf[j] = startStr.charAt(startStr.length() - (6 - j));
173             else
174                 buf[j] = ' ';
175         }
176         buf[6] = ' '; buf[7] = '0'; buf[8] = 'x';
177         for (j = 0; j < 4; j++)
178         {
179             if (j + startStrHex.length() >= 4)
180                 buf[j + 9] = startStrHex.charAt(
181                                  startStrHex.length() - (4 - j));
182             else
183                 buf[j + 9] = '0';
184         }
185     }
186 }
187
Popular Tags