src/share/jaxws_classes/com/sun/xml/internal/ws/util/ASCIIUtility.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 1997, 2012, 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.OutputStream;
31
32 /**
33 * Copied from mail.jar.
34 */
35 public class ASCIIUtility {
36 // Private constructor so that this class is not instantiated
37 private ASCIIUtility() { }
38
39
40 /**
41 * Convert the bytes within the specified range of the given byte
42 * array into a signed integer in the given radix . The range extends
43 * from <code>start</code> till, but not including <code>end</code>. <p>
44 *
45 * Based on java.lang.Integer.parseInt()
46 */
47 public static int parseInt(byte[] b, int start, int end, int radix)
48 throws NumberFormatException {
49 if (b == null)
50 throw new NumberFormatException("null");
51
52 int result = 0;
53 boolean negative = false;
54 int i = start;
55 int limit;
56 int multmin;
57 int digit;
58
59 if (end > start) {
60 if (b[i] == '-') {
61 negative = true;
62 limit = Integer.MIN_VALUE;
63 i++;
64 } else {
65 limit = -Integer.MAX_VALUE;
66 }
67 multmin = limit / radix;
68 if (i < end) {
69 digit = Character.digit((char)b[i++], radix);
70 if (digit < 0) {
71 throw new NumberFormatException(
72 "illegal number: " + toString(b, start, end)
73 );
74 } else {
75 result = -digit;
76 }
77 }
78 while (i < end) {
79 // Accumulating negatively avoids surprises near MAX_VALUE
80 digit = Character.digit((char)b[i++], radix);
81 if (digit < 0) {
82 throw new NumberFormatException("illegal number");
83 }
84 if (result < multmin) {
85 throw new NumberFormatException("illegal number");
86 }
87 result *= radix;
88 if (result < limit + digit) {
89 throw new NumberFormatException("illegal number");
90 }
91 result -= digit;
92 }
93 } else {
94 throw new NumberFormatException("illegal number");
95 }
96 if (negative) {
97 if (i > start + 1) {
98 return result;
99 } else { /* Only got "-" */
100 throw new NumberFormatException("illegal number");
101 }
102 } else {
103 return -result;
104 }
105 }
106
107 /**
108 * Convert the bytes within the specified range of the given byte
109 * array into a String. The range extends from <code>start</code>
110 * till, but not including <code>end</code>. <p>
111 */
112 public static String toString(byte[] b, int start, int end) {
113 int size = end - start;
114 char[] theChars = new char[size];
115
116 for (int i = 0, j = start; i < size; )
117 theChars[i++] = (char)(b[j++]&0xff);
118
119 return new String(theChars);
120 }
121
122 public static void copyStream(InputStream is, OutputStream out) throws IOException {
123 int size = 1024;
124 byte[] buf = new byte[size];
125 int len;
126
127 while ((len = is.read(buf, 0, size)) != -1)
128 out.write(buf, 0, len);
129 }
130 }

mercurial