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 } | 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 } |