KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > util > ASCIIUtility


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)ASCIIUtility.java 1.10 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27 package com.sun.mail.util;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 public class ASCIIUtility {
35
36     // Private constructor so that this class is not instantiated
37
private ASCIIUtility() { }
38     
39     /**
40      * Convert the bytes within the specified range of the given byte
41      * array into a signed integer in the given radix . The range extends
42      * from <code>start</code> till, but not including <code>end</code>. <p>
43      *
44      * Based on java.lang.Integer.parseInt()
45      */

46     public static int parseInt(byte[] b, int start, int end, int radix)
47         throws NumberFormatException JavaDoc {
48     if (b == null)
49         throw new NumberFormatException JavaDoc("null");
50     
51     int result = 0;
52     boolean negative = false;
53     int i = start;
54     int limit;
55     int multmin;
56     int digit;
57
58     if (end > start) {
59         if (b[i] == '-') {
60         negative = true;
61         limit = Integer.MIN_VALUE;
62         i++;
63         } else {
64         limit = -Integer.MAX_VALUE;
65         }
66         multmin = limit / radix;
67         if (i < end) {
68         digit = Character.digit((char)b[i++], radix);
69         if (digit < 0) {
70             throw new NumberFormatException JavaDoc(
71             "illegal number: " + toString(b, start, end)
72             );
73         } else {
74             result = -digit;
75         }
76         }
77         while (i < end) {
78         // Accumulating negatively avoids surprises near MAX_VALUE
79
digit = Character.digit((char)b[i++], radix);
80         if (digit < 0) {
81             throw new NumberFormatException JavaDoc("illegal number");
82         }
83         if (result < multmin) {
84             throw new NumberFormatException JavaDoc("illegal number");
85         }
86         result *= radix;
87         if (result < limit + digit) {
88             throw new NumberFormatException JavaDoc("illegal number");
89         }
90         result -= digit;
91         }
92     } else {
93         throw new NumberFormatException JavaDoc("illegal number");
94     }
95     if (negative) {
96         if (i > start + 1) {
97         return result;
98         } else { /* Only got "-" */
99         throw new NumberFormatException JavaDoc("illegal number");
100         }
101     } else {
102         return -result;
103     }
104     }
105
106     /**
107      * Convert the bytes within the specified range of the given byte
108      * array into a signed integer . The range extends from
109      * <code>start</code> till, but not including <code>end</code>. <p>
110      */

111     public static int parseInt(byte[] b, int start, int end)
112         throws NumberFormatException JavaDoc {
113     return parseInt(b, start, end, 10);
114     }
115
116     /**
117      * Convert the bytes within the specified range of the given byte
118      * array into a signed long in the given radix . The range extends
119      * from <code>start</code> till, but not including <code>end</code>. <p>
120      *
121      * Based on java.lang.Long.parseLong()
122      */

123     public static long parseLong(byte[] b, int start, int end, int radix)
124         throws NumberFormatException JavaDoc {
125     if (b == null)
126         throw new NumberFormatException JavaDoc("null");
127     
128     long result = 0;
129     boolean negative = false;
130     int i = start;
131     long limit;
132     long multmin;
133     int digit;
134
135     if (end > start) {
136         if (b[i] == '-') {
137         negative = true;
138         limit = Long.MIN_VALUE;
139         i++;
140         } else {
141         limit = -Long.MAX_VALUE;
142         }
143         multmin = limit / radix;
144         if (i < end) {
145         digit = Character.digit((char)b[i++], radix);
146         if (digit < 0) {
147             throw new NumberFormatException JavaDoc(
148             "illegal number: " + toString(b, start, end)
149             );
150         } else {
151             result = -digit;
152         }
153         }
154         while (i < end) {
155         // Accumulating negatively avoids surprises near MAX_VALUE
156
digit = Character.digit((char)b[i++], radix);
157         if (digit < 0) {
158             throw new NumberFormatException JavaDoc("illegal number");
159         }
160         if (result < multmin) {
161             throw new NumberFormatException JavaDoc("illegal number");
162         }
163         result *= radix;
164         if (result < limit + digit) {
165             throw new NumberFormatException JavaDoc("illegal number");
166         }
167         result -= digit;
168         }
169     } else {
170         throw new NumberFormatException JavaDoc("illegal number");
171     }
172     if (negative) {
173         if (i > start + 1) {
174         return result;
175         } else { /* Only got "-" */
176         throw new NumberFormatException JavaDoc("illegal number");
177         }
178     } else {
179         return -result;
180     }
181     }
182
183     /**
184      * Convert the bytes within the specified range of the given byte
185      * array into a signed long . The range extends from
186      * <code>start</code> till, but not including <code>end</code>. <p>
187      */

188     public static long parseLong(byte[] b, int start, int end)
189         throws NumberFormatException JavaDoc {
190     return parseLong(b, start, end, 10);
191     }
192
193     /**
194      * Convert the bytes within the specified range of the given byte
195      * array into a String. The range extends from <code>start</code>
196      * till, but not including <code>end</code>. <p>
197      */

198     public static String JavaDoc toString(byte[] b, int start, int end) {
199     int size = end - start;
200     char[] theChars = new char[size];
201
202     for (int i = 0, j = start; i < size; )
203         theChars[i++] = (char)(b[j++]&0xff);
204     
205     return new String JavaDoc(theChars);
206     }
207
208     public static String JavaDoc toString(ByteArrayInputStream JavaDoc is) {
209     int size = is.available();
210     char[] theChars = new char[size];
211     byte[] bytes = new byte[size];
212
213     is.read(bytes, 0, size);
214     for (int i = 0; i < size;)
215         theChars[i] = (char)(bytes[i++]&0xff);
216     
217     return new String JavaDoc(theChars);
218     }
219
220
221     public static byte[] getBytes(String JavaDoc s) {
222     char [] chars= s.toCharArray();
223     int size = chars.length;
224     byte[] bytes = new byte[size];
225         
226     for (int i = 0; i < size;)
227         bytes[i] = (byte) chars[i++];
228     return bytes;
229     }
230
231     public static byte[] getBytes(InputStream JavaDoc is) throws IOException JavaDoc {
232
233     int len;
234     int size = 1024;
235     byte [] buf;
236
237
238     if (is instanceof ByteArrayInputStream JavaDoc) {
239         size = is.available();
240         buf = new byte[size];
241         len = is.read(buf, 0, size);
242     }
243     else {
244         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
245         buf = new byte[size];
246         while ((len = is.read(buf, 0, size)) != -1)
247         bos.write(buf, 0, len);
248         buf = bos.toByteArray();
249     }
250     return buf;
251     }
252 }
253
Popular Tags