1 32 33 package com.imagero.uio.io; 34 35 import com.imagero.uio.RandomAccessRO; 36 import com.imagero.uio.Sys; 37 38 import java.io.BufferedReader ; 39 import java.io.BufferedWriter ; 40 import java.io.EOFException ; 41 import java.io.File ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.OutputStream ; 45 import java.io.RandomAccessFile ; 46 47 52 public class IOutils { 53 54 60 public static void closeStream(BufferedWriter bw) { 61 try { 62 if(bw != null) { 63 bw.close(); 64 } 65 } 66 catch(IOException ex) { 67 } 68 } 69 70 76 public static void closeStream(BufferedReader br) { 77 try { 78 if(br != null) { 79 br.close(); 80 } 81 } 82 catch(IOException ex) { 83 } 84 } 85 86 92 public static void closeStream(InputStream is) { 93 try { 94 if(is != null) { 95 is.close(); 96 } 97 } 98 catch(IOException ex) { 99 } 100 } 101 102 108 public static void closeStream(OutputStream os) { 109 try { 110 if(os != null) { 111 os.close(); 112 } 113 } 114 catch(IOException ex) { 115 } 116 } 117 118 124 public static void closeStream(RandomAccessFile raf) { 125 try { 126 if(raf != null) { 127 raf.close(); 128 } 129 } 130 catch(IOException ex) { 131 } 132 } 133 134 140 public static void closeStream(RandomAccessRO ro) { 141 try { 142 if(ro != null) { 143 ro.close(); 144 } 145 } 146 catch(IOException ex) { 147 } 148 } 149 150 static final int[] mask = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; 151 static byte b0 = (byte) '0'; 152 static byte b1 = (byte) '1'; 153 154 public static String toBinaryString(byte value) { 155 byte[] b = new byte[8]; 156 int cnt = 0; 157 for(int i = 7; i > -1; i--) { 158 b[cnt++] = (value & mask[i]) == 0 ? b0 : b1; 159 } 160 return new String (b); 161 } 162 163 public static String toBinaryString(char value) { 164 byte[] b = new byte[16]; 165 int cnt = 0; 166 for(int i = 15; i > -1; i--) { 167 b[cnt++] = (value & mask[i]) == 0 ? b0 : b1; 168 } 169 return new String (b); 170 } 171 172 public static String toBinaryString(int value, int length) { 173 byte[] b = new byte[length]; 174 int cnt = 0; 175 for(int i = length - 1; i > -1; i--) { 176 if(((value >> i) & 1) == 1) { 177 b[cnt++] = b1; 178 } 179 else { 180 b[cnt++] = b0; 181 } 182 } 183 return new String (b); 184 } 185 186 final static byte[] digits = { 187 (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', 188 (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', 189 (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' 190 }; 191 192 public static String toHexString(byte value) { 193 return toUnsignedString(value & 0xFF, 4); 194 } 195 196 private static String toUnsignedString(int i, int shift) { 197 byte[] buf = new byte[]{(byte) '0', (byte) '0'}; 198 int charPos = 2; 199 int radix = 1 << shift; 200 int mask = radix - 1; 201 do { 202 buf[--charPos] = digits[i & mask]; 203 i >>>= shift; 204 } 205 while(i != 0); 206 207 return new String (buf); 208 } 209 210 public static void printHexByte(int value) { 211 printHexImpl(value & 0xFFFF, 2); 212 } 213 214 public static void printlnHexByte(int value) { 215 printHexImpl(value & 0xFFFF, 2); 216 Sys.out.println(""); 217 } 218 219 public static void printHexShort(int value) { 220 printHexImpl(value & 0xFFFF, 4); 221 } 222 223 public static void printlnHexShort(int value) { 224 printHexImpl(value & 0xFFFF, 4); 225 Sys.out.println(""); 226 } 227 228 public static void printHexInt(int value) { 229 printHexImpl(value & 0xFFFFFFFF, 8); 230 } 231 232 public static void printlnHexInt(int value) { 233 printHexImpl(value & 0xFFFFFFFF, 8); 234 Sys.out.println(""); 235 } 236 237 public static void printHexLong(long value) { 238 printHexImpl(value & 0xFFFFFFFFFFFFFFFFL, 16); 239 } 240 241 public static void printlnHexLong(long value) { 242 printHexImpl(value & 0xFFFFFFFFFFFFFFFFL, 16); 243 Sys.out.println(""); 244 } 245 246 static void printHexImpl(long value, int length) { 247 String s = Long.toHexString(value); 248 for(int i = 0, size = length - s.length(); i < size; i++) { 250 Sys.out.print("0"); 251 } 252 Sys.out.print(s); 253 } 254 255 static void printHexImpl(int value, int length) { 256 String s = Integer.toHexString(value); 257 if(s.length() > length) { 258 s = s.substring(s.length() - length); 259 } 260 for(int i = 0, size = length - s.length(); i < size; i++) { 262 Sys.out.print("0"); 263 } 264 Sys.out.print(s); 265 } 266 267 public static String getExtension(File f) { 268 String s = f.getName(); 269 return s.substring(s.lastIndexOf(".") + 1).toUpperCase(); 270 } 271 272 275 public static int readShort4D(InputStream in) throws IOException { 276 return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0); 277 } 278 279 282 public static int readShort4D(RandomAccessFile in) throws IOException { 283 return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0); 284 } 285 286 289 public static int readShort4D(RandomAccessRO in) throws IOException { 290 return ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0); 291 } 292 293 296 public static int readShort49(InputStream in) throws IOException { 297 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8); 298 } 299 300 303 public static int readShort49(RandomAccessFile in) throws IOException { 304 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8); 305 } 306 307 310 public static int readShort49(RandomAccessRO in) throws IOException { 311 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8); 312 } 313 314 317 public static int readInt4D(InputStream in) throws IOException { 318 return (((in.read() & 0xFF) << 24) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 0)); 319 } 320 321 324 public static int readInt4D(RandomAccessFile in) throws IOException { 325 int b0 = (in.read() & 0xFF); 326 int b1 = (in.read() & 0xFF); 327 int b2 = (in.read() & 0xFF); 328 int b3 = (in.read() & 0xFF); 329 return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3; 330 } 331 332 335 public static int readInt4D(RandomAccessRO in) throws IOException { 336 int b0 = (in.read() & 0xFF); 337 int b1 = (in.read() & 0xFF); 338 int b2 = (in.read() & 0xFF); 339 int b3 = (in.read() & 0xFF); 340 return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3; 341 } 342 343 346 public static int readInt49(InputStream in) throws IOException { 347 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24); 348 } 349 350 353 public static int readInt49(RandomAccessFile in) throws IOException { 354 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24); 355 } 356 357 360 public static int readInt49(RandomAccessRO in) throws IOException { 361 return ((in.read() & 0xFF) << 0) + ((in.read() & 0xFF) << 8) + ((in.read() & 0xFF) << 16) + ((in.read() & 0xFF) << 24); 362 } 363 364 367 public static long readLong4D(InputStream in) throws IOException { 368 return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL); 369 } 370 371 374 public static long readLong4D(RandomAccessFile in) throws IOException { 375 return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL); 376 } 377 378 381 public static long readLong4D(RandomAccessRO in) throws IOException { 382 return ((long) (readInt4D(in)) << 32) + (readInt4D(in) & 0xFFFFFFFFL); 383 } 384 385 388 public static long readLong49(InputStream in) throws IOException { 389 return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32); 390 } 391 392 395 public static long readLong49(RandomAccessFile in) throws IOException { 396 return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32); 397 } 398 399 402 public static long readLong49(RandomAccessRO in) throws IOException { 403 return ((long) (readInt49(in)) & 0xFFFFFFFFL) + (readInt49(in) << 32); 404 } 405 406 407 public static void readFully(InputStream in, byte b[]) throws IOException { 408 readFully(in, b, 0, b.length); 409 } 410 411 public static void readFully(InputStream in, byte b[], int off, int len) throws IOException { 412 int n = 0; 413 do { 414 int count = in.read(b, off + n, len - n); 415 if(count < 0) { 416 throw new EOFException ("eof"); 417 } 419 n += count; 420 } 421 while(n < len); 422 } 423 424 434 public static int readFully2(InputStream in, byte b[]) throws IOException { 435 return readFully2(in, b, 0, b.length); 436 } 437 438 450 public static int readFully2(InputStream in, byte b[], int off, int len) throws IOException { 451 int n = 0; 452 do { 453 int count = in.read(b, off + n, len - n); 454 if(count < 0) { 455 return n == 0 ? -1 : n; 456 } 457 n += count; 458 } 459 while(n < len); 460 return n; 461 } 462 } 463
| Popular Tags
|