KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > ldap > ldif > Utils


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

16 package scriptella.driver.ldap.ldif;
17
18 import scriptella.util.IOUtils;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23
24 /**
25  * Utility methods from the following apache classes:
26  * <ul>
27  * <li>org.apache.directory.shared.ldap.util.StringTools</li>
28  * <li>org.apache.directory.shared.ldap.util.Base64</li>
29  * </ul>
30  */

31 class Utils {
32     /** Hex chars */
33     private static final byte[] HEX_CHAR = new byte[]
34         { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
35
36
37     private Utils() {
38     }
39
40     /**
41      * Test if the current character is equal to a specific character.
42      *
43      * @param chars
44      * The buffer which contains the data
45      * @param index
46      * Current position in the buffer
47      * @param car
48      * The character we want to compare with the current buffer
49      * position
50      * @return <code>true</code> if the current character equals the given
51      * character.
52      */

53     public static boolean isCharASCII( char[] chars, int index, char car )
54     {
55         if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) || ( index >= chars.length ) )
56         {
57             return false;
58         }
59         else
60         {
61             return chars[index] == car;
62         }
63     }
64
65     /**
66      * Return an UTF-8 encoded String
67      *
68      * @param bytes
69      * The byte array to be transformed to a String
70      * @return A String.
71      */

72     public static String JavaDoc utf8ToString( byte[] bytes )
73     {
74         if ( bytes == null )
75         {
76             return "";
77         }
78
79         try
80         {
81             return new String JavaDoc( bytes, "UTF-8" );
82         }
83         catch ( UnsupportedEncodingException JavaDoc uee )
84         {
85             return "";
86         }
87     }
88
89     /** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
90         public static final boolean[] DIGIT =
91             {
92                 false, false, false, false, false, false, false, false,
93                 false, false, false, false, false, false, false, false,
94                 false, false, false, false, false, false, false, false,
95                 false, false, false, false, false, false, false, false,
96                 false, false, false, false, false, false, false, false,
97                 false, false, false, false, false, false, false, false,
98                 true, true, true, true, true, true, true, true,
99                 true, true, false, false, false, false, false, false,
100                 false, false, false, false, false, false, false, false,
101                 false, false, false, false, false, false, false, false,
102                 false, false, false, false, false, false, false, false,
103                 false, false, false, false, false, false, false, false,
104                 false, false, false, false, false, false, false, false,
105                 false, false, false, false, false, false, false, false,
106                 false, false, false, false, false, false, false, false,
107                 false, false, false, false, false, false, false, false
108             };
109
110
111     /**
112      * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
113      * '3' | '4' | '5' | '6' | '7' | '8' | '9'
114      *
115      * @param chars
116      * The buffer which contains the data
117      * @param index
118      * Current position in the buffer
119      * @return <code>true</code> if the current character is a Digit
120      */

121     public static boolean isDigit( char[] chars, int index )
122     {
123         if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) || ( index >= chars.length ) )
124         {
125             return false;
126         }
127         else
128         {
129             return !((chars[index] > 127) || !DIGIT[chars[index]]);
130         }
131     }
132
133
134     //BASE64 Section from org.apache.directory.shared.ldap.util.Base64 class
135

136     /**
137      * Decodes a BASE-64 encoded stream to recover the original data. White
138      * space before and after will be trimmed away, but no other manipulation of
139      * the input will be performed. As of version 1.2 this method will properly
140      * handle input containing junk characters (newlines and the like) rather
141      * than throwing an error. It does this by pre-parsing the input and
142      * generating from that a count of VALID input characters.
143      *
144      * @param a_data
145      * data to decode.
146      * @return the decoded binary data.
147      */

148     public static byte[] base64Decode( char[] a_data )
149     {
150         // as our input could contain non-BASE64 data (newlines,
151
// whitespace of any sort, whatever) we must first adjust
152
// our count of USABLE data so that...
153
// (a) we don't misallocate the output array, and
154
// (b) think that we miscalculated our data length
155
// just because of extraneous throw-away junk
156

157         int l_tempLen = a_data.length;
158         for (char anA_data1 : a_data) {
159             if ((anA_data1 > 255) || s_codes[anA_data1] < 0) {
160                 --l_tempLen; // ignore non-valid chars and padding
161
}
162         }
163         // calculate required length:
164
// -- 3 bytes for every 4 valid base64 chars
165
// -- plus 2 bytes if there are 3 extra base64 chars,
166
// or plus 1 byte if there are 2 extra.
167

168         int l_len = ( l_tempLen / 4 ) * 3;
169
170         if ( ( l_tempLen % 4 ) == 3 )
171         {
172             l_len += 2;
173         }
174
175         if ( ( l_tempLen % 4 ) == 2 )
176         {
177             l_len += 1;
178         }
179
180         byte[] l_out = new byte[l_len];
181
182         int l_shift = 0; // # of excess bits stored in accum
183
int l_accum = 0; // excess bits
184
int l_index = 0;
185
186         // we now go through the entire array (NOT using the 'tempLen' value)
187
for (char anA_data : a_data) {
188             int l_value = (anA_data > 255) ? -1 : s_codes[anA_data];
189
190             if (l_value >= 0) // skip over non-code
191
{
192                 l_accum <<= 6; // bits shift up by 6 each time thru
193
l_shift += 6; // loop, with new bits being put in
194
l_accum |= l_value; // at the bottom. whenever there
195
if (l_shift >= 8) // are 8 or more shifted in, write them
196
{
197                     l_shift -= 8; // out (from the top, leaving any excess
198
l_out[l_index++] = // at the bottom for next iteration.
199
(byte) ((l_accum >> l_shift) & 0xff);
200                 }
201             }
202             // we will also have skipped processing a padding null byte ('=')
203
// here;
204
// these are used ONLY for padding to an even length and do not
205
// legally
206
// occur as encoded data. for this reason we can ignore the fact
207
// that
208
// no index++ operation occurs in that special case: the out[] array
209
// is
210
// initialized to all-zero bytes to start with and that works to our
211
// advantage in this combination.
212
}
213
214         // if there is STILL something wrong we just have to throw up now!
215
if ( l_index != l_out.length )
216         {
217             throw new Error JavaDoc( "Miscalculated data length (wrote " + l_index + " instead of " + l_out.length + ")" );
218         }
219
220         return l_out;
221     }
222
223     /** lookup table for converting base64 characters to value in range 0..63 */
224     private static byte[] s_codes = new byte[256];
225
226     static
227     {
228         for ( int ii = 0; ii < 256; ii++ )
229         {
230             s_codes[ii] = -1;
231         }
232
233         for ( int ii = 'A'; ii <= 'Z'; ii++ )
234         {
235             s_codes[ii] = ( byte ) ( ii - 'A' );
236         }
237
238         for ( int ii = 'a'; ii <= 'z'; ii++ )
239         {
240             s_codes[ii] = ( byte ) ( 26 + ii - 'a' );
241         }
242
243         for ( int ii = '0'; ii <= '9'; ii++ )
244         {
245             s_codes[ii] = ( byte ) ( 52 + ii - '0' );
246         }
247
248         s_codes['+'] = 62;
249         s_codes['/'] = 63;
250     }
251
252     /**
253      * Helper function that dump an array of bytes in hex form
254      *
255      * @param buffer
256      * The bytes array to dump
257      * @return A string representation of the array of bytes
258      */

259     public static String JavaDoc dumpBytes( byte[] buffer )
260     {
261         if ( buffer == null )
262         {
263             return "";
264         }
265
266         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(2+buffer.length*2);
267
268         for (byte b : buffer) {
269             sb.append("0x").append((char) (HEX_CHAR[(b & 0x00F0) >> 4])).append(
270                     (char) (HEX_CHAR[b & 0x000F])).append(" ");
271         }
272
273         return sb.toString();
274     }
275
276     public static byte[] toByteArray(InputStream JavaDoc is, long maxLength) throws IOException JavaDoc {
277         return IOUtils.toByteArray(is, maxLength);
278     }
279
280
281
282
283
284
285
286
287 }
288
Popular Tags