KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > external > IOUtil


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/external/IOUtil.java,v 1.5 2004/08/18 12:26:09 hkollmann Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/18 12:26:09 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 /*
25  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/external/IOUtil.java,v 1.5 2004/08/18 12:26:09 hkollmann Exp $
26  * $Revision: 1.5 $
27  * $Date: 2004/08/18 12:26:09 $
28  *
29  * IOUtil class taken from jakarta-commons-sandbox project.
30  *
31  *
32 */

33 package org.dbforms.util.external;
34
35
36 /* ====================================================================
37  * The Apache Software License, Version 1.1
38  *
39  * Copyright (c) 2001 The Apache Software Foundation. All rights
40  * reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  *
46  * 1. Redistributions of source code must retain the above copyright
47  * notice, this list of conditions and the following disclaimer.
48  *
49  * 2. Redistributions in binary form must reproduce the above copyright
50  * notice, this list of conditions and the following disclaimer in
51  * the documentation and/or other materials provided with the
52  * distribution.
53  *
54  * 3. The end-user documentation included with the redistribution,
55  * if any, must include the following acknowledgment:
56  * "This product includes software developed by the
57  * Apache Software Foundation (http://www.apache.org/)."
58  * Alternately, this acknowledgment may appear in the software itself,
59  * if and wherever such third-party acknowledgments normally appear.
60  *
61  * 4. The names "Apache" and "Apache Software Foundation" and
62  * "Apache Turbine" must not be used to endorse or promote products
63  * derived from this software without prior written permission. For
64  * written permission, please contact apache@apache.org.
65  *
66  * 5. Products derived from this software may not be called "Apache",
67  * "Apache Turbine", nor may "Apache" appear in their name, without
68  * prior written permission of the Apache Software Foundation.
69  *
70  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
71  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
72  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
74  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
76  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
77  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
78  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
79  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
80  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81  * SUCH DAMAGE.
82  * ====================================================================
83  *
84  * This software consists of voluntary contributions made by many
85  * individuals on behalf of the Apache Software Foundation. For more
86  * information on the Apache Software Foundation, please see
87  * <http://www.apache.org/>.
88  */

89 import java.io.BufferedInputStream JavaDoc;
90 import java.io.BufferedOutputStream JavaDoc;
91 import java.io.ByteArrayInputStream JavaDoc;
92 import java.io.ByteArrayOutputStream JavaDoc;
93 import java.io.IOException JavaDoc;
94 import java.io.InputStream JavaDoc;
95 import java.io.InputStreamReader JavaDoc;
96 import java.io.OutputStream JavaDoc;
97 import java.io.OutputStreamWriter JavaDoc;
98 import java.io.Reader JavaDoc;
99 import java.io.StringReader JavaDoc;
100 import java.io.StringWriter JavaDoc;
101 import java.io.Writer JavaDoc;
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 JavaDoc input,
138                                    final OutputStream JavaDoc output)
139                             throws IOException JavaDoc {
140       final BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(input);
141       final BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(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 JavaDoc input1,
160                                        final InputStream JavaDoc input2)
161                                 throws IOException JavaDoc {
162       final InputStream JavaDoc bufferedInput1 = new BufferedInputStream JavaDoc(input1);
163       final InputStream JavaDoc bufferedInput2 = new BufferedInputStream JavaDoc(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
// Core copy methods
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 JavaDoc input,
201                            final OutputStream JavaDoc output) throws IOException JavaDoc {
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 JavaDoc input,
217                            final OutputStream JavaDoc output,
218                            final int bufferSize)
219                     throws IOException JavaDoc {
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 JavaDoc input,
238                            final Writer JavaDoc output) throws IOException JavaDoc {
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 JavaDoc input,
253                            final Writer JavaDoc output,
254                            final int bufferSize) throws IOException JavaDoc {
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
// Derived copy methods
266
// InputStream -> *
267
///////////////////////////////////////////////////////////////
268
///////////////////////////////////////////////////////////////
269
// InputStream -> Writer
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 JavaDoc input,
282                            final Writer JavaDoc output) throws IOException JavaDoc {
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 JavaDoc input,
299                            final Writer JavaDoc output,
300                            final int bufferSize)
301                     throws IOException JavaDoc {
302       final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(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 JavaDoc input,
320                            final Writer JavaDoc output,
321                            final String JavaDoc encoding)
322                     throws IOException JavaDoc {
323       final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(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 JavaDoc input,
342                            final Writer JavaDoc output,
343                            final String JavaDoc encoding,
344                            final int bufferSize)
345                     throws IOException JavaDoc {
346       final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(input, encoding);
347       copy(in, output, bufferSize);
348    }
349
350
351    ///////////////////////////////////////////////////////////////
352
// Derived copy methods
353
// Reader -> *
354
///////////////////////////////////////////////////////////////
355
///////////////////////////////////////////////////////////////
356
// Reader -> OutputStream
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 JavaDoc input,
368                            final OutputStream JavaDoc output) throws IOException JavaDoc {
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 JavaDoc input,
384                            final OutputStream JavaDoc output,
385                            final int bufferSize)
386                     throws IOException JavaDoc {
387       final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc(output);
388       copy(input, out, bufferSize);
389
390       // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
391
// here.
392
out.flush();
393    }
394
395
396    ///////////////////////////////////////////////////////////////
397
// Derived copy methods
398
// String -> *
399
///////////////////////////////////////////////////////////////
400
///////////////////////////////////////////////////////////////
401
// String -> OutputStream
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 JavaDoc input,
413                            final OutputStream JavaDoc output) throws IOException JavaDoc {
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 JavaDoc input,
429                            final OutputStream JavaDoc output,
430                            final int bufferSize)
431                     throws IOException JavaDoc {
432       final StringReader JavaDoc in = new StringReader JavaDoc(input);
433       final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc(output);
434       copy(in, out, bufferSize);
435
436       // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
437
// here.
438
out.flush();
439    }
440
441
442    ///////////////////////////////////////////////////////////////
443
// String -> Writer
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 JavaDoc input,
454                            final Writer JavaDoc output) throws IOException JavaDoc {
455       output.write(input);
456    }
457
458
459    ///////////////////////////////////////////////////////////////
460
// Derived copy methods
461
// byte[] -> *
462
///////////////////////////////////////////////////////////////
463
///////////////////////////////////////////////////////////////
464
// byte[] -> Writer
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 JavaDoc output) throws IOException JavaDoc {
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 JavaDoc output,
495                            final int bufferSize) throws IOException JavaDoc {
496       final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(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 JavaDoc output,
515                            final String JavaDoc encoding) throws IOException JavaDoc {
516       final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(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 JavaDoc output,
536                            final String JavaDoc encoding,
537                            final int bufferSize) throws IOException JavaDoc {
538       final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(input);
539       copy(in, output, encoding, bufferSize);
540    }
541
542
543    ///////////////////////////////////////////////////////////////
544
// byte[] -> OutputStream
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 JavaDoc output) throws IOException JavaDoc {
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 JavaDoc output,
571                            final int bufferSize)
572                     throws IOException JavaDoc {
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 JavaDoc input) {
584       if (input == null) {
585          return;
586       }
587
588       try {
589          input.close();
590       } catch (final IOException JavaDoc 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 JavaDoc output) {
603       if (output == null) {
604          return;
605       }
606
607       try {
608          output.close();
609       } catch (final IOException JavaDoc 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 JavaDoc input) {
622       if (input == null) {
623          return;
624       }
625
626       try {
627          input.close();
628       } catch (final IOException JavaDoc 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 JavaDoc output) {
641       if (output == null) {
642          return;
643       }
644
645       try {
646          output.close();
647       } catch (final IOException JavaDoc ioe) {
648          ;
649       }
650    }
651
652
653    ///////////////////////////////////////////////////////////////
654
// InputStream -> byte[]
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 JavaDoc input)
666                              throws IOException JavaDoc {
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 JavaDoc input,
682                                     final int bufferSize)
683                              throws IOException JavaDoc {
684       final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
685       copy(input, output, bufferSize);
686
687       return output.toByteArray();
688    }
689
690
691    ///////////////////////////////////////////////////////////////
692
// Reader -> byte[]
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 JavaDoc input)
704                              throws IOException JavaDoc {
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 JavaDoc input,
720                                     final int bufferSize)
721                              throws IOException JavaDoc {
722       ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
723       copy(input, output, bufferSize);
724
725       return output.toByteArray();
726    }
727
728
729    ///////////////////////////////////////////////////////////////
730
// String -> byte[]
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 JavaDoc input)
742                              throws IOException JavaDoc {
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 JavaDoc input,
758                                     final int bufferSize)
759                              throws IOException JavaDoc {
760       ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
761       copy(input, output, bufferSize);
762
763       return output.toByteArray();
764    }
765
766
767    ///////////////////////////////////////////////////////////////
768
// InputStream -> String
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 JavaDoc toString(final InputStream JavaDoc input)
781                           throws IOException JavaDoc {
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 JavaDoc toString(final InputStream JavaDoc input,
798                                  final int bufferSize)
799                           throws IOException JavaDoc {
800       final StringWriter JavaDoc sw = new StringWriter JavaDoc();
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 JavaDoc toString(final InputStream JavaDoc input,
820                                  final String JavaDoc encoding)
821                           throws IOException JavaDoc {
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 JavaDoc toString(final InputStream JavaDoc input,
840                                  final String JavaDoc encoding,
841                                  final int bufferSize)
842                           throws IOException JavaDoc {
843       final StringWriter JavaDoc sw = new StringWriter JavaDoc();
844       copy(input, sw, encoding, bufferSize);
845
846       return sw.toString();
847    }
848
849
850    ///////////////////////////////////////////////////////////////
851
// Reader -> String
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 JavaDoc toString(final Reader JavaDoc input) throws IOException JavaDoc {
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 JavaDoc toString(final Reader JavaDoc input,
878                                  final int bufferSize)
879                           throws IOException JavaDoc {
880       final StringWriter JavaDoc sw = new StringWriter JavaDoc();
881       copy(input, sw, bufferSize);
882
883       return sw.toString();
884    }
885
886
887    ///////////////////////////////////////////////////////////////
888
// byte[] -> String
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 JavaDoc toString(final byte[] input) throws IOException JavaDoc {
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 JavaDoc toString(final byte[] input,
917                                  final int bufferSize)
918                           throws IOException JavaDoc {
919       final StringWriter JavaDoc sw = new StringWriter JavaDoc();
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 JavaDoc toString(final byte[] input,
939                                  final String JavaDoc encoding)
940                           throws IOException JavaDoc {
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 JavaDoc toString(final byte[] input,
959                                  final String JavaDoc encoding,
960                                  final int bufferSize)
961                           throws IOException JavaDoc {
962       final StringWriter JavaDoc sw = new StringWriter JavaDoc();
963       copy(input, sw, encoding, bufferSize);
964
965       return sw.toString();
966    }
967 }
968
Popular Tags