1 50 package org.apache.avalon.excalibur.io; 51 52 import java.io.BufferedInputStream ; 53 import java.io.BufferedOutputStream ; 54 import java.io.ByteArrayInputStream ; 55 import java.io.ByteArrayOutputStream ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.InputStreamReader ; 59 import java.io.OutputStream ; 60 import java.io.OutputStreamWriter ; 61 import java.io.Reader ; 62 import java.io.StringReader ; 63 import java.io.StringWriter ; 64 import java.io.Writer ; 65 66 116 117 147 148 public final class IOUtil 149 { 150 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 151 152 155 private IOUtil() 156 { 157 } 158 159 165 public static void shutdownReader( final Reader input ) 166 { 167 if( null == input ) 168 { 169 return; 170 } 171 172 try 173 { 174 input.close(); 175 } 176 catch( final IOException ioe ) 177 { 178 } 179 } 180 181 187 public static void shutdownWriter( final Writer output ) 188 { 189 if( null == output ) 190 { 191 return; 192 } 193 194 try 195 { 196 output.close(); 197 } 198 catch( final IOException ioe ) 199 { 200 } 201 } 202 203 208 public static void shutdownStream( final OutputStream output ) 209 { 210 if( null == output ) 211 { 212 return; 213 } 214 215 try 216 { 217 output.close(); 218 } 219 catch( final IOException ioe ) 220 { 221 } 222 } 223 224 229 public static void shutdownStream( final InputStream input ) 230 { 231 if( null == input ) 232 { 233 return; 234 } 235 236 try 237 { 238 input.close(); 239 } 240 catch( final IOException ioe ) 241 { 242 } 243 } 244 245 249 252 public static void copy( final InputStream input, final OutputStream output ) 253 throws IOException 254 { 255 copy( input, output, DEFAULT_BUFFER_SIZE ); 256 } 257 258 262 public static void copy( final InputStream input, 263 final OutputStream output, 264 final int bufferSize ) 265 throws IOException 266 { 267 final byte[] buffer = new byte[ bufferSize ]; 268 int n = 0; 269 while( -1 != ( n = input.read( buffer ) ) ) 270 { 271 output.write( buffer, 0, n ); 272 } 273 } 274 275 278 public static void copy( final Reader input, final Writer output ) 279 throws IOException 280 { 281 copy( input, output, DEFAULT_BUFFER_SIZE ); 282 } 283 284 288 public static void copy( final Reader input, final Writer output, final int bufferSize ) 289 throws IOException 290 { 291 final char[] buffer = new char[ bufferSize ]; 292 int n = 0; 293 while( -1 != ( n = input.read( buffer ) ) ) 294 { 295 output.write( buffer, 0, n ); 296 } 297 } 298 299 304 305 308 313 public static void copy( final InputStream input, final Writer output ) 314 throws IOException 315 { 316 copy( input, output, DEFAULT_BUFFER_SIZE ); 317 } 318 319 325 public static void copy( final InputStream input, final Writer output, final int bufferSize ) 326 throws IOException 327 { 328 final InputStreamReader in = new InputStreamReader ( input ); 329 copy( in, output, bufferSize ); 330 } 331 332 339 public static void copy( final InputStream input, final Writer output, final String encoding ) 340 throws IOException 341 { 342 final InputStreamReader in = new InputStreamReader ( input, encoding ); 343 copy( in, output ); 344 } 345 346 354 public static void copy( final InputStream input, 355 final Writer output, 356 final String encoding, 357 final int bufferSize ) 358 throws IOException 359 { 360 final InputStreamReader in = new InputStreamReader ( input, encoding ); 361 copy( in, output, bufferSize ); 362 } 363 364 365 368 372 public static String toString( final InputStream input ) 373 throws IOException 374 { 375 return toString( input, DEFAULT_BUFFER_SIZE ); 376 } 377 378 383 public static String toString( final InputStream input, final int bufferSize ) 384 throws IOException 385 { 386 final StringWriter sw = new StringWriter (); 387 copy( input, sw, bufferSize ); 388 return sw.toString(); 389 } 390 391 397 public static String toString( final InputStream input, final String encoding ) 398 throws IOException 399 { 400 return toString( input, encoding, DEFAULT_BUFFER_SIZE ); 401 } 402 403 410 public static String toString( final InputStream input, 411 final String encoding, 412 final int bufferSize ) 413 throws IOException 414 { 415 final StringWriter sw = new StringWriter (); 416 copy( input, sw, encoding, bufferSize ); 417 return sw.toString(); 418 } 419 420 423 426 public static byte[] toByteArray( final InputStream input ) 427 throws IOException 428 { 429 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 430 } 431 432 436 public static byte[] toByteArray( final InputStream input, final int bufferSize ) 437 throws IOException 438 { 439 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 440 copy( input, output, bufferSize ); 441 return output.toByteArray(); 442 } 443 444 445 450 456 public static void copy( final Reader input, final OutputStream output ) 457 throws IOException 458 { 459 copy( input, output, DEFAULT_BUFFER_SIZE ); 460 } 461 462 467 public static void copy( final Reader input, final OutputStream output, final int bufferSize ) 468 throws IOException 469 { 470 final OutputStreamWriter out = new OutputStreamWriter ( output ); 471 copy( input, out, bufferSize ); 472 out.flush(); 475 } 476 477 482 public static String toString( final Reader input ) 483 throws IOException 484 { 485 return toString( input, DEFAULT_BUFFER_SIZE ); 486 } 487 488 492 public static String toString( final Reader input, final int bufferSize ) 493 throws IOException 494 { 495 final StringWriter sw = new StringWriter (); 496 copy( input, sw, bufferSize ); 497 return sw.toString(); 498 } 499 500 501 506 public static byte[] toByteArray( final Reader input ) 507 throws IOException 508 { 509 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 510 } 511 512 516 public static byte[] toByteArray( final Reader input, final int bufferSize ) 517 throws IOException 518 { 519 ByteArrayOutputStream output = new ByteArrayOutputStream (); 520 copy( input, output, bufferSize ); 521 return output.toByteArray(); 522 } 523 524 525 530 531 534 538 public static void copy( final String input, final OutputStream output ) 539 throws IOException 540 { 541 copy( input, output, DEFAULT_BUFFER_SIZE ); 542 } 543 544 549 public static void copy( final String input, final OutputStream output, final int bufferSize ) 550 throws IOException 551 { 552 final StringReader in = new StringReader ( input ); 553 final OutputStreamWriter out = new OutputStreamWriter ( output ); 554 copy( in, out, bufferSize ); 555 out.flush(); 558 } 559 560 561 562 565 568 public static void copy( final String input, final Writer output ) 569 throws IOException 570 { 571 output.write( input ); 572 } 573 574 585 public static void bufferedCopy( final InputStream input, final OutputStream output ) 586 throws IOException 587 { 588 final BufferedInputStream in = new BufferedInputStream ( input ); 589 final BufferedOutputStream out = new BufferedOutputStream ( output ); 590 copy( in, out ); 591 out.flush(); 592 } 593 594 595 600 public static byte[] toByteArray( final String input ) 601 throws IOException 602 { 603 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 604 } 605 606 610 public static byte[] toByteArray( final String input, final int bufferSize ) 611 throws IOException 612 { 613 ByteArrayOutputStream output = new ByteArrayOutputStream (); 614 copy( input, output, bufferSize ); 615 return output.toByteArray(); 616 } 617 618 619 620 625 626 629 634 public static void copy( final byte[] input, final Writer output ) 635 throws IOException 636 { 637 copy( input, output, DEFAULT_BUFFER_SIZE ); 638 } 639 640 646 public static void copy( final byte[] input, final Writer output, final int bufferSize ) 647 throws IOException 648 { 649 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 650 copy( in, output, bufferSize ); 651 } 652 653 660 public static void copy( final byte[] input, final Writer output, final String encoding ) 661 throws IOException 662 { 663 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 664 copy( in, output, encoding ); 665 } 666 667 675 public static void copy( final byte[] input, 676 final Writer output, 677 final String encoding, 678 final int bufferSize ) 679 throws IOException 680 { 681 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 682 copy( in, output, encoding, bufferSize ); 683 } 684 685 686 689 693 public static String toString( final byte[] input ) 694 throws IOException 695 { 696 return toString( input, DEFAULT_BUFFER_SIZE ); 697 } 698 699 704 public static String toString( final byte[] input, final int bufferSize ) 705 throws IOException 706 { 707 final StringWriter sw = new StringWriter (); 708 copy( input, sw, bufferSize ); 709 return sw.toString(); 710 } 711 712 718 public static String toString( final byte[] input, final String encoding ) 719 throws IOException 720 { 721 return toString( input, encoding, DEFAULT_BUFFER_SIZE ); 722 } 723 724 731 public static String toString( final byte[] input, 732 final String encoding, 733 final int bufferSize ) 734 throws IOException 735 { 736 final StringWriter sw = new StringWriter (); 737 copy( input, sw, encoding, bufferSize ); 738 return sw.toString(); 739 } 740 741 742 745 748 public static void copy( final byte[] input, final OutputStream output ) 749 throws IOException 750 { 751 copy( input, output, DEFAULT_BUFFER_SIZE ); 752 } 753 754 758 public static void copy( final byte[] input, 759 final OutputStream output, 760 final int bufferSize ) 761 throws IOException 762 { 763 output.write( input ); 764 } 765 766 773 public static boolean contentEquals( final InputStream input1, 774 final InputStream input2 ) 775 throws IOException 776 { 777 final InputStream bufferedInput1 = new BufferedInputStream ( input1 ); 778 final InputStream bufferedInput2 = new BufferedInputStream ( input2 ); 779 780 int ch = bufferedInput1.read(); 781 while( -1 != ch ) 782 { 783 final int ch2 = bufferedInput2.read(); 784 if( ch != ch2 ) 785 { 786 return false; 787 } 788 ch = bufferedInput1.read(); 789 } 790 791 final int ch2 = bufferedInput2.read(); 792 if( -1 != ch2 ) 793 { 794 return false; 795 } 796 else 797 { 798 return true; 799 } 800 } 801 } 802 | Popular Tags |