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 Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,130 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, 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.OutputStream; 1.34 + 1.35 +/** 1.36 + * Copied from mail.jar. 1.37 + */ 1.38 +public class ASCIIUtility { 1.39 + // Private constructor so that this class is not instantiated 1.40 + private ASCIIUtility() { } 1.41 + 1.42 + 1.43 + /** 1.44 + * Convert the bytes within the specified range of the given byte 1.45 + * array into a signed integer in the given radix . The range extends 1.46 + * from <code>start</code> till, but not including <code>end</code>. <p> 1.47 + * 1.48 + * Based on java.lang.Integer.parseInt() 1.49 + */ 1.50 + public static int parseInt(byte[] b, int start, int end, int radix) 1.51 + throws NumberFormatException { 1.52 + if (b == null) 1.53 + throw new NumberFormatException("null"); 1.54 + 1.55 + int result = 0; 1.56 + boolean negative = false; 1.57 + int i = start; 1.58 + int limit; 1.59 + int multmin; 1.60 + int digit; 1.61 + 1.62 + if (end > start) { 1.63 + if (b[i] == '-') { 1.64 + negative = true; 1.65 + limit = Integer.MIN_VALUE; 1.66 + i++; 1.67 + } else { 1.68 + limit = -Integer.MAX_VALUE; 1.69 + } 1.70 + multmin = limit / radix; 1.71 + if (i < end) { 1.72 + digit = Character.digit((char)b[i++], radix); 1.73 + if (digit < 0) { 1.74 + throw new NumberFormatException( 1.75 + "illegal number: " + toString(b, start, end) 1.76 + ); 1.77 + } else { 1.78 + result = -digit; 1.79 + } 1.80 + } 1.81 + while (i < end) { 1.82 + // Accumulating negatively avoids surprises near MAX_VALUE 1.83 + digit = Character.digit((char)b[i++], radix); 1.84 + if (digit < 0) { 1.85 + throw new NumberFormatException("illegal number"); 1.86 + } 1.87 + if (result < multmin) { 1.88 + throw new NumberFormatException("illegal number"); 1.89 + } 1.90 + result *= radix; 1.91 + if (result < limit + digit) { 1.92 + throw new NumberFormatException("illegal number"); 1.93 + } 1.94 + result -= digit; 1.95 + } 1.96 + } else { 1.97 + throw new NumberFormatException("illegal number"); 1.98 + } 1.99 + if (negative) { 1.100 + if (i > start + 1) { 1.101 + return result; 1.102 + } else { /* Only got "-" */ 1.103 + throw new NumberFormatException("illegal number"); 1.104 + } 1.105 + } else { 1.106 + return -result; 1.107 + } 1.108 + } 1.109 + 1.110 + /** 1.111 + * Convert the bytes within the specified range of the given byte 1.112 + * array into a String. The range extends from <code>start</code> 1.113 + * till, but not including <code>end</code>. <p> 1.114 + */ 1.115 + public static String toString(byte[] b, int start, int end) { 1.116 + int size = end - start; 1.117 + char[] theChars = new char[size]; 1.118 + 1.119 + for (int i = 0, j = start; i < size; ) 1.120 + theChars[i++] = (char)(b[j++]&0xff); 1.121 + 1.122 + return new String(theChars); 1.123 + } 1.124 + 1.125 + public static void copyStream(InputStream is, OutputStream out) throws IOException { 1.126 + int size = 1024; 1.127 + byte[] buf = new byte[size]; 1.128 + int len; 1.129 + 1.130 + while ((len = is.read(buf, 0, size)) != -1) 1.131 + out.write(buf, 0, len); 1.132 + } 1.133 +}