1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @(#)ASCIIUtility.java 1.9 02/03/27 28 */ 29 30 31 package com.sun.xml.internal.messaging.saaj.packaging.mime.util; 32 33 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; 34 35 import java.io.IOException; 36 import java.io.InputStream; 37 38 public class ASCIIUtility { 39 40 // Private constructor so that this class is not instantiated 41 private ASCIIUtility() { 42 } 43 44 45 /** 46 * Convert the bytes within the specified range of the given byte 47 * array into a signed integer in the given radix . The range extends 48 * from <code>start</code> till, but not including <code>end</code>. 49 * 50 * Based on java.lang.Integer.parseInt(). 51 * 52 * @param b bytes to convert to integer. 53 * @param start start of the range. 54 * @param end end of the range (not including). 55 * @param radix radix. 56 * @return integer. 57 */ 58 public static int parseInt(byte[] b, int start, int end, int radix) 59 throws NumberFormatException { 60 if (b == null) 61 throw new NumberFormatException("null"); 62 63 int result = 0; 64 boolean negative = false; 65 int i = start; 66 int limit; 67 int multmin; 68 int digit; 69 70 if (end > start) { 71 if (b[i] == '-') { 72 negative = true; 73 limit = Integer.MIN_VALUE; 74 i++; 75 } else { 76 limit = -Integer.MAX_VALUE; 77 } 78 multmin = limit / radix; 79 if (i < end) { 80 digit = Character.digit((char) b[i++], radix); 81 if (digit < 0) { 82 throw new NumberFormatException( 83 "illegal number: " + toString(b, start, end) 84 ); 85 } else { 86 result = -digit; 87 } 88 } 89 while (i < end) { 90 // Accumulating negatively avoids surprises near MAX_VALUE 91 digit = Character.digit((char) b[i++], radix); 92 if (digit < 0) { 93 throw new NumberFormatException("illegal number"); 94 } 95 if (result < multmin) { 96 throw new NumberFormatException("illegal number"); 97 } 98 result *= radix; 99 if (result < limit + digit) { 100 throw new NumberFormatException("illegal number"); 101 } 102 result -= digit; 103 } 104 } else { 105 throw new NumberFormatException("illegal number"); 106 } 107 if (negative) { 108 if (i > start + 1) { 109 return result; 110 } else { /* Only got "-" */ 111 throw new NumberFormatException("illegal number"); 112 } 113 } else { 114 return -result; 115 } 116 } 117 118 /** 119 * Convert the bytes within the specified range of the given byte 120 * array into a String. The range extends from <code>start</code> 121 * till, but not including <code>end</code>. 122 * 123 * @param b bytes to convert to integer. 124 * @param start start of the range. 125 * @param end end of the range (not including). 126 * @return integer. 127 */ 128 public static String toString(byte[] b, int start, int end) { 129 int size = end - start; 130 char[] theChars = new char[size]; 131 132 for (int i = 0, j = start; i < size; ) 133 theChars[i++] = (char) (b[j++] & 0xff); 134 135 return new String(theChars); 136 } 137 138 /** 139 * Encodes specified String into a sequence of bytes using the platform's 140 * default charset, storing the result into a new byte array. 141 * 142 * @param s string to encode into byte array. 143 * @return byte array. 144 */ 145 public static byte[] getBytes(String s) { 146 char[] chars = s.toCharArray(); 147 int size = chars.length; 148 byte[] bytes = new byte[size]; 149 150 for (int i = 0; i < size; ) 151 bytes[i] = (byte) chars[i++]; 152 return bytes; 153 } 154 155 /** 156 * Converts input stream to array. 157 * 158 * @param is stream to convert to array. 159 * @return byte array. 160 * @throws IOException if an I/O error occurs. 161 * @deprecated this is an expensive operation that require an additional 162 * buffer reallocation just to get the array of an exact size. 163 * Unless you absolutely need the exact size array, don't use this. 164 * Use {@link ByteOutputStream} and {@link ByteOutputStream#write(InputStream)}. 165 */ 166 @Deprecated 167 public static byte[] getBytes(InputStream is) throws IOException { 168 ByteOutputStream bos = null; 169 try { 170 bos = new ByteOutputStream(); 171 bos.write(is); 172 } finally { 173 if (bos != null) 174 bos.close(); 175 is.close(); 176 } 177 return bos.toByteArray(); 178 } 179 }