1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/ASCIIUtility.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,148 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.util; 1.30 + 1.31 +import java.io.InputStream; 1.32 +import java.io.IOException; 1.33 +import java.io.ByteArrayInputStream; 1.34 +import java.io.ByteArrayOutputStream; 1.35 +import java.io.OutputStream; 1.36 + 1.37 +/** 1.38 + * Copied from mail.jar. 1.39 + */ 1.40 +public class ASCIIUtility { 1.41 + // Private constructor so that this class is not instantiated 1.42 + private ASCIIUtility() { } 1.43 + 1.44 + 1.45 + /** 1.46 + * Convert the bytes within the specified range of the given byte 1.47 + * array into a signed integer in the given radix . The range extends 1.48 + * from <code>start</code> till, but not including <code>end</code>. <p> 1.49 + * 1.50 + * Based on java.lang.Integer.parseInt() 1.51 + */ 1.52 + public static int parseInt(byte[] b, int start, int end, int radix) 1.53 + throws NumberFormatException { 1.54 + if (b == null) 1.55 + throw new NumberFormatException("null"); 1.56 + 1.57 + int result = 0; 1.58 + boolean negative = false; 1.59 + int i = start; 1.60 + int limit; 1.61 + int multmin; 1.62 + int digit; 1.63 + 1.64 + if (end > start) { 1.65 + if (b[i] == '-') { 1.66 + negative = true; 1.67 + limit = Integer.MIN_VALUE; 1.68 + i++; 1.69 + } else { 1.70 + limit = -Integer.MAX_VALUE; 1.71 + } 1.72 + multmin = limit / radix; 1.73 + if (i < end) { 1.74 + digit = Character.digit((char)b[i++], radix); 1.75 + if (digit < 0) { 1.76 + throw new NumberFormatException( 1.77 + "illegal number: " + toString(b, start, end) 1.78 + ); 1.79 + } else { 1.80 + result = -digit; 1.81 + } 1.82 + } 1.83 + while (i < end) { 1.84 + // Accumulating negatively avoids surprises near MAX_VALUE 1.85 + digit = Character.digit((char)b[i++], radix); 1.86 + if (digit < 0) { 1.87 + throw new NumberFormatException("illegal number"); 1.88 + } 1.89 + if (result < multmin) { 1.90 + throw new NumberFormatException("illegal number"); 1.91 + } 1.92 + result *= radix; 1.93 + if (result < limit + digit) { 1.94 + throw new NumberFormatException("illegal number"); 1.95 + } 1.96 + result -= digit; 1.97 + } 1.98 + } else { 1.99 + throw new NumberFormatException("illegal number"); 1.100 + } 1.101 + if (negative) { 1.102 + if (i > start + 1) { 1.103 + return result; 1.104 + } else { /* Only got "-" */ 1.105 + throw new NumberFormatException("illegal number"); 1.106 + } 1.107 + } else { 1.108 + return -result; 1.109 + } 1.110 + } 1.111 + 1.112 + /** 1.113 + * Convert the bytes within the specified range of the given byte 1.114 + * array into a String. The range extends from <code>start</code> 1.115 + * till, but not including <code>end</code>. <p> 1.116 + */ 1.117 + public static String toString(byte[] b, int start, int end) { 1.118 + int size = end - start; 1.119 + char[] theChars = new char[size]; 1.120 + 1.121 + for (int i = 0, j = start; i < size; ) 1.122 + theChars[i++] = (char)(b[j++]&0xff); 1.123 + 1.124 + return new String(theChars); 1.125 + } 1.126 + 1.127 + public static byte[] getBytes(String s) { 1.128 + char [] chars= s.toCharArray(); 1.129 + int size = chars.length; 1.130 + byte[] bytes = new byte[size]; 1.131 + 1.132 + for (int i = 0; i < size;) 1.133 + bytes[i] = (byte) chars[i++]; 1.134 + return bytes; 1.135 + } 1.136 + 1.137 + public static byte[] getBytes(InputStream is) throws IOException { 1.138 + ByteArrayBuffer bab = new ByteArrayBuffer(); 1.139 + bab.write(is); 1.140 + return bab.toByteArray(); 1.141 + } 1.142 + 1.143 + public static void copyStream(InputStream is, OutputStream out) throws IOException { 1.144 + int size = 1024; 1.145 + byte[] buf = new byte[size]; 1.146 + int len; 1.147 + 1.148 + while ((len = is.read(buf, 0, size)) != -1) 1.149 + out.write(buf, 0, len); 1.150 + } 1.151 +}