1 17 18 package org.apache.avalon.cornerstone.blocks.masterstore; 19 20 import java.io.BufferedInputStream ; 21 import java.io.BufferedOutputStream ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Reader ; 30 import java.io.StringReader ; 31 import java.io.StringWriter ; 32 import java.io.Writer ; 33 34 84 85 115 116 public final class IOUtil 117 { 118 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 119 120 123 private IOUtil() 124 { 125 } 126 127 133 public static void shutdownReader( final Reader input ) 134 { 135 if( null == input ) 136 { 137 return; 138 } 139 140 try 141 { 142 input.close(); 143 } 144 catch( final IOException ioe ) 145 { 146 } 147 } 148 149 155 public static void shutdownWriter( final Writer output ) 156 { 157 if( null == output ) 158 { 159 return; 160 } 161 162 try 163 { 164 output.close(); 165 } 166 catch( final IOException ioe ) 167 { 168 } 169 } 170 171 176 public static void shutdownStream( final OutputStream output ) 177 { 178 if( null == output ) 179 { 180 return; 181 } 182 183 try 184 { 185 output.close(); 186 } 187 catch( final IOException ioe ) 188 { 189 } 190 } 191 192 197 public static void shutdownStream( final InputStream input ) 198 { 199 if( null == input ) 200 { 201 return; 202 } 203 204 try 205 { 206 input.close(); 207 } 208 catch( final IOException ioe ) 209 { 210 } 211 } 212 213 217 220 public static void copy( final InputStream input, final OutputStream output ) 221 throws IOException 222 { 223 copy( input, output, DEFAULT_BUFFER_SIZE ); 224 } 225 226 230 public static void copy( final InputStream input, 231 final OutputStream output, 232 final int bufferSize ) 233 throws IOException 234 { 235 final byte[] buffer = new byte[ bufferSize ]; 236 int n = 0; 237 while( -1 != ( n = input.read( buffer ) ) ) 238 { 239 output.write( buffer, 0, n ); 240 } 241 } 242 243 246 public static void copy( final Reader input, final Writer output ) 247 throws IOException 248 { 249 copy( input, output, DEFAULT_BUFFER_SIZE ); 250 } 251 252 256 public static void copy( final Reader input, final Writer output, final int bufferSize ) 257 throws IOException 258 { 259 final char[] buffer = new char[ bufferSize ]; 260 int n = 0; 261 while( -1 != ( n = input.read( buffer ) ) ) 262 { 263 output.write( buffer, 0, n ); 264 } 265 } 266 267 272 273 276 281 public static void copy( final InputStream input, final Writer output ) 282 throws IOException 283 { 284 copy( input, output, DEFAULT_BUFFER_SIZE ); 285 } 286 287 293 public static void copy( final InputStream input, final Writer output, final int bufferSize ) 294 throws IOException 295 { 296 final InputStreamReader in = new InputStreamReader ( input ); 297 copy( in, output, bufferSize ); 298 } 299 300 307 public static void copy( final InputStream input, final Writer output, final String encoding ) 308 throws IOException 309 { 310 final InputStreamReader in = new InputStreamReader ( input, encoding ); 311 copy( in, output ); 312 } 313 314 322 public static void copy( final InputStream input, 323 final Writer output, 324 final String encoding, 325 final int bufferSize ) 326 throws IOException 327 { 328 final InputStreamReader in = new InputStreamReader ( input, encoding ); 329 copy( in, output, bufferSize ); 330 } 331 332 333 336 340 public static String toString( final InputStream input ) 341 throws IOException 342 { 343 return toString( input, DEFAULT_BUFFER_SIZE ); 344 } 345 346 351 public static String toString( final InputStream input, final int bufferSize ) 352 throws IOException 353 { 354 final StringWriter sw = new StringWriter (); 355 copy( input, sw, bufferSize ); 356 return sw.toString(); 357 } 358 359 365 public static String toString( final InputStream input, final String encoding ) 366 throws IOException 367 { 368 return toString( input, encoding, DEFAULT_BUFFER_SIZE ); 369 } 370 371 378 public static String toString( final InputStream input, 379 final String encoding, 380 final int bufferSize ) 381 throws IOException 382 { 383 final StringWriter sw = new StringWriter (); 384 copy( input, sw, encoding, bufferSize ); 385 return sw.toString(); 386 } 387 388 391 394 public static byte[] toByteArray( final InputStream input ) 395 throws IOException 396 { 397 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 398 } 399 400 404 public static byte[] toByteArray( final InputStream input, final int bufferSize ) 405 throws IOException 406 { 407 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 408 copy( input, output, bufferSize ); 409 return output.toByteArray(); 410 } 411 412 413 418 424 public static void copy( final Reader input, final OutputStream output ) 425 throws IOException 426 { 427 copy( input, output, DEFAULT_BUFFER_SIZE ); 428 } 429 430 435 public static void copy( final Reader input, final OutputStream output, final int bufferSize ) 436 throws IOException 437 { 438 final OutputStreamWriter out = new OutputStreamWriter ( output ); 439 copy( input, out, bufferSize ); 440 out.flush(); 443 } 444 445 450 public static String toString( final Reader input ) 451 throws IOException 452 { 453 return toString( input, DEFAULT_BUFFER_SIZE ); 454 } 455 456 460 public static String toString( final Reader input, final int bufferSize ) 461 throws IOException 462 { 463 final StringWriter sw = new StringWriter (); 464 copy( input, sw, bufferSize ); 465 return sw.toString(); 466 } 467 468 469 474 public static byte[] toByteArray( final Reader input ) 475 throws IOException 476 { 477 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 478 } 479 480 484 public static byte[] toByteArray( final Reader input, final int bufferSize ) 485 throws IOException 486 { 487 ByteArrayOutputStream output = new ByteArrayOutputStream (); 488 copy( input, output, bufferSize ); 489 return output.toByteArray(); 490 } 491 492 493 498 499 502 506 public static void copy( final String input, final OutputStream output ) 507 throws IOException 508 { 509 copy( input, output, DEFAULT_BUFFER_SIZE ); 510 } 511 512 517 public static void copy( final String input, final OutputStream output, final int bufferSize ) 518 throws IOException 519 { 520 final StringReader in = new StringReader ( input ); 521 final OutputStreamWriter out = new OutputStreamWriter ( output ); 522 copy( in, out, bufferSize ); 523 out.flush(); 526 } 527 528 529 530 533 536 public static void copy( final String input, final Writer output ) 537 throws IOException 538 { 539 output.write( input ); 540 } 541 542 553 public static void bufferedCopy( final InputStream input, final OutputStream output ) 554 throws IOException 555 { 556 final BufferedInputStream in = new BufferedInputStream ( input ); 557 final BufferedOutputStream out = new BufferedOutputStream ( output ); 558 copy( in, out ); 559 out.flush(); 560 } 561 562 563 568 public static byte[] toByteArray( final String input ) 569 throws IOException 570 { 571 return toByteArray( input, DEFAULT_BUFFER_SIZE ); 572 } 573 574 578 public static byte[] toByteArray( final String input, final int bufferSize ) 579 throws IOException 580 { 581 ByteArrayOutputStream output = new ByteArrayOutputStream (); 582 copy( input, output, bufferSize ); 583 return output.toByteArray(); 584 } 585 586 587 588 593 594 597 602 public static void copy( final byte[] input, final Writer output ) 603 throws IOException 604 { 605 copy( input, output, DEFAULT_BUFFER_SIZE ); 606 } 607 608 614 public static void copy( final byte[] input, final Writer output, final int bufferSize ) 615 throws IOException 616 { 617 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 618 copy( in, output, bufferSize ); 619 } 620 621 628 public static void copy( final byte[] input, final Writer output, final String encoding ) 629 throws IOException 630 { 631 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 632 copy( in, output, encoding ); 633 } 634 635 643 public static void copy( final byte[] input, 644 final Writer output, 645 final String encoding, 646 final int bufferSize ) 647 throws IOException 648 { 649 final ByteArrayInputStream in = new ByteArrayInputStream ( input ); 650 copy( in, output, encoding, bufferSize ); 651 } 652 653 654 657 661 public static String toString( final byte[] input ) 662 throws IOException 663 { 664 return toString( input, DEFAULT_BUFFER_SIZE ); 665 } 666 667 672 public static String toString( final byte[] input, final int bufferSize ) 673 throws IOException 674 { 675 final StringWriter sw = new StringWriter (); 676 copy( input, sw, bufferSize ); 677 return sw.toString(); 678 } 679 680 686 public static String toString( final byte[] input, final String encoding ) 687 throws IOException 688 { 689 return toString( input, encoding, DEFAULT_BUFFER_SIZE ); 690 } 691 692 699 public static String toString( final byte[] input, 700 final String encoding, 701 final int bufferSize ) 702 throws IOException 703 { 704 final StringWriter sw = new StringWriter (); 705 copy( input, sw, encoding, bufferSize ); 706 return sw.toString(); 707 } 708 709 710 713 716 public static void copy( final byte[] input, final OutputStream output ) 717 throws IOException 718 { 719 copy( input, output, DEFAULT_BUFFER_SIZE ); 720 } 721 722 726 public static void copy( final byte[] input, 727 final OutputStream output, 728 final int bufferSize ) 729 throws IOException 730 { 731 output.write( input ); 732 } 733 734 741 public static boolean contentEquals( final InputStream input1, 742 final InputStream input2 ) 743 throws IOException 744 { 745 final InputStream bufferedInput1 = new BufferedInputStream ( input1 ); 746 final InputStream bufferedInput2 = new BufferedInputStream ( input2 ); 747 748 int ch = bufferedInput1.read(); 749 while( -1 != ch ) 750 { 751 final int ch2 = bufferedInput2.read(); 752 if( ch != ch2 ) 753 { 754 return false; 755 } 756 ch = bufferedInput1.read(); 757 } 758 759 final int ch2 = bufferedInput2.read(); 760 if( -1 != ch2 ) 761 { 762 return false; 763 } 764 else 765 { 766 return true; 767 } 768 } 769 } 770 | Popular Tags |