KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > Convert


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: Convert.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25
26
27
28 package com.lutris.util;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.DataOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.text.CharacterIterator JavaDoc;
33 import java.text.StringCharacterIterator JavaDoc;
34
35 /**
36  * Various conversion methods.
37  * These methods are mostly used to convert internal java data
38  * fields into byte arrays or strings for use over the network
39  * or in 8 bit ASCII fields.
40  *
41  * @deprecated Use HexEncoder or Base64Encoder.
42  * @see HexEncoder
43  * @see Base64Encoder
44  *
45  */

46 public class Convert {
47     /**
48      * Hexadecimal characters corresponding to each half byte value.
49      */

50     private static final char[] HexChars = {
51     '0', '1', '2', '3', '4', '5', '6', '7',
52     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
53     };
54
55     /**
56      * The BASE64 encoding standard's 6-bit alphabet, from RFC 1521,
57      * plus the padding character at the end.
58      */

59     private static final char[] Base64Chars = {
60     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
61     'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
62     'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
63     'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
64     'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
65     'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
66     'w', 'x', 'y', 'z', '0', '1', '2', '3',
67     '4', '5', '6', '7', '8', '9', '+', '/',
68         '='
69     };
70
71     /**
72      * Converts a long integer to an unsigned hexadecimal String. Treats
73      * the integer as an unsigned 64 bit value and left-pads with the
74      * pad character of the caller's choice.
75      *
76      * @param value The long integer to convert to a hexadecimal string.
77      * @param len The total padded length of the string. If the number
78      * is larger than the padded length, then this length
79      * of the string will be the length of the number.
80      * @param pad The character to use for padding.
81      * @return Unsigned hexadecimal numeric string representing
82      * the specified value.
83      */

84     public static final String JavaDoc toHexString(long value, int len, char pad) {
85     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(Long.toHexString(value));
86     int npad = len - sb.length();
87     while (npad-- > 0) sb.insert(0, pad);
88     return new String JavaDoc(sb);
89     }
90
91     /**
92      * Converts an arbitrary array of bytes to ASCII hexadecimal string
93      * form, with two hex characters corresponding to each byte. The
94      * length of the resultant string in characters will be twice the
95      * length of the specified array of bytes.
96      *
97      * @param bytes The array of bytes to convert to ASCII hex form.
98      * @return An ASCII hexadecimal numeric string representing the
99      * specified array of bytes.
100      */

101     public static final String JavaDoc toHexString(byte[] bytes) {
102     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
103     int i;
104     for (i=0; i < bytes.length; i++) {
105         sb.append(HexChars[(bytes[i] >> 4) & 0xf]);
106         sb.append(HexChars[bytes[i] & 0xf]);
107     }
108     return new String JavaDoc(sb);
109     }
110
111     /**
112      * Performs RFC1521 style Base64 encoding of arbitrary binary data.
113      * The output is a java String containing the Base64 characters representing
114      * the binary data. Be aware that this string is in Unicode form, and
115      * should be converted to UTF8 with the usual java conversion routines
116      * before it is sent over a network. The output string is guaranteed
117      * to only contain characters that are a single byte in UTF8 format.
118      * Also be aware that this routine leaves it to the caller to break
119      * the string into 70 byte lines as per RFC1521.
120      *
121      * @param bytes The array of bytes to convert to Base64 encoding.
122      * @return An string containing the specified bytes in Base64
123      * encoded form.
124      */

125     public static final String JavaDoc toBase64String(byte[] bytes) {
126         return toBase64String(bytes, Base64Chars);
127     }
128
129     
130     /**
131      * Performs encoding of arbitrary binary data based on a 6 bit
132      * alphabet. The output is a java String containing the encoded
133      * characters representing the binary data. Be aware that this
134      * string is in Unicode form, and should be converted to UTF8 with
135      * the usual java conversion routines before it is sent over a
136      * network. The alphabet passed in via <code>chars</code> is used
137      * without further checks, it's the callers responsibility to set
138      * it to something meaningful.
139      *
140      * @param bytes The array of bytes to convert to Base64 encoding.
141      * @param chars The alphabet used in encoding. Must contain
142      * exactly 65 characters: A 6 bit alphabet plus one
143      * padding char at position 65.
144      * @return An string containing the specified bytes in Base64
145      * encoded form.
146      */

147     public static final String JavaDoc toBase64String(byte[] bytes, char[] chars) {
148     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
149     int len = bytes.length, i=0, ival;
150     while (len >= 3) {
151         ival = ((int)bytes[i++] + 256) & 0xff;
152         ival <<= 8;
153         ival += ((int)bytes[i++] + 256) & 0xff;
154         ival <<= 8;
155         ival += ((int)bytes[i++] + 256) & 0xff;
156         len -= 3;
157         sb.append(chars[(ival >> 18) & 63]);
158         sb.append(chars[(ival >> 12) & 63]);
159         sb.append(chars[(ival >> 6) & 63]);
160         sb.append(chars[ival & 63]);
161     }
162     switch (len) {
163         case 0: // No pads needed.
164
break;
165         case 1: // Two more output bytes and two pads.
166
ival = ((int)bytes[i++] + 256) & 0xff;
167         ival <<= 16;
168         sb.append(chars[(ival >> 18) & 63]);
169         sb.append(chars[(ival >> 12) & 63]);
170         sb.append(chars[64]);
171         sb.append(chars[64]);
172         break;
173         case 2: // Three more output bytes and one pad.
174
ival = ((int)bytes[i++] + 256) & 0xff;
175         ival <<= 8;
176         ival += ((int)bytes[i] + 256) & 0xff;
177         ival <<= 8;
178         sb.append(chars[(ival >> 18) & 63]);
179         sb.append(chars[(ival >> 12) & 63]);
180         sb.append(chars[(ival >> 6) & 63]);
181         sb.append(chars[64]);
182         break;
183     }
184     return new String JavaDoc(sb);
185     }
186     
187     /**
188      * Performs RFC1521 style Base64 decoding of Base64 encoded data.
189      * The output is a byte array containing the decoded binary data.
190      * The input is expected to be a normal Unicode String object.
191      *
192      * @param s The Base64 encoded string to decode into binary data.
193      * @return An array of bytes containing the decoded data.
194      */

195     public static final byte[] fromBase64String(String JavaDoc s) {
196     try {
197         StringCharacterIterator JavaDoc iter = new StringCharacterIterator JavaDoc(s);
198         ByteArrayOutputStream JavaDoc bytestr = new ByteArrayOutputStream JavaDoc();
199         DataOutputStream JavaDoc outstr = new DataOutputStream JavaDoc(bytestr);
200         char c;
201         int d, i, group;
202         int[] bgroup = new int[4];
203         decode: for (i=0, group=0, c = iter.first();
204              c != CharacterIterator.DONE;
205              c = iter.next())
206         {
207         switch (c) {
208             case 'A': d = 0; break; case 'B': d = 1; break;
209             case 'C': d = 2; break; case 'D': d = 3; break;
210             case 'E': d = 4; break; case 'F': d = 5; break;
211             case 'G': d = 6; break; case 'H': d = 7; break;
212             case 'I': d = 8; break; case 'J': d = 9; break;
213             case 'K': d = 10; break; case 'L': d = 11; break;
214             case 'M': d = 12; break; case 'N': d = 13; break;
215             case 'O': d = 14; break; case 'P': d = 15; break;
216             case 'Q': d = 16; break; case 'R': d = 17; break;
217             case 'S': d = 18; break; case 'T': d = 19; break;
218             case 'U': d = 20; break; case 'V': d = 21; break;
219             case 'W': d = 22; break; case 'X': d = 23; break;
220             case 'Y': d = 24; break; case 'Z': d = 25; break;
221             case 'a': d = 26; break; case 'b': d = 27; break;
222             case 'c': d = 28; break; case 'd': d = 29; break;
223             case 'e': d = 30; break; case 'f': d = 31; break;
224             case 'g': d = 32; break; case 'h': d = 33; break;
225             case 'i': d = 34; break; case 'j': d = 35; break;
226             case 'k': d = 36; break; case 'l': d = 37; break;
227             case 'm': d = 38; break; case 'n': d = 39; break;
228             case 'o': d = 40; break; case 'p': d = 41; break;
229             case 'q': d = 42; break; case 'r': d = 43; break;
230             case 's': d = 44; break; case 't': d = 45; break;
231             case 'u': d = 46; break; case 'v': d = 47; break;
232             case 'w': d = 48; break; case 'x': d = 49; break;
233             case 'y': d = 50; break; case 'z': d = 51; break;
234             case '0': d = 52; break; case '1': d = 53; break;
235             case '2': d = 54; break; case '3': d = 55; break;
236             case '4': d = 56; break; case '5': d = 57; break;
237             case '6': d = 58; break; case '7': d = 59; break;
238             case '8': d = 60; break; case '9': d = 61; break;
239             case '+': d = 62; break; case '/': d = 63; break;
240             default:
241             // Any character not in Base64 alphabet is treated
242
// as end of data. This includes the '=' (pad) char.
243
break decode; // Skip illegal characters.
244
}
245         bgroup[i++] = d;
246         if (i >= 4) {
247             i = 0;
248             group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+
249                 ((bgroup[2] & 63) << 6) + (bgroup[3] & 63);
250             outstr.writeByte(((group >> 16) & 255));
251             outstr.writeByte(((group >> 8) & 255));
252             outstr.writeByte(group & 255);
253         }
254         }
255         // Handle the case of remaining characters and
256
// pad handling. If input is not a multiple of 4
257
// in length, then '=' pads are assumed.
258
switch (i) {
259         case 2:
260             // One output byte from two input bytes.
261
group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12);
262             outstr.writeByte(((group >> 16) & 255));
263             break;
264         case 3:
265             // Two output bytes from three input bytes.
266
group = ((bgroup[0] & 63) << 18)+((bgroup[1] & 63) << 12)+
267                 ((bgroup[2] & 63) << 6);
268             outstr.writeByte(((group >> 16) & 255));
269             outstr.writeByte(((group >> 8) & 255));
270             break;
271         default:
272             // Any other case, including correct 0, is treated as
273
// end of data.
274
break;
275         }
276         outstr.flush();
277         return bytestr.toByteArray();
278     }
279     catch (IOException JavaDoc e) {} // Won't happen. Return null if it does.
280
return null;
281     }
282 }
283
284
Popular Tags