|
1 /* |
|
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.util; |
|
27 |
|
28 import java.io.InputStream; |
|
29 import java.io.IOException; |
|
30 import java.io.ByteArrayInputStream; |
|
31 import java.io.ByteArrayOutputStream; |
|
32 import java.io.OutputStream; |
|
33 |
|
34 /** |
|
35 * Copied from mail.jar. |
|
36 */ |
|
37 public class ASCIIUtility { |
|
38 // Private constructor so that this class is not instantiated |
|
39 private ASCIIUtility() { } |
|
40 |
|
41 |
|
42 /** |
|
43 * Convert the bytes within the specified range of the given byte |
|
44 * array into a signed integer in the given radix . The range extends |
|
45 * from <code>start</code> till, but not including <code>end</code>. <p> |
|
46 * |
|
47 * Based on java.lang.Integer.parseInt() |
|
48 */ |
|
49 public static int parseInt(byte[] b, int start, int end, int radix) |
|
50 throws NumberFormatException { |
|
51 if (b == null) |
|
52 throw new NumberFormatException("null"); |
|
53 |
|
54 int result = 0; |
|
55 boolean negative = false; |
|
56 int i = start; |
|
57 int limit; |
|
58 int multmin; |
|
59 int digit; |
|
60 |
|
61 if (end > start) { |
|
62 if (b[i] == '-') { |
|
63 negative = true; |
|
64 limit = Integer.MIN_VALUE; |
|
65 i++; |
|
66 } else { |
|
67 limit = -Integer.MAX_VALUE; |
|
68 } |
|
69 multmin = limit / radix; |
|
70 if (i < end) { |
|
71 digit = Character.digit((char)b[i++], radix); |
|
72 if (digit < 0) { |
|
73 throw new NumberFormatException( |
|
74 "illegal number: " + toString(b, start, end) |
|
75 ); |
|
76 } else { |
|
77 result = -digit; |
|
78 } |
|
79 } |
|
80 while (i < end) { |
|
81 // Accumulating negatively avoids surprises near MAX_VALUE |
|
82 digit = Character.digit((char)b[i++], radix); |
|
83 if (digit < 0) { |
|
84 throw new NumberFormatException("illegal number"); |
|
85 } |
|
86 if (result < multmin) { |
|
87 throw new NumberFormatException("illegal number"); |
|
88 } |
|
89 result *= radix; |
|
90 if (result < limit + digit) { |
|
91 throw new NumberFormatException("illegal number"); |
|
92 } |
|
93 result -= digit; |
|
94 } |
|
95 } else { |
|
96 throw new NumberFormatException("illegal number"); |
|
97 } |
|
98 if (negative) { |
|
99 if (i > start + 1) { |
|
100 return result; |
|
101 } else { /* Only got "-" */ |
|
102 throw new NumberFormatException("illegal number"); |
|
103 } |
|
104 } else { |
|
105 return -result; |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * Convert the bytes within the specified range of the given byte |
|
111 * array into a String. The range extends from <code>start</code> |
|
112 * till, but not including <code>end</code>. <p> |
|
113 */ |
|
114 public static String toString(byte[] b, int start, int end) { |
|
115 int size = end - start; |
|
116 char[] theChars = new char[size]; |
|
117 |
|
118 for (int i = 0, j = start; i < size; ) |
|
119 theChars[i++] = (char)(b[j++]&0xff); |
|
120 |
|
121 return new String(theChars); |
|
122 } |
|
123 |
|
124 public static byte[] getBytes(String s) { |
|
125 char [] chars= s.toCharArray(); |
|
126 int size = chars.length; |
|
127 byte[] bytes = new byte[size]; |
|
128 |
|
129 for (int i = 0; i < size;) |
|
130 bytes[i] = (byte) chars[i++]; |
|
131 return bytes; |
|
132 } |
|
133 |
|
134 public static byte[] getBytes(InputStream is) throws IOException { |
|
135 ByteArrayBuffer bab = new ByteArrayBuffer(); |
|
136 bab.write(is); |
|
137 return bab.toByteArray(); |
|
138 } |
|
139 |
|
140 public static void copyStream(InputStream is, OutputStream out) throws IOException { |
|
141 int size = 1024; |
|
142 byte[] buf = new byte[size]; |
|
143 int len; |
|
144 |
|
145 while ((len = is.read(buf, 0, size)) != -1) |
|
146 out.write(buf, 0, len); |
|
147 } |
|
148 } |