001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.util;
026:
027: import java.io.InputStream;
028: import java.io.IOException;
029: import java.io.ByteArrayInputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.OutputStream;
032:
033: /**
034: * Copied from mail.jar.
035: */
036: public class ASCIIUtility {
037: // Private constructor so that this class is not instantiated
038: private ASCIIUtility() {
039: }
040:
041: /**
042: * Convert the bytes within the specified range of the given byte
043: * array into a signed integer in the given radix . The range extends
044: * from <code>start</code> till, but not including <code>end</code>. <p>
045: *
046: * Based on java.lang.Integer.parseInt()
047: */
048: public static int parseInt(byte[] b, int start, int end, int radix)
049: throws NumberFormatException {
050: if (b == null)
051: throw new NumberFormatException("null");
052:
053: int result = 0;
054: boolean negative = false;
055: int i = start;
056: int limit;
057: int multmin;
058: int digit;
059:
060: if (end > start) {
061: if (b[i] == '-') {
062: negative = true;
063: limit = Integer.MIN_VALUE;
064: i++;
065: } else {
066: limit = -Integer.MAX_VALUE;
067: }
068: multmin = limit / radix;
069: if (i < end) {
070: digit = Character.digit((char) b[i++], radix);
071: if (digit < 0) {
072: throw new NumberFormatException("illegal number: "
073: + toString(b, start, end));
074: } else {
075: result = -digit;
076: }
077: }
078: while (i < end) {
079: // Accumulating negatively avoids surprises near MAX_VALUE
080: digit = Character.digit((char) b[i++], radix);
081: if (digit < 0) {
082: throw new NumberFormatException("illegal number");
083: }
084: if (result < multmin) {
085: throw new NumberFormatException("illegal number");
086: }
087: result *= radix;
088: if (result < limit + digit) {
089: throw new NumberFormatException("illegal number");
090: }
091: result -= digit;
092: }
093: } else {
094: throw new NumberFormatException("illegal number");
095: }
096: if (negative) {
097: if (i > start + 1) {
098: return result;
099: } 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 byte[] getBytes(String s) {
123: char[] chars = s.toCharArray();
124: int size = chars.length;
125: byte[] bytes = new byte[size];
126:
127: for (int i = 0; i < size;)
128: bytes[i] = (byte) chars[i++];
129: return bytes;
130: }
131:
132: public static byte[] getBytes(InputStream is) throws IOException {
133: ByteArrayBuffer bab = new ByteArrayBuffer();
134: bab.write(is);
135: return bab.toByteArray();
136: }
137:
138: public static void copyStream(InputStream is, OutputStream out)
139: throws IOException {
140: int size = 1024;
141: byte[] buf = new byte[size];
142: int len;
143:
144: while ((len = is.read(buf, 0, size)) != -1)
145: out.write(buf, 0, len);
146: }
147: }
|