1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.dbforms.util.external;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 /***
106 * DOCUMENT ME!
107 *
108 * @author $author$
109 * @version $Revision: 1.5 $
110 */
111 public final class IOUtil {
112 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
113
114 /***
115 * Private constructor to prevent instantiation.
116 */
117 private IOUtil() {
118 }
119
120 /***
121 * Copy bytes from an <code>InputStream</code> to an
122 * <code>OutputStream</code>, with buffering. This is equivalent to passing
123 * a {@link java.io.BufferedInputStream} and {@link
124 * java.io.BufferedOutputStream} to {@link #copy(InputStream,
125 * OutputStream)}, and flushing the output stream afterwards. The streams
126 * are not closed after the copy.
127 *
128 * @param input DOCUMENT ME!
129 * @param output DOCUMENT ME!
130 *
131 * @throws IOException DOCUMENT ME!
132 *
133 * @deprecated Buffering streams is actively harmful! See the class
134 * description as to why. Use {@link #copy(InputStream,
135 * OutputStream)} instead.
136 */
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 /***
148 * Compare the contents of two Streams to determine if they are equal or
149 * not.
150 *
151 * @param input1 the first stream
152 * @param input2 the second stream
153 *
154 * @return true if the content of the streams are equal or they both don't
155 * exist, false otherwise
156 *
157 * @throws IOException DOCUMENT ME!
158 */
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
188
189
190
191 /***
192 * Copy bytes from an <code>InputStream</code> to an
193 * <code>OutputStream</code>.
194 *
195 * @param input DOCUMENT ME!
196 * @param output DOCUMENT ME!
197 *
198 * @throws IOException DOCUMENT ME!
199 */
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 /***
207 * Copy bytes from an <code>InputStream</code> to an
208 * <code>OutputStream</code>.
209 *
210 * @param input DOCUMENT ME!
211 * @param output DOCUMENT ME!
212 * @param bufferSize Size of internal buffer to use.
213 *
214 * @throws IOException DOCUMENT ME!
215 */
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 /***
230 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
231 *
232 * @param input DOCUMENT ME!
233 * @param output DOCUMENT ME!
234 *
235 * @throws IOException DOCUMENT ME!
236 */
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 /***
244 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
245 *
246 * @param input DOCUMENT ME!
247 * @param output DOCUMENT ME!
248 * @param bufferSize Size of internal buffer to use.
249 *
250 * @throws IOException DOCUMENT ME!
251 */
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
265
266
267
268
269
270
271 /***
272 * Copy and convert bytes from an <code>InputStream</code> to chars on a
273 * <code>Writer</code>. The platform's default encoding is used for the
274 * byte-to-char conversion.
275 *
276 * @param input DOCUMENT ME!
277 * @param output DOCUMENT ME!
278 *
279 * @throws IOException DOCUMENT ME!
280 */
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 /***
288 * Copy and convert bytes from an <code>InputStream</code> to chars on a
289 * <code>Writer</code>. The platform's default encoding is used for the
290 * byte-to-char conversion.
291 *
292 * @param input DOCUMENT ME!
293 * @param output DOCUMENT ME!
294 * @param bufferSize Size of internal buffer to use.
295 *
296 * @throws IOException DOCUMENT ME!
297 */
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 /***
308 * Copy and convert bytes from an <code>InputStream</code> to chars on a
309 * <code>Writer</code>, using the specified encoding.
310 *
311 * @param input DOCUMENT ME!
312 * @param output DOCUMENT ME!
313 * @param encoding The name of a supported character encoding. See the <a
314 * href="http://www.iana.org/assignments/character-sets">IANA
315 * Charset Registry</a> for a list of valid encoding types.
316 *
317 * @throws IOException DOCUMENT ME!
318 */
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 /***
329 * Copy and convert bytes from an <code>InputStream</code> to chars on a
330 * <code>Writer</code>, using the specified encoding.
331 *
332 * @param input DOCUMENT ME!
333 * @param output DOCUMENT ME!
334 * @param encoding The name of a supported character encoding. See the <a
335 * href="http://www.iana.org/assignments/character-sets">IANA
336 * Charset Registry</a> for a list of valid encoding types.
337 * @param bufferSize Size of internal buffer to use.
338 *
339 * @throws IOException DOCUMENT ME!
340 */
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
352
353
354
355
356
357
358 /***
359 * Serialize chars from a <code>Reader</code> to bytes on an
360 * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
361 *
362 * @param input DOCUMENT ME!
363 * @param output DOCUMENT ME!
364 *
365 * @throws IOException DOCUMENT ME!
366 */
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 /***
374 * Serialize chars from a <code>Reader</code> to bytes on an
375 * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
376 *
377 * @param input DOCUMENT ME!
378 * @param output DOCUMENT ME!
379 * @param bufferSize Size of internal buffer to use.
380 *
381 * @throws IOException DOCUMENT ME!
382 */
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
391
392 out.flush();
393 }
394
395
396
397
398
399
400
401
402
403 /***
404 * Serialize chars from a <code>String</code> to bytes on an
405 * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
406 *
407 * @param input DOCUMENT ME!
408 * @param output DOCUMENT ME!
409 *
410 * @throws IOException DOCUMENT ME!
411 */
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 /***
419 * Serialize chars from a <code>String</code> to bytes on an
420 * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
421 *
422 * @param input DOCUMENT ME!
423 * @param output DOCUMENT ME!
424 * @param bufferSize Size of internal buffer to use.
425 *
426 * @throws IOException DOCUMENT ME!
427 */
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
437
438 out.flush();
439 }
440
441
442
443
444
445 /***
446 * Copy chars from a <code>String</code> to a <code>Writer</code>.
447 *
448 * @param input DOCUMENT ME!
449 * @param output DOCUMENT ME!
450 *
451 * @throws IOException DOCUMENT ME!
452 */
453 public static void copy(final String input,
454 final Writer output) throws IOException {
455 output.write(input);
456 }
457
458
459
460
461
462
463
464
465
466 /***
467 * Copy and convert bytes from a <code>byte[]</code> to chars on a
468 * <code>Writer</code>. The platform's default encoding is used for the
469 * byte-to-char conversion.
470 *
471 * @param input DOCUMENT ME!
472 * @param output DOCUMENT ME!
473 *
474 * @throws IOException DOCUMENT ME!
475 */
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 /***
483 * Copy and convert bytes from a <code>byte[]</code> to chars on a
484 * <code>Writer</code>. The platform's default encoding is used for the
485 * byte-to-char conversion.
486 *
487 * @param input DOCUMENT ME!
488 * @param output DOCUMENT ME!
489 * @param bufferSize Size of internal buffer to use.
490 *
491 * @throws IOException DOCUMENT ME!
492 */
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 /***
502 * Copy and convert bytes from a <code>byte[]</code> to chars on a
503 * <code>Writer</code>, using the specified encoding.
504 *
505 * @param input DOCUMENT ME!
506 * @param output DOCUMENT ME!
507 * @param encoding The name of a supported character encoding. See the <a
508 * href="http://www.iana.org/assignments/character-sets">IANA
509 * Charset Registry</a> for a list of valid encoding types.
510 *
511 * @throws IOException DOCUMENT ME!
512 */
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 /***
522 * Copy and convert bytes from a <code>byte[]</code> to chars on a
523 * <code>Writer</code>, using the specified encoding.
524 *
525 * @param input DOCUMENT ME!
526 * @param output DOCUMENT ME!
527 * @param encoding The name of a supported character encoding. See the <a
528 * href="http://www.iana.org/assignments/character-sets">IANA
529 * Charset Registry</a> for a list of valid encoding types.
530 * @param bufferSize Size of internal buffer to use.
531 *
532 * @throws IOException DOCUMENT ME!
533 */
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
544
545
546 /***
547 * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
548 *
549 * @param input DOCUMENT ME!
550 * @param output DOCUMENT ME!
551 *
552 * @throws IOException DOCUMENT ME!
553 */
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 /***
561 * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
562 *
563 * @param input DOCUMENT ME!
564 * @param output DOCUMENT ME!
565 * @param bufferSize Size of internal buffer to use.
566 *
567 * @throws IOException DOCUMENT ME!
568 */
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 /***
578 * Unconditionally close an <code>Reader</code>. Equivalent to {@link
579 * Reader#close()}, except any exceptions will be ignored.
580 *
581 * @param input A (possibly null) Reader
582 */
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 /***
597 * Unconditionally close an <code>OutputStream</code>. Equivalent to {@link
598 * OutputStream#close()}, except any exceptions will be ignored.
599 *
600 * @param output A (possibly null) OutputStream
601 */
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 /***
616 * Unconditionally close an <code>InputStream</code>. Equivalent to {@link
617 * InputStream#close()}, except any exceptions will be ignored.
618 *
619 * @param input A (possibly null) InputStream
620 */
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 /***
635 * Unconditionally close an <code>Writer</code>. Equivalent to {@link
636 * Writer#close()}, except any exceptions will be ignored.
637 *
638 * @param output A (possibly null) Writer
639 */
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
654
655
656 /***
657 * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
658 *
659 * @param input DOCUMENT ME!
660 *
661 * @return DOCUMENT ME!
662 *
663 * @throws IOException DOCUMENT ME!
664 */
665 public static byte[] toByteArray(final InputStream input)
666 throws IOException {
667 return toByteArray(input, DEFAULT_BUFFER_SIZE);
668 }
669
670
671 /***
672 * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
673 *
674 * @param input DOCUMENT ME!
675 * @param bufferSize Size of internal buffer to use.
676 *
677 * @return DOCUMENT ME!
678 *
679 * @throws IOException DOCUMENT ME!
680 */
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
692
693
694 /***
695 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
696 *
697 * @param input DOCUMENT ME!
698 *
699 * @return DOCUMENT ME!
700 *
701 * @throws IOException DOCUMENT ME!
702 */
703 public static byte[] toByteArray(final Reader input)
704 throws IOException {
705 return toByteArray(input, DEFAULT_BUFFER_SIZE);
706 }
707
708
709 /***
710 * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
711 *
712 * @param input DOCUMENT ME!
713 * @param bufferSize Size of internal buffer to use.
714 *
715 * @return DOCUMENT ME!
716 *
717 * @throws IOException DOCUMENT ME!
718 */
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
730
731
732 /***
733 * Get the contents of a <code>String</code> as a <code>byte[]</code>.
734 *
735 * @param input DOCUMENT ME!
736 *
737 * @return DOCUMENT ME!
738 *
739 * @throws IOException DOCUMENT ME!
740 */
741 public static byte[] toByteArray(final String input)
742 throws IOException {
743 return toByteArray(input, DEFAULT_BUFFER_SIZE);
744 }
745
746
747 /***
748 * Get the contents of a <code>String</code> as a <code>byte[]</code>.
749 *
750 * @param input DOCUMENT ME!
751 * @param bufferSize Size of internal buffer to use.
752 *
753 * @return DOCUMENT ME!
754 *
755 * @throws IOException DOCUMENT ME!
756 */
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
768
769
770 /***
771 * Get the contents of an <code>InputStream</code> as a String. The
772 * platform's default encoding is used for the byte-to-char conversion.
773 *
774 * @param input DOCUMENT ME!
775 *
776 * @return DOCUMENT ME!
777 *
778 * @throws IOException DOCUMENT ME!
779 */
780 public static String toString(final InputStream input)
781 throws IOException {
782 return toString(input, DEFAULT_BUFFER_SIZE);
783 }
784
785
786 /***
787 * Get the contents of an <code>InputStream</code> as a String. The
788 * platform's default encoding is used for the byte-to-char conversion.
789 *
790 * @param input DOCUMENT ME!
791 * @param bufferSize Size of internal buffer to use.
792 *
793 * @return DOCUMENT ME!
794 *
795 * @throws IOException DOCUMENT ME!
796 */
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 /***
808 * Get the contents of an <code>InputStream</code> as a String.
809 *
810 * @param input DOCUMENT ME!
811 * @param encoding The name of a supported character encoding. See the <a
812 * href="http://www.iana.org/assignments/character-sets">IANA
813 * Charset Registry</a> for a list of valid encoding types.
814 *
815 * @return DOCUMENT ME!
816 *
817 * @throws IOException DOCUMENT ME!
818 */
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 /***
827 * Get the contents of an <code>InputStream</code> as a String.
828 *
829 * @param input DOCUMENT ME!
830 * @param encoding The name of a supported character encoding. See the <a
831 * href="http://www.iana.org/assignments/character-sets">IANA
832 * Charset Registry</a> for a list of valid encoding types.
833 * @param bufferSize Size of internal buffer to use.
834 *
835 * @return DOCUMENT ME!
836 *
837 * @throws IOException DOCUMENT ME!
838 */
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
851
852
853 /***
854 * Get the contents of a <code>Reader</code> as a String.
855 *
856 * @param input DOCUMENT ME!
857 *
858 * @return DOCUMENT ME!
859 *
860 * @throws IOException DOCUMENT ME!
861 */
862 public static String toString(final Reader input) throws IOException {
863 return toString(input, DEFAULT_BUFFER_SIZE);
864 }
865
866
867 /***
868 * Get the contents of a <code>Reader</code> as a String.
869 *
870 * @param input DOCUMENT ME!
871 * @param bufferSize Size of internal buffer to use.
872 *
873 * @return DOCUMENT ME!
874 *
875 * @throws IOException DOCUMENT ME!
876 */
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
888
889
890 /***
891 * Get the contents of a <code>byte[]</code> as a String. The platform's
892 * default encoding is used for the byte-to-char conversion.
893 *
894 * @param input DOCUMENT ME!
895 *
896 * @return DOCUMENT ME!
897 *
898 * @throws IOException DOCUMENT ME!
899 */
900 public static String toString(final byte[] input) throws IOException {
901 return toString(input, DEFAULT_BUFFER_SIZE);
902 }
903
904
905 /***
906 * Get the contents of a <code>byte[]</code> as a String. The platform's
907 * default encoding is used for the byte-to-char conversion.
908 *
909 * @param input DOCUMENT ME!
910 * @param bufferSize Size of internal buffer to use.
911 *
912 * @return DOCUMENT ME!
913 *
914 * @throws IOException DOCUMENT ME!
915 */
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 /***
927 * Get the contents of a <code>byte[]</code> as a String.
928 *
929 * @param input DOCUMENT ME!
930 * @param encoding The name of a supported character encoding. See the <a
931 * href="http://www.iana.org/assignments/character-sets">IANA
932 * Charset Registry</a> for a list of valid encoding types.
933 *
934 * @return DOCUMENT ME!
935 *
936 * @throws IOException DOCUMENT ME!
937 */
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 /***
946 * Get the contents of a <code>byte[]</code> as a String.
947 *
948 * @param input DOCUMENT ME!
949 * @param encoding The name of a supported character encoding. See the <a
950 * href="http://www.iana.org/assignments/character-sets">IANA
951 * Charset Registry</a> for a list of valid encoding types.
952 * @param bufferSize Size of internal buffer to use.
953 *
954 * @return DOCUMENT ME!
955 *
956 * @throws IOException DOCUMENT ME!
957 */
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 }