1 21 22 27 package com.sun.mail.util; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.InputStream ; 32 import java.io.IOException ; 33 34 public class ASCIIUtility { 35 36 private ASCIIUtility() { } 38 39 46 public static int parseInt(byte[] b, int start, int end, int radix) 47 throws NumberFormatException { 48 if (b == null) 49 throw new NumberFormatException ("null"); 50 51 int result = 0; 52 boolean negative = false; 53 int i = start; 54 int limit; 55 int multmin; 56 int digit; 57 58 if (end > start) { 59 if (b[i] == '-') { 60 negative = true; 61 limit = Integer.MIN_VALUE; 62 i++; 63 } else { 64 limit = -Integer.MAX_VALUE; 65 } 66 multmin = limit / radix; 67 if (i < end) { 68 digit = Character.digit((char)b[i++], radix); 69 if (digit < 0) { 70 throw new NumberFormatException ( 71 "illegal number: " + toString(b, start, end) 72 ); 73 } else { 74 result = -digit; 75 } 76 } 77 while (i < end) { 78 digit = Character.digit((char)b[i++], radix); 80 if (digit < 0) { 81 throw new NumberFormatException ("illegal number"); 82 } 83 if (result < multmin) { 84 throw new NumberFormatException ("illegal number"); 85 } 86 result *= radix; 87 if (result < limit + digit) { 88 throw new NumberFormatException ("illegal number"); 89 } 90 result -= digit; 91 } 92 } else { 93 throw new NumberFormatException ("illegal number"); 94 } 95 if (negative) { 96 if (i > start + 1) { 97 return result; 98 } else { 99 throw new NumberFormatException ("illegal number"); 100 } 101 } else { 102 return -result; 103 } 104 } 105 106 111 public static int parseInt(byte[] b, int start, int end) 112 throws NumberFormatException { 113 return parseInt(b, start, end, 10); 114 } 115 116 123 public static long parseLong(byte[] b, int start, int end, int radix) 124 throws NumberFormatException { 125 if (b == null) 126 throw new NumberFormatException ("null"); 127 128 long result = 0; 129 boolean negative = false; 130 int i = start; 131 long limit; 132 long multmin; 133 int digit; 134 135 if (end > start) { 136 if (b[i] == '-') { 137 negative = true; 138 limit = Long.MIN_VALUE; 139 i++; 140 } else { 141 limit = -Long.MAX_VALUE; 142 } 143 multmin = limit / radix; 144 if (i < end) { 145 digit = Character.digit((char)b[i++], radix); 146 if (digit < 0) { 147 throw new NumberFormatException ( 148 "illegal number: " + toString(b, start, end) 149 ); 150 } else { 151 result = -digit; 152 } 153 } 154 while (i < end) { 155 digit = Character.digit((char)b[i++], radix); 157 if (digit < 0) { 158 throw new NumberFormatException ("illegal number"); 159 } 160 if (result < multmin) { 161 throw new NumberFormatException ("illegal number"); 162 } 163 result *= radix; 164 if (result < limit + digit) { 165 throw new NumberFormatException ("illegal number"); 166 } 167 result -= digit; 168 } 169 } else { 170 throw new NumberFormatException ("illegal number"); 171 } 172 if (negative) { 173 if (i > start + 1) { 174 return result; 175 } else { 176 throw new NumberFormatException ("illegal number"); 177 } 178 } else { 179 return -result; 180 } 181 } 182 183 188 public static long parseLong(byte[] b, int start, int end) 189 throws NumberFormatException { 190 return parseLong(b, start, end, 10); 191 } 192 193 198 public static String toString(byte[] b, int start, int end) { 199 int size = end - start; 200 char[] theChars = new char[size]; 201 202 for (int i = 0, j = start; i < size; ) 203 theChars[i++] = (char)(b[j++]&0xff); 204 205 return new String (theChars); 206 } 207 208 public static String toString(ByteArrayInputStream is) { 209 int size = is.available(); 210 char[] theChars = new char[size]; 211 byte[] bytes = new byte[size]; 212 213 is.read(bytes, 0, size); 214 for (int i = 0; i < size;) 215 theChars[i] = (char)(bytes[i++]&0xff); 216 217 return new String (theChars); 218 } 219 220 221 public static byte[] getBytes(String s) { 222 char [] chars= s.toCharArray(); 223 int size = chars.length; 224 byte[] bytes = new byte[size]; 225 226 for (int i = 0; i < size;) 227 bytes[i] = (byte) chars[i++]; 228 return bytes; 229 } 230 231 public static byte[] getBytes(InputStream is) throws IOException { 232 233 int len; 234 int size = 1024; 235 byte [] buf; 236 237 238 if (is instanceof ByteArrayInputStream ) { 239 size = is.available(); 240 buf = new byte[size]; 241 len = is.read(buf, 0, size); 242 } 243 else { 244 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 245 buf = new byte[size]; 246 while ((len = is.read(buf, 0, size)) != -1) 247 bos.write(buf, 0, len); 248 buf = bos.toByteArray(); 249 } 250 return buf; 251 } 252 } 253 | Popular Tags |