1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/ASCIIUtility.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,152 @@ 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 +/* 1.30 + * @(#)ASCIIUtility.java 1.9 02/03/27 1.31 + */ 1.32 + 1.33 + 1.34 +package com.sun.xml.internal.messaging.saaj.packaging.mime.util; 1.35 + 1.36 +import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; 1.37 + 1.38 +import java.io.*; 1.39 + 1.40 +public class ASCIIUtility { 1.41 + 1.42 + // Private constructor so that this class is not instantiated 1.43 + private ASCIIUtility() { } 1.44 + 1.45 + 1.46 + /** 1.47 + * Convert the bytes within the specified range of the given byte 1.48 + * array into a signed integer in the given radix . The range extends 1.49 + * from <code>start</code> till, but not including <code>end</code>. <p> 1.50 + * 1.51 + * Based on java.lang.Integer.parseInt() 1.52 + */ 1.53 + public static int parseInt(byte[] b, int start, int end, int radix) 1.54 + throws NumberFormatException { 1.55 + if (b == null) 1.56 + throw new NumberFormatException("null"); 1.57 + 1.58 + int result = 0; 1.59 + boolean negative = false; 1.60 + int i = start; 1.61 + int limit; 1.62 + int multmin; 1.63 + int digit; 1.64 + 1.65 + if (end > start) { 1.66 + if (b[i] == '-') { 1.67 + negative = true; 1.68 + limit = Integer.MIN_VALUE; 1.69 + i++; 1.70 + } else { 1.71 + limit = -Integer.MAX_VALUE; 1.72 + } 1.73 + multmin = limit / radix; 1.74 + if (i < end) { 1.75 + digit = Character.digit((char)b[i++], radix); 1.76 + if (digit < 0) { 1.77 + throw new NumberFormatException( 1.78 + "illegal number: " + toString(b, start, end) 1.79 + ); 1.80 + } else { 1.81 + result = -digit; 1.82 + } 1.83 + } 1.84 + while (i < end) { 1.85 + // Accumulating negatively avoids surprises near MAX_VALUE 1.86 + digit = Character.digit((char)b[i++], radix); 1.87 + if (digit < 0) { 1.88 + throw new NumberFormatException("illegal number"); 1.89 + } 1.90 + if (result < multmin) { 1.91 + throw new NumberFormatException("illegal number"); 1.92 + } 1.93 + result *= radix; 1.94 + if (result < limit + digit) { 1.95 + throw new NumberFormatException("illegal number"); 1.96 + } 1.97 + result -= digit; 1.98 + } 1.99 + } else { 1.100 + throw new NumberFormatException("illegal number"); 1.101 + } 1.102 + if (negative) { 1.103 + if (i > start + 1) { 1.104 + return result; 1.105 + } else { /* Only got "-" */ 1.106 + throw new NumberFormatException("illegal number"); 1.107 + } 1.108 + } else { 1.109 + return -result; 1.110 + } 1.111 + } 1.112 + 1.113 + /** 1.114 + * Convert the bytes within the specified range of the given byte 1.115 + * array into a String. The range extends from <code>start</code> 1.116 + * till, but not including <code>end</code>. <p> 1.117 + */ 1.118 + public static String toString(byte[] b, int start, int end) { 1.119 + int size = end - start; 1.120 + char[] theChars = new char[size]; 1.121 + 1.122 + for (int i = 0, j = start; i < size; ) 1.123 + theChars[i++] = (char)(b[j++]&0xff); 1.124 + 1.125 + return new String(theChars); 1.126 + } 1.127 + 1.128 + public static byte[] getBytes(String s) { 1.129 + char [] chars= s.toCharArray(); 1.130 + int size = chars.length; 1.131 + byte[] bytes = new byte[size]; 1.132 + 1.133 + for (int i = 0; i < size;) 1.134 + bytes[i] = (byte) chars[i++]; 1.135 + return bytes; 1.136 + } 1.137 + 1.138 + /** 1.139 + * 1.140 + * @deprecated 1.141 + * this is an expensive operation that require an additional 1.142 + * buffer reallocation just to get the array of an exact size. 1.143 + * Unless you absolutely need the exact size array, don't use this. 1.144 + * Use {@link ByteOutputStream} and {@link ByteOutputStream#write(InputStream)}. 1.145 + */ 1.146 + public static byte[] getBytes(InputStream is) throws IOException { 1.147 + ByteOutputStream bos = new ByteOutputStream(); 1.148 + try { 1.149 + bos.write(is); 1.150 + } finally { 1.151 + is.close(); 1.152 + } 1.153 + return bos.toByteArray(); 1.154 + } 1.155 +}