ohair@286: /*
ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.ws.util;
ohair@286:
ohair@286: import java.io.InputStream;
ohair@286: import java.io.IOException;
ohair@286: import java.io.ByteArrayInputStream;
ohair@286: import java.io.ByteArrayOutputStream;
ohair@286: import java.io.OutputStream;
ohair@286:
ohair@286: /**
ohair@286: * Copied from mail.jar.
ohair@286: */
ohair@286: public class ASCIIUtility {
ohair@286: // Private constructor so that this class is not instantiated
ohair@286: private ASCIIUtility() { }
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Convert the bytes within the specified range of the given byte
ohair@286: * array into a signed integer in the given radix . The range extends
ohair@286: * from start
till, but not including end
.
ohair@286: *
ohair@286: * Based on java.lang.Integer.parseInt()
ohair@286: */
ohair@286: public static int parseInt(byte[] b, int start, int end, int radix)
ohair@286: throws NumberFormatException {
ohair@286: if (b == null)
ohair@286: throw new NumberFormatException("null");
ohair@286:
ohair@286: int result = 0;
ohair@286: boolean negative = false;
ohair@286: int i = start;
ohair@286: int limit;
ohair@286: int multmin;
ohair@286: int digit;
ohair@286:
ohair@286: if (end > start) {
ohair@286: if (b[i] == '-') {
ohair@286: negative = true;
ohair@286: limit = Integer.MIN_VALUE;
ohair@286: i++;
ohair@286: } else {
ohair@286: limit = -Integer.MAX_VALUE;
ohair@286: }
ohair@286: multmin = limit / radix;
ohair@286: if (i < end) {
ohair@286: digit = Character.digit((char)b[i++], radix);
ohair@286: if (digit < 0) {
ohair@286: throw new NumberFormatException(
ohair@286: "illegal number: " + toString(b, start, end)
ohair@286: );
ohair@286: } else {
ohair@286: result = -digit;
ohair@286: }
ohair@286: }
ohair@286: while (i < end) {
ohair@286: // Accumulating negatively avoids surprises near MAX_VALUE
ohair@286: digit = Character.digit((char)b[i++], radix);
ohair@286: if (digit < 0) {
ohair@286: throw new NumberFormatException("illegal number");
ohair@286: }
ohair@286: if (result < multmin) {
ohair@286: throw new NumberFormatException("illegal number");
ohair@286: }
ohair@286: result *= radix;
ohair@286: if (result < limit + digit) {
ohair@286: throw new NumberFormatException("illegal number");
ohair@286: }
ohair@286: result -= digit;
ohair@286: }
ohair@286: } else {
ohair@286: throw new NumberFormatException("illegal number");
ohair@286: }
ohair@286: if (negative) {
ohair@286: if (i > start + 1) {
ohair@286: return result;
ohair@286: } else { /* Only got "-" */
ohair@286: throw new NumberFormatException("illegal number");
ohair@286: }
ohair@286: } else {
ohair@286: return -result;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convert the bytes within the specified range of the given byte
ohair@286: * array into a String. The range extends from start
ohair@286: * till, but not including end
.
ohair@286: */ ohair@286: public static String toString(byte[] b, int start, int end) { ohair@286: int size = end - start; ohair@286: char[] theChars = new char[size]; ohair@286: ohair@286: for (int i = 0, j = start; i < size; ) ohair@286: theChars[i++] = (char)(b[j++]&0xff); ohair@286: ohair@286: return new String(theChars); ohair@286: } ohair@286: ohair@286: public static byte[] getBytes(String s) { ohair@286: char [] chars= s.toCharArray(); ohair@286: int size = chars.length; ohair@286: byte[] bytes = new byte[size]; ohair@286: ohair@286: for (int i = 0; i < size;) ohair@286: bytes[i] = (byte) chars[i++]; ohair@286: return bytes; ohair@286: } ohair@286: ohair@286: public static byte[] getBytes(InputStream is) throws IOException { ohair@286: ByteArrayBuffer bab = new ByteArrayBuffer(); ohair@286: bab.write(is); ohair@286: return bab.toByteArray(); ohair@286: } ohair@286: ohair@286: public static void copyStream(InputStream is, OutputStream out) throws IOException { ohair@286: int size = 1024; ohair@286: byte[] buf = new byte[size]; ohair@286: int len; ohair@286: ohair@286: while ((len = is.read(buf, 0, size)) != -1) ohair@286: out.write(buf, 0, len); ohair@286: } ohair@286: }