001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.util;
037:
038: import java.io.InputStream;
039: import java.io.IOException;
040: import java.io.ByteArrayInputStream;
041: import java.io.ByteArrayOutputStream;
042: import java.io.OutputStream;
043:
044: /**
045: * Copied from mail.jar.
046: */
047: public class ASCIIUtility {
048: // Private constructor so that this class is not instantiated
049: private ASCIIUtility() {
050: }
051:
052: /**
053: * Convert the bytes within the specified range of the given byte
054: * array into a signed integer in the given radix . The range extends
055: * from <code>start</code> till, but not including <code>end</code>. <p>
056: *
057: * Based on java.lang.Integer.parseInt()
058: */
059: public static int parseInt(byte[] b, int start, int end, int radix)
060: throws NumberFormatException {
061: if (b == null)
062: throw new NumberFormatException("null");
063:
064: int result = 0;
065: boolean negative = false;
066: int i = start;
067: int limit;
068: int multmin;
069: int digit;
070:
071: if (end > start) {
072: if (b[i] == '-') {
073: negative = true;
074: limit = Integer.MIN_VALUE;
075: i++;
076: } else {
077: limit = -Integer.MAX_VALUE;
078: }
079: multmin = limit / radix;
080: if (i < end) {
081: digit = Character.digit((char) b[i++], radix);
082: if (digit < 0) {
083: throw new NumberFormatException("illegal number: "
084: + toString(b, start, end));
085: } else {
086: result = -digit;
087: }
088: }
089: while (i < end) {
090: // Accumulating negatively avoids surprises near MAX_VALUE
091: digit = Character.digit((char) b[i++], radix);
092: if (digit < 0) {
093: throw new NumberFormatException("illegal number");
094: }
095: if (result < multmin) {
096: throw new NumberFormatException("illegal number");
097: }
098: result *= radix;
099: if (result < limit + digit) {
100: throw new NumberFormatException("illegal number");
101: }
102: result -= digit;
103: }
104: } else {
105: throw new NumberFormatException("illegal number");
106: }
107: if (negative) {
108: if (i > start + 1) {
109: return result;
110: } else { /* Only got "-" */
111: throw new NumberFormatException("illegal number");
112: }
113: } else {
114: return -result;
115: }
116: }
117:
118: /**
119: * Convert the bytes within the specified range of the given byte
120: * array into a String. The range extends from <code>start</code>
121: * till, but not including <code>end</code>. <p>
122: */
123: public static String toString(byte[] b, int start, int end) {
124: int size = end - start;
125: char[] theChars = new char[size];
126:
127: for (int i = 0, j = start; i < size;)
128: theChars[i++] = (char) (b[j++] & 0xff);
129:
130: return new String(theChars);
131: }
132:
133: public static byte[] getBytes(String s) {
134: char[] chars = s.toCharArray();
135: int size = chars.length;
136: byte[] bytes = new byte[size];
137:
138: for (int i = 0; i < size;)
139: bytes[i] = (byte) chars[i++];
140: return bytes;
141: }
142:
143: public static byte[] getBytes(InputStream is) throws IOException {
144: ByteArrayBuffer bab = new ByteArrayBuffer();
145: bab.write(is);
146: return bab.toByteArray();
147: }
148:
149: public static void copyStream(InputStream is, OutputStream out)
150: throws IOException {
151: int size = 1024;
152: byte[] buf = new byte[size];
153: int len;
154:
155: while ((len = is.read(buf, 0, size)) != -1)
156: out.write(buf, 0, len);
157: }
158: }
|