1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/ASCIIUtility.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,118 @@ 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 +/* FROM mail.jar */ 1.30 +package com.sun.xml.internal.org.jvnet.mimepull; 1.31 + 1.32 +final class ASCIIUtility { 1.33 + 1.34 + // Private constructor so that this class is not instantiated 1.35 + private ASCIIUtility() { } 1.36 + 1.37 + /** 1.38 + * Convert the bytes within the specified range of the given byte 1.39 + * array into a signed integer in the given radix . The range extends 1.40 + * from <code>start</code> till, but not including <code>end</code>. <p> 1.41 + * 1.42 + * Based on java.lang.Integer.parseInt() 1.43 + */ 1.44 + public static int parseInt(byte[] b, int start, int end, int radix) 1.45 + throws NumberFormatException { 1.46 + if (b == null) { 1.47 + throw new NumberFormatException("null"); 1.48 + } 1.49 + 1.50 + int result = 0; 1.51 + boolean negative = false; 1.52 + int i = start; 1.53 + int limit; 1.54 + int multmin; 1.55 + int digit; 1.56 + 1.57 + if (end > start) { 1.58 + if (b[i] == '-') { 1.59 + negative = true; 1.60 + limit = Integer.MIN_VALUE; 1.61 + i++; 1.62 + } else { 1.63 + limit = -Integer.MAX_VALUE; 1.64 + } 1.65 + multmin = limit / radix; 1.66 + if (i < end) { 1.67 + digit = Character.digit((char)b[i++], radix); 1.68 + if (digit < 0) { 1.69 + throw new NumberFormatException( 1.70 + "illegal number: " + toString(b, start, end) 1.71 + ); 1.72 + } else { 1.73 + result = -digit; 1.74 + } 1.75 + } 1.76 + while (i < end) { 1.77 + // Accumulating negatively avoids surprises near MAX_VALUE 1.78 + digit = Character.digit((char)b[i++], radix); 1.79 + if (digit < 0) { 1.80 + throw new NumberFormatException("illegal number"); 1.81 + } 1.82 + if (result < multmin) { 1.83 + throw new NumberFormatException("illegal number"); 1.84 + } 1.85 + result *= radix; 1.86 + if (result < limit + digit) { 1.87 + throw new NumberFormatException("illegal number"); 1.88 + } 1.89 + result -= digit; 1.90 + } 1.91 + } else { 1.92 + throw new NumberFormatException("illegal number"); 1.93 + } 1.94 + if (negative) { 1.95 + if (i > start + 1) { 1.96 + return result; 1.97 + } else { /* Only got "-" */ 1.98 + throw new NumberFormatException("illegal number"); 1.99 + } 1.100 + } else { 1.101 + return -result; 1.102 + } 1.103 + } 1.104 + 1.105 + /** 1.106 + * Convert the bytes within the specified range of the given byte 1.107 + * array into a String. The range extends from <code>start</code> 1.108 + * till, but not including <code>end</code>. <p> 1.109 + */ 1.110 + public static String toString(byte[] b, int start, int end) { 1.111 + int size = end - start; 1.112 + char[] theChars = new char[size]; 1.113 + 1.114 + for (int i = 0, j = start; i < size; ) { 1.115 + theChars[i++] = (char)(b[j++]&0xff); 1.116 + } 1.117 + 1.118 + return new String(theChars); 1.119 + } 1.120 + 1.121 +}