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.*; 36 37 public class ASCIIUtility { 38 39 // Private constructor so that this class is not instantiated 40 private ASCIIUtility() { } 41 42 43 /** 44 * Convert the bytes within the specified range of the given byte 45 * array into a signed integer in the given radix . The range extends 46 * from <code>start</code> till, but not including <code>end</code>. 47 * 48 * Based on java.lang.Integer.parseInt(). 49 * 50 * @param b bytes to convert to integer. 51 * @param start start of the range. 52 * @param end end of the range (not including). 53 * @param radix radix. 54 * 55 * @return integer. 56 * 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 * 127 * @return integer. 128 * 129 */ 130 public static String toString(byte[] b, int start, int end) { 131 int size = end - start; 132 char[] theChars = new char[size]; 133 134 for (int i = 0, j = start; i < size; ) 135 theChars[i++] = (char)(b[j++]&0xff); 136 137 return new String(theChars); 138 } 139 140 /** 141 * Encodes specified String into a sequence of bytes using the platform's 142 * default charset, storing the result into a new byte array. 143 * 144 * @param s string to encode into byte array. 145 * 146 * @return byte array. 147 * 148 */ 149 public static byte[] getBytes(String s) { 150 char [] chars= s.toCharArray(); 151 int size = chars.length; 152 byte[] bytes = new byte[size]; 153 154 for (int i = 0; i < size;) 155 bytes[i] = (byte) chars[i++]; 156 return bytes; 157 } 158 159 /** 160 * Converts input stream to array. 161 * 162 * @param is stream to convert to array. 163 * 164 * @return byte array. 165 * 166 * @throws IOException if an I/O error occurs. 167 * 168 * @deprecated 169 * this is an expensive operation that require an additional 170 * buffer reallocation just to get the array of an exact size. 171 * Unless you absolutely need the exact size array, don't use this. 172 * Use {@link ByteOutputStream} and {@link ByteOutputStream#write(InputStream)}. 173 */ 174 @Deprecated 175 public static byte[] getBytes(InputStream is) throws IOException { 176 ByteOutputStream bos = null; 177 try { 178 bos = new ByteOutputStream(); 179 bos.write(is); 180 } finally { 181 if (bos != null) 182 bos.close(); 183 is.close(); 184 } 185 return bos.toByteArray(); 186 } 187 }