1 23 24 33 package org.dbforms.util.external; 34 35 36 89 import java.io.BufferedInputStream ; 90 import java.io.BufferedOutputStream ; 91 import java.io.ByteArrayInputStream ; 92 import java.io.ByteArrayOutputStream ; 93 import java.io.IOException ; 94 import java.io.InputStream ; 95 import java.io.InputStreamReader ; 96 import java.io.OutputStream ; 97 import java.io.OutputStreamWriter ; 98 import java.io.Reader ; 99 import java.io.StringReader ; 100 import java.io.StringWriter ; 101 import java.io.Writer ; 102 103 104 105 111 public final class IOUtil { 112 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 113 114 117 private IOUtil() { 118 } 119 120 137 public static void bufferedCopy(final InputStream input, 138 final OutputStream output) 139 throws IOException { 140 final BufferedInputStream in = new BufferedInputStream (input); 141 final BufferedOutputStream out = new BufferedOutputStream (output); 142 copy(in, out); 143 out.flush(); 144 } 145 146 147 159 public static boolean contentEquals(final InputStream input1, 160 final InputStream input2) 161 throws IOException { 162 final InputStream bufferedInput1 = new BufferedInputStream (input1); 163 final InputStream bufferedInput2 = new BufferedInputStream (input2); 164 165 int ch = bufferedInput1.read(); 166 167 while (-1 != ch) { 168 final int ch2 = bufferedInput2.read(); 169 170 if (ch != ch2) { 171 return false; 172 } 173 174 ch = bufferedInput1.read(); 175 } 176 177 final int ch2 = bufferedInput2.read(); 178 179 if (-1 != ch2) { 180 return false; 181 } else { 182 return true; 183 } 184 } 185 186 187 191 200 public static void copy(final InputStream input, 201 final OutputStream output) throws IOException { 202 copy(input, output, DEFAULT_BUFFER_SIZE); 203 } 204 205 206 216 public static void copy(final InputStream input, 217 final OutputStream output, 218 final int bufferSize) 219 throws IOException { 220 final byte[] buffer = new byte[bufferSize]; 221 int n = 0; 222 223 while (-1 != (n = input.read(buffer))) { 224 output.write(buffer, 0, n); 225 } 226 } 227 228 229 237 public static void copy(final Reader input, 238 final Writer output) throws IOException { 239 copy(input, output, DEFAULT_BUFFER_SIZE); 240 } 241 242 243 252 public static void copy(final Reader input, 253 final Writer output, 254 final int bufferSize) throws IOException { 255 final char[] buffer = new char[bufferSize]; 256 int n = 0; 257 258 while (-1 != (n = input.read(buffer))) { 259 output.write(buffer, 0, n); 260 } 261 } 262 263 264 271 281 public static void copy(final InputStream input, 282 final Writer output) throws IOException { 283 copy(input, output, DEFAULT_BUFFER_SIZE); 284 } 285 286 287 298 public static void copy(final InputStream input, 299 final Writer output, 300 final int bufferSize) 301 throws IOException { 302 final InputStreamReader in = new InputStreamReader (input); 303 copy(in, output, bufferSize); 304 } 305 306 307 319 public static void copy(final InputStream input, 320 final Writer output, 321 final String encoding) 322 throws IOException { 323 final InputStreamReader in = new InputStreamReader (input, encoding); 324 copy(in, output); 325 } 326 327 328 341 public static void copy(final InputStream input, 342 final Writer output, 343 final String encoding, 344 final int bufferSize) 345 throws IOException { 346 final InputStreamReader in = new InputStreamReader (input, encoding); 347 copy(in, output, bufferSize); 348 } 349 350 351 358 367 public static void copy(final Reader input, 368 final OutputStream output) throws IOException { 369 copy(input, output, DEFAULT_BUFFER_SIZE); 370 } 371 372 373 383 public static void copy(final Reader input, 384 final OutputStream output, 385 final int bufferSize) 386 throws IOException { 387 final OutputStreamWriter out = new OutputStreamWriter (output); 388 copy(input, out, bufferSize); 389 390 out.flush(); 393 } 394 395 396 403 412 public static void copy(final String input, 413 final OutputStream output) throws IOException { 414 copy(input, output, DEFAULT_BUFFER_SIZE); 415 } 416 417 418 428 public static void copy(final String input, 429 final OutputStream output, 430 final int bufferSize) 431 throws IOException { 432 final StringReader in = new StringReader (input); 433 final OutputStreamWriter out = new OutputStreamWriter (output); 434 copy(in, out, bufferSize); 435 436 out.flush(); 439 } 440 441 442 445 453 public static void copy(final String input, 454 final Writer output) throws IOException { 455 output.write(input); 456 } 457 458 459 466 476 public static void copy(final byte[] input, 477 final Writer output) throws IOException { 478 copy(input, output, DEFAULT_BUFFER_SIZE); 479 } 480 481 482 493 public static void copy(final byte[] input, 494 final Writer output, 495 final int bufferSize) throws IOException { 496 final ByteArrayInputStream in = new ByteArrayInputStream (input); 497 copy(in, output, bufferSize); 498 } 499 500 501 513 public static void copy(final byte[] input, 514 final Writer output, 515 final String encoding) throws IOException { 516 final ByteArrayInputStream in = new ByteArrayInputStream (input); 517 copy(in, output, encoding); 518 } 519 520 521 534 public static void copy(final byte[] input, 535 final Writer output, 536 final String encoding, 537 final int bufferSize) throws IOException { 538 final ByteArrayInputStream in = new ByteArrayInputStream (input); 539 copy(in, output, encoding, bufferSize); 540 } 541 542 543 546 554 public static void copy(final byte[] input, 555 final OutputStream output) throws IOException { 556 copy(input, output, DEFAULT_BUFFER_SIZE); 557 } 558 559 560 569 public static void copy(final byte[] input, 570 final OutputStream output, 571 final int bufferSize) 572 throws IOException { 573 output.write(input); 574 } 575 576 577 583 public static void shutdownReader(final Reader input) { 584 if (input == null) { 585 return; 586 } 587 588 try { 589 input.close(); 590 } catch (final IOException ioe) { 591 ; 592 } 593 } 594 595 596 602 public static void shutdownStream(final OutputStream output) { 603 if (output == null) { 604 return; 605 } 606 607 try { 608 output.close(); 609 } catch (final IOException ioe) { 610 ; 611 } 612 } 613 614 615 621 public static void shutdownStream(final InputStream input) { 622 if (input == null) { 623 return; 624 } 625 626 try { 627 input.close(); 628 } catch (final IOException ioe) { 629 ; 630 } 631 } 632 633 634 640 public static void shutdownWriter(final Writer output) { 641 if (output == null) { 642 return; 643 } 644 645 try { 646 output.close(); 647 } catch (final IOException ioe) { 648 ; 649 } 650 } 651 652 653 656 665 public static byte[] toByteArray(final InputStream input) 666 throws IOException { 667 return toByteArray(input, DEFAULT_BUFFER_SIZE); 668 } 669 670 671 681 public static byte[] toByteArray(final InputStream input, 682 final int bufferSize) 683 throws IOException { 684 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 685 copy(input, output, bufferSize); 686 687 return output.toByteArray(); 688 } 689 690 691 694 703 public static byte[] toByteArray(final Reader input) 704 throws IOException { 705 return toByteArray(input, DEFAULT_BUFFER_SIZE); 706 } 707 708 709 719 public static byte[] toByteArray(final Reader input, 720 final int bufferSize) 721 throws IOException { 722 ByteArrayOutputStream output = new ByteArrayOutputStream (); 723 copy(input, output, bufferSize); 724 725 return output.toByteArray(); 726 } 727 728 729 732 741 public static byte[] toByteArray(final String input) 742 throws IOException { 743 return toByteArray(input, DEFAULT_BUFFER_SIZE); 744 } 745 746 747 757 public static byte[] toByteArray(final String input, 758 final int bufferSize) 759 throws IOException { 760 ByteArrayOutputStream output = new ByteArrayOutputStream (); 761 copy(input, output, bufferSize); 762 763 return output.toByteArray(); 764 } 765 766 767 770 780 public static String toString(final InputStream input) 781 throws IOException { 782 return toString(input, DEFAULT_BUFFER_SIZE); 783 } 784 785 786 797 public static String toString(final InputStream input, 798 final int bufferSize) 799 throws IOException { 800 final StringWriter sw = new StringWriter (); 801 copy(input, sw, bufferSize); 802 803 return sw.toString(); 804 } 805 806 807 819 public static String toString(final InputStream input, 820 final String encoding) 821 throws IOException { 822 return toString(input, encoding, DEFAULT_BUFFER_SIZE); 823 } 824 825 826 839 public static String toString(final InputStream input, 840 final String encoding, 841 final int bufferSize) 842 throws IOException { 843 final StringWriter sw = new StringWriter (); 844 copy(input, sw, encoding, bufferSize); 845 846 return sw.toString(); 847 } 848 849 850 853 862 public static String toString(final Reader input) throws IOException { 863 return toString(input, DEFAULT_BUFFER_SIZE); 864 } 865 866 867 877 public static String toString(final Reader input, 878 final int bufferSize) 879 throws IOException { 880 final StringWriter sw = new StringWriter (); 881 copy(input, sw, bufferSize); 882 883 return sw.toString(); 884 } 885 886 887 890 900 public static String toString(final byte[] input) throws IOException { 901 return toString(input, DEFAULT_BUFFER_SIZE); 902 } 903 904 905 916 public static String toString(final byte[] input, 917 final int bufferSize) 918 throws IOException { 919 final StringWriter sw = new StringWriter (); 920 copy(input, sw, bufferSize); 921 922 return sw.toString(); 923 } 924 925 926 938 public static String toString(final byte[] input, 939 final String encoding) 940 throws IOException { 941 return toString(input, encoding, DEFAULT_BUFFER_SIZE); 942 } 943 944 945 958 public static String toString(final byte[] input, 959 final String encoding, 960 final int bufferSize) 961 throws IOException { 962 final StringWriter sw = new StringWriter (); 963 copy(input, sw, encoding, bufferSize); 964 965 return sw.toString(); 966 } 967 } 968 | Popular Tags |