KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > poibrowser > Codec


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.contrib.poibrowser;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.apache.poi.hpsf.ClassID;
26
27
28
29 /**
30  * <p>Provides utility methods for encoding and decoding hexadecimal
31  * data.</p>
32  *
33  * @author Rainer Klute (klute@rainer-klute.de) - with portions from Tomcat
34  * @version $Id: Codec.java,v 1.7 2004/04/09 13:05:08 glens Exp $
35  * @since 2002-01-24
36  */

37 public class Codec
38 {
39
40     /**
41      * <p>The nibbles' hexadecimal values. A nibble is a half byte.</p>
42      */

43     protected static final byte hexval[] =
44         {(byte) '0', (byte) '1', (byte) '2', (byte) '3',
45          (byte) '4', (byte) '5', (byte) '6', (byte) '7',
46          (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
47          (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'};
48
49
50
51     /**
52      * <p>Converts a string into its hexadecimal notation.</p>
53      */

54     public static String JavaDoc hexEncode(final String JavaDoc s)
55     {
56         return hexEncode(s.getBytes());
57     }
58
59
60
61     /**
62      * <p>Converts a byte array into its hexadecimal notation.</p>
63      */

64     public static String JavaDoc hexEncode(final byte[] s)
65     {
66         return hexEncode(s, 0, s.length);
67     }
68
69
70
71     /**
72      * <p>Converts a part of a byte array into its hexadecimal
73      * notation.</p>
74      */

75     public static String JavaDoc hexEncode(final byte[] s, final int offset,
76                                    final int length)
77     {
78         StringBuffer JavaDoc b = new StringBuffer JavaDoc(length * 2);
79         for (int i = offset; i < offset + length; i++)
80         {
81             int c = s[i];
82             b.append((char) hexval[(c & 0xF0) >> 4]);
83             b.append((char) hexval[(c & 0x0F) >> 0]);
84         }
85         return b.toString();
86     }
87
88
89
90     /**
91      * <p>Converts a single byte into its hexadecimal notation.</p>
92      */

93     public static String JavaDoc hexEncode(final byte b)
94     {
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2);
96         sb.append((char) hexval[(b & 0xF0) >> 4]);
97         sb.append((char) hexval[(b & 0x0F) >> 0]);
98         return sb.toString();
99     }
100
101
102
103     /**
104      * <p>Converts a short value (16-bit) into its hexadecimal
105      * notation.</p>
106      */

107     public static String JavaDoc hexEncode(final short s)
108     {
109         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(4);
110         sb.append((char) hexval[(s & 0xF000) >> 12]);
111         sb.append((char) hexval[(s & 0x0F00) >> 8]);
112         sb.append((char) hexval[(s & 0x00F0) >> 4]);
113         sb.append((char) hexval[(s & 0x000F) >> 0]);
114         return sb.toString();
115     }
116
117
118
119     /**
120      * <p>Converts an int value (32-bit) into its hexadecimal
121      * notation.</p>
122      */

123     public static String JavaDoc hexEncode(final int i)
124     {
125         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(8);
126         sb.append((char) hexval[(i & 0xF0000000) >> 28]);
127         sb.append((char) hexval[(i & 0x0F000000) >> 24]);
128         sb.append((char) hexval[(i & 0x00F00000) >> 20]);
129         sb.append((char) hexval[(i & 0x000F0000) >> 16]);
130         sb.append((char) hexval[(i & 0x0000F000) >> 12]);
131         sb.append((char) hexval[(i & 0x00000F00) >> 8]);
132         sb.append((char) hexval[(i & 0x000000F0) >> 4]);
133         sb.append((char) hexval[(i & 0x0000000F) >> 0]);
134         return sb.toString();
135     }
136
137
138
139     /**
140      * <p>Converts a long value (64-bit) into its hexadecimal
141      * notation.</p>
142      */

143     public static String JavaDoc hexEncode(final long l)
144     {
145         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(16);
146         sb.append(hexEncode((int) (l & 0xFFFFFFFF00000000L) >> 32));
147         sb.append(hexEncode((int) (l & 0x00000000FFFFFFFFL) >> 0));
148         return sb.toString();
149     }
150
151
152
153     /**
154      * <p>Converts a class ID into its hexadecimal notation.</p>
155      */

156     public static String JavaDoc hexEncode(final ClassID classID)
157     {
158         return hexEncode(classID.getBytes());
159     }
160
161
162
163     /**
164      * <p>Decodes the hexadecimal representation of a sequence of
165      * bytes into a byte array. Each character in the string
166      * represents a nibble (half byte) and must be one of the
167      * characters '0'-'9', 'A'-'F' or 'a'-'f'.</p>
168      *
169      * @param s The string to be decoded
170      *
171      * @return The bytes
172      *
173      * @throws IllegalArgumentException if the string does not contain
174      * a valid representation of a byte sequence.
175      */

176     public static byte[] hexDecode(final String JavaDoc s)
177     {
178         final int length = s.length();
179
180         /* The string to be converted must have an even number of
181            characters. */

182         if (length % 2 == 1)
183             throw new IllegalArgumentException JavaDoc
184                 ("String has odd length " + length);
185         byte[] b = new byte[length / 2];
186         char[] c = new char[length];
187         s.toUpperCase().getChars(0, length, c, 0);
188         for (int i = 0; i < length; i += 2)
189             b[i/2] = (byte) (decodeNibble(c[i]) << 4 & 0xF0 |
190                              decodeNibble(c[i+1]) & 0x0F);
191         return b;
192     }
193
194
195
196     /**
197      * <p>Decodes a nibble.</p>
198      *
199      * @param c A character in the range '0'-'9' or 'A'-'F'. Lower
200      * case is not supported here.
201      *
202      * @return The decoded nibble in the range 0-15
203      *
204      * @throws IllegalArgumentException if <em>c</em> is not a
205      * permitted character
206      */

207     protected static byte decodeNibble(final char c)
208     {
209         for (byte i = 0; i < hexval.length; i++)
210             if ((byte) c == hexval[i])
211                 return i;
212         throw new IllegalArgumentException JavaDoc("\"" + c + "\"" +
213                                            " does not represent a nibble.");
214     }
215
216
217
218     /**
219      * <p>For testing.</p>
220      */

221     public static void main(final String JavaDoc args[])
222         throws IOException JavaDoc
223     {
224         final BufferedReader JavaDoc in =
225             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
226         String JavaDoc s;
227         do
228         {
229             s = in.readLine();
230             if (s != null)
231             {
232                 String JavaDoc bytes = hexEncode(s);
233                 System.out.print("Hex encoded (String): ");
234                 System.out.println(bytes);
235                 System.out.print("Hex encoded (byte[]): ");
236                 System.out.println(hexEncode(s.getBytes()));
237                 System.out.print("Re-decoded (byte[]): ");
238                 System.out.println(new String JavaDoc(hexDecode(bytes)));
239             }
240         }
241         while (s != null);
242     }
243
244 }
245
Popular Tags