KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > io > IOUtil


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.io;
51
52 import java.io.BufferedInputStream JavaDoc;
53 import java.io.BufferedOutputStream JavaDoc;
54 import java.io.ByteArrayInputStream JavaDoc;
55 import java.io.ByteArrayOutputStream JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58 import java.io.InputStreamReader JavaDoc;
59 import java.io.OutputStream JavaDoc;
60 import java.io.OutputStreamWriter JavaDoc;
61 import java.io.Reader JavaDoc;
62 import java.io.StringReader JavaDoc;
63 import java.io.StringWriter JavaDoc;
64 import java.io.Writer JavaDoc;
65
66 /**
67  * General IO Stream manipulation.
68  * <p>
69  * This class provides static utility methods for input/output operations, particularly buffered
70  * copying between sources (<code>InputStream</code>, <code>Reader</code>, <code>String</code> and
71  * <code>byte[]</code>) and destinations (<code>OutputStream</code>, <code>Writer</code>,
72  * <code>String</code> and <code>byte[]</code>).
73  * </p>
74  *
75  * <p>Unless otherwise noted, these <code>copy</code> methods do <em>not</em> flush or close the
76  * streams. Often, doing so would require making non-portable assumptions about the streams' origin
77  * and further use. This means that both streams' <code>close()</code> methods must be called after
78  * copying. if one omits this step, then the stream resources (sockets, file descriptors) are
79  * released when the associated Stream is garbage-collected. It is not a good idea to rely on this
80  * mechanism. For a good overview of the distinction between "memory management" and "resource
81  * management", see <a HREF="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
82  * UnixReview article</a></p>
83  *
84  * <p>For each <code>copy</code> method, a variant is provided that allows the caller to specify the
85  * buffer size (the default is 4k). As the buffer size can have a fairly large impact on speed, this
86  * may be worth tweaking. Often "large buffer -&gt; faster" does not hold, even for large data
87  * transfers.</p>
88  *
89  * <p>For byte-to-char methods, a <code>copy</code> variant allows the encoding to be selected
90  * (otherwise the platform default is used).</p>
91  *
92  * <p>The <code>copy</code> methods use an internal buffer when copying. It is therefore advisable
93  * <em>not</em> to deliberately wrap the stream arguments to the <code>copy</code> methods in
94  * <code>Buffered*</code> streams. For example, don't do the
95  * following:</p>
96  *
97  * <code>copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );</code>
98  *
99  * <p>The rationale is as follows:</p>
100  *
101  * <p>Imagine that an InputStream's read() is a very expensive operation, which would usually suggest
102  * wrapping in a BufferedInputStream. The BufferedInputStream works by issuing infrequent
103  * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the underlying InputStream, to
104  * fill an internal buffer, from which further <code>read</code> requests can inexpensively get
105  * their data (until the buffer runs out).</p>
106  * <p>However, the <code>copy</code> methods do the same thing, keeping an internal buffer,
107  * populated by {@link InputStream#read(byte[] b, int off, int len)} requests. Having two buffers
108  * (or three if the destination stream is also buffered) is pointless, and the unnecessary buffer
109  * management hurts performance slightly (about 3%, according to some simple experiments).</p>
110  *
111  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
112  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
113  * @version CVS $Revision: 1.4 $ $Date: 2003/03/22 12:46:24 $
114  * @since 4.0
115  */

116
117 /*
118  * Behold, intrepid explorers; a map of this class:
119  *
120  * Method Input Output Dependency
121  * ------ ----- ------ -------
122  * 1 copy InputStream OutputStream (primitive)
123  * 2 copy Reader Writer (primitive)
124  *
125  * 3 copy InputStream Writer 2
126  * 4 toString InputStream String 3
127  * 5 toByteArray InputStream byte[] 1
128  *
129  * 6 copy Reader OutputStream 2
130  * 7 toString Reader String 2
131  * 8 toByteArray Reader byte[] 6
132  *
133  * 9 copy String OutputStream 2
134  * 10 copy String Writer (trivial)
135  * 11 toByteArray String byte[] 9
136  *
137  * 12 copy byte[] Writer 3
138  * 13 toString byte[] String 12
139  * 14 copy byte[] OutputStream (trivial)
140  *
141  *
142  * Note that only the first two methods shuffle bytes; the rest use these two, or (if possible) copy
143  * using native Java copy methods. As there are method variants to specify buffer size and encoding,
144  * each row may correspond to up to 4 methods.
145  *
146  */

147
148 public final class IOUtil
149 {
150     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
151
152     /**
153      * Private constructor to prevent instantiation.
154      */

155     private IOUtil()
156     {
157     }
158
159     /**
160      * Unconditionally close an <code>Reader</code>.
161      * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
162      *
163      * @param input A (possibly null) Reader
164      */

165     public static void shutdownReader( final Reader JavaDoc input )
166     {
167         if( null == input )
168         {
169             return;
170         }
171
172         try
173         {
174             input.close();
175         }
176         catch( final IOException JavaDoc ioe )
177         {
178         }
179     }
180
181     /**
182      * Unconditionally close an <code>Writer</code>.
183      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
184      *
185      * @param output A (possibly null) Writer
186      */

187     public static void shutdownWriter( final Writer JavaDoc output )
188     {
189         if( null == output )
190         {
191             return;
192         }
193
194         try
195         {
196             output.close();
197         }
198         catch( final IOException JavaDoc ioe )
199         {
200         }
201     }
202
203     /**
204      * Unconditionally close an <code>OutputStream</code>.
205      * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
206      * @param output A (possibly null) OutputStream
207      */

208     public static void shutdownStream( final OutputStream JavaDoc output )
209     {
210         if( null == output )
211         {
212             return;
213         }
214
215         try
216         {
217             output.close();
218         }
219         catch( final IOException JavaDoc ioe )
220         {
221         }
222     }
223
224     /**
225      * Unconditionally close an <code>InputStream</code>.
226      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
227      * @param input A (possibly null) InputStream
228      */

229     public static void shutdownStream( final InputStream JavaDoc input )
230     {
231         if( null == input )
232         {
233             return;
234         }
235
236         try
237         {
238             input.close();
239         }
240         catch( final IOException JavaDoc ioe )
241         {
242         }
243     }
244
245     ///////////////////////////////////////////////////////////////
246
// Core copy methods
247
///////////////////////////////////////////////////////////////
248

249     /**
250      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
251      */

252     public static void copy( final InputStream JavaDoc input, final OutputStream JavaDoc output )
253         throws IOException JavaDoc
254     {
255         copy( input, output, DEFAULT_BUFFER_SIZE );
256     }
257
258     /**
259      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
260      * @param bufferSize Size of internal buffer to use.
261      */

262     public static void copy( final InputStream JavaDoc input,
263                              final OutputStream JavaDoc output,
264                              final int bufferSize )
265         throws IOException JavaDoc
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     /**
276      * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
277      */

278     public static void copy( final Reader JavaDoc input, final Writer JavaDoc output )
279         throws IOException JavaDoc
280     {
281         copy( input, output, DEFAULT_BUFFER_SIZE );
282     }
283
284     /**
285      * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
286      * @param bufferSize Size of internal buffer to use.
287      */

288     public static void copy( final Reader JavaDoc input, final Writer JavaDoc output, final int bufferSize )
289         throws IOException JavaDoc
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     ///////////////////////////////////////////////////////////////
300
// Derived copy methods
301
// InputStream -> *
302
///////////////////////////////////////////////////////////////
303

304
305     ///////////////////////////////////////////////////////////////
306
// InputStream -> Writer
307

308     /**
309      * Copy and convert bytes from an <code>InputStream</code> to chars on a
310      * <code>Writer</code>.
311      * The platform's default encoding is used for the byte-to-char conversion.
312      */

313     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output )
314         throws IOException JavaDoc
315     {
316         copy( input, output, DEFAULT_BUFFER_SIZE );
317     }
318
319     /**
320      * Copy and convert bytes from an <code>InputStream</code> to chars on a
321      * <code>Writer</code>.
322      * The platform's default encoding is used for the byte-to-char conversion.
323      * @param bufferSize Size of internal buffer to use.
324      */

325     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output, final int bufferSize )
326         throws IOException JavaDoc
327     {
328         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input );
329         copy( in, output, bufferSize );
330     }
331
332     /**
333      * Copy and convert bytes from an <code>InputStream</code> to chars on a
334      * <code>Writer</code>, using the specified encoding.
335      * @param encoding The name of a supported character encoding. See the
336      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
337      * Charset Registry</a> for a list of valid encoding types.
338      */

339     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output, final String JavaDoc encoding )
340         throws IOException JavaDoc
341     {
342         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input, encoding );
343         copy( in, output );
344     }
345
346     /**
347      * Copy and convert bytes from an <code>InputStream</code> to chars on a
348      * <code>Writer</code>, using the specified encoding.
349      * @param encoding The name of a supported character encoding. See the
350      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
351      * Charset Registry</a> for a list of valid encoding types.
352      * @param bufferSize Size of internal buffer to use.
353      */

354     public static void copy( final InputStream JavaDoc input,
355                              final Writer JavaDoc output,
356                              final String JavaDoc encoding,
357                              final int bufferSize )
358         throws IOException JavaDoc
359     {
360         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input, encoding );
361         copy( in, output, bufferSize );
362     }
363
364
365     ///////////////////////////////////////////////////////////////
366
// InputStream -> String
367

368     /**
369      * Get the contents of an <code>InputStream</code> as a String.
370      * The platform's default encoding is used for the byte-to-char conversion.
371      */

372     public static String JavaDoc toString( final InputStream JavaDoc input )
373         throws IOException JavaDoc
374     {
375         return toString( input, DEFAULT_BUFFER_SIZE );
376     }
377
378     /**
379      * Get the contents of an <code>InputStream</code> as a String.
380      * The platform's default encoding is used for the byte-to-char conversion.
381      * @param bufferSize Size of internal buffer to use.
382      */

383     public static String JavaDoc toString( final InputStream JavaDoc input, final int bufferSize )
384         throws IOException JavaDoc
385     {
386         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
387         copy( input, sw, bufferSize );
388         return sw.toString();
389     }
390
391     /**
392      * Get the contents of an <code>InputStream</code> as a String.
393      * @param encoding The name of a supported character encoding. See the
394      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
395      * Charset Registry</a> for a list of valid encoding types.
396      */

397     public static String JavaDoc toString( final InputStream JavaDoc input, final String JavaDoc encoding )
398         throws IOException JavaDoc
399     {
400         return toString( input, encoding, DEFAULT_BUFFER_SIZE );
401     }
402
403     /**
404      * Get the contents of an <code>InputStream</code> as a String.
405      * @param encoding The name of a supported character encoding. See the
406      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
407      * Charset Registry</a> for a list of valid encoding types.
408      * @param bufferSize Size of internal buffer to use.
409      */

410     public static String JavaDoc toString( final InputStream JavaDoc input,
411                                    final String JavaDoc encoding,
412                                    final int bufferSize )
413         throws IOException JavaDoc
414     {
415         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
416         copy( input, sw, encoding, bufferSize );
417         return sw.toString();
418     }
419
420     ///////////////////////////////////////////////////////////////
421
// InputStream -> byte[]
422

423     /**
424      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
425      */

426     public static byte[] toByteArray( final InputStream JavaDoc input )
427         throws IOException JavaDoc
428     {
429         return toByteArray( input, DEFAULT_BUFFER_SIZE );
430     }
431
432     /**
433      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
434      * @param bufferSize Size of internal buffer to use.
435      */

436     public static byte[] toByteArray( final InputStream JavaDoc input, final int bufferSize )
437         throws IOException JavaDoc
438     {
439         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
440         copy( input, output, bufferSize );
441         return output.toByteArray();
442     }
443
444
445     ///////////////////////////////////////////////////////////////
446
// Derived copy methods
447
// Reader -> *
448
///////////////////////////////////////////////////////////////
449

450     ///////////////////////////////////////////////////////////////
451
// Reader -> OutputStream
452
/**
453      * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
454      * flush the <code>OutputStream</code>.
455      */

456     public static void copy( final Reader JavaDoc input, final OutputStream JavaDoc output )
457         throws IOException JavaDoc
458     {
459         copy( input, output, DEFAULT_BUFFER_SIZE );
460     }
461
462     /**
463      * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
464      * flush the <code>OutputStream</code>.
465      * @param bufferSize Size of internal buffer to use.
466      */

467     public static void copy( final Reader JavaDoc input, final OutputStream JavaDoc output, final int bufferSize )
468         throws IOException JavaDoc
469     {
470         final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc( output );
471         copy( input, out, bufferSize );
472         // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
473
// here.
474
out.flush();
475     }
476
477     ///////////////////////////////////////////////////////////////
478
// Reader -> String
479
/**
480      * Get the contents of a <code>Reader</code> as a String.
481      */

482     public static String JavaDoc toString( final Reader JavaDoc input )
483         throws IOException JavaDoc
484     {
485         return toString( input, DEFAULT_BUFFER_SIZE );
486     }
487
488     /**
489      * Get the contents of a <code>Reader</code> as a String.
490      * @param bufferSize Size of internal buffer to use.
491      */

492     public static String JavaDoc toString( final Reader JavaDoc input, final int bufferSize )
493         throws IOException JavaDoc
494     {
495         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
496         copy( input, sw, bufferSize );
497         return sw.toString();
498     }
499
500
501     ///////////////////////////////////////////////////////////////
502
// Reader -> byte[]
503
/**
504      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
505      */

506     public static byte[] toByteArray( final Reader JavaDoc input )
507         throws IOException JavaDoc
508     {
509         return toByteArray( input, DEFAULT_BUFFER_SIZE );
510     }
511
512     /**
513      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
514      * @param bufferSize Size of internal buffer to use.
515      */

516     public static byte[] toByteArray( final Reader JavaDoc input, final int bufferSize )
517         throws IOException JavaDoc
518     {
519         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
520         copy( input, output, bufferSize );
521         return output.toByteArray();
522     }
523
524
525     ///////////////////////////////////////////////////////////////
526
// Derived copy methods
527
// String -> *
528
///////////////////////////////////////////////////////////////
529

530
531     ///////////////////////////////////////////////////////////////
532
// String -> OutputStream
533

534     /**
535      * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
536      * flush the <code>OutputStream</code>.
537      */

538     public static void copy( final String JavaDoc input, final OutputStream JavaDoc output )
539         throws IOException JavaDoc
540     {
541         copy( input, output, DEFAULT_BUFFER_SIZE );
542     }
543
544     /**
545      * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
546      * flush the <code>OutputStream</code>.
547      * @param bufferSize Size of internal buffer to use.
548      */

549     public static void copy( final String JavaDoc input, final OutputStream JavaDoc output, final int bufferSize )
550         throws IOException JavaDoc
551     {
552         final StringReader JavaDoc in = new StringReader JavaDoc( input );
553         final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc( output );
554         copy( in, out, bufferSize );
555         // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
556
// here.
557
out.flush();
558     }
559
560
561
562     ///////////////////////////////////////////////////////////////
563
// String -> Writer
564

565     /**
566      * Copy chars from a <code>String</code> to a <code>Writer</code>.
567      */

568     public static void copy( final String JavaDoc input, final Writer JavaDoc output )
569         throws IOException JavaDoc
570     {
571         output.write( input );
572     }
573
574     /**
575      * Copy bytes from an <code>InputStream</code> to an
576      * <code>OutputStream</code>, with buffering.
577      * This is equivalent to passing a
578      * {@link java.io.BufferedInputStream} and
579      * {@link java.io.BufferedOutputStream} to {@link #copy(InputStream, OutputStream)},
580      * and flushing the output stream afterwards. The streams are not closed
581      * after the copy.
582      * @deprecated Buffering streams is actively harmful! See the class description as to why. Use
583      * {@link #copy(InputStream, OutputStream)} instead.
584      */

585     public static void bufferedCopy( final InputStream JavaDoc input, final OutputStream JavaDoc output )
586         throws IOException JavaDoc
587     {
588         final BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc( input );
589         final BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc( output );
590         copy( in, out );
591         out.flush();
592     }
593
594
595     ///////////////////////////////////////////////////////////////
596
// String -> byte[]
597
/**
598      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
599      */

600     public static byte[] toByteArray( final String JavaDoc input )
601         throws IOException JavaDoc
602     {
603         return toByteArray( input, DEFAULT_BUFFER_SIZE );
604     }
605
606     /**
607      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
608      * @param bufferSize Size of internal buffer to use.
609      */

610     public static byte[] toByteArray( final String JavaDoc input, final int bufferSize )
611         throws IOException JavaDoc
612     {
613         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
614         copy( input, output, bufferSize );
615         return output.toByteArray();
616     }
617
618
619
620     ///////////////////////////////////////////////////////////////
621
// Derived copy methods
622
// byte[] -> *
623
///////////////////////////////////////////////////////////////
624

625
626     ///////////////////////////////////////////////////////////////
627
// byte[] -> Writer
628

629     /**
630      * Copy and convert bytes from a <code>byte[]</code> to chars on a
631      * <code>Writer</code>.
632      * The platform's default encoding is used for the byte-to-char conversion.
633      */

634     public static void copy( final byte[] input, final Writer JavaDoc output )
635         throws IOException JavaDoc
636     {
637         copy( input, output, DEFAULT_BUFFER_SIZE );
638     }
639
640     /**
641      * Copy and convert bytes from a <code>byte[]</code> to chars on a
642      * <code>Writer</code>.
643      * The platform's default encoding is used for the byte-to-char conversion.
644      * @param bufferSize Size of internal buffer to use.
645      */

646     public static void copy( final byte[] input, final Writer JavaDoc output, final int bufferSize )
647         throws IOException JavaDoc
648     {
649         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
650         copy( in, output, bufferSize );
651     }
652
653     /**
654      * Copy and convert bytes from a <code>byte[]</code> to chars on a
655      * <code>Writer</code>, using the specified encoding.
656      * @param encoding The name of a supported character encoding. See the
657      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
658      * Charset Registry</a> for a list of valid encoding types.
659      */

660     public static void copy( final byte[] input, final Writer JavaDoc output, final String JavaDoc encoding )
661         throws IOException JavaDoc
662     {
663         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
664         copy( in, output, encoding );
665     }
666
667     /**
668      * Copy and convert bytes from a <code>byte[]</code> to chars on a
669      * <code>Writer</code>, using the specified encoding.
670      * @param encoding The name of a supported character encoding. See the
671      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
672      * Charset Registry</a> for a list of valid encoding types.
673      * @param bufferSize Size of internal buffer to use.
674      */

675     public static void copy( final byte[] input,
676                              final Writer JavaDoc output,
677                              final String JavaDoc encoding,
678                              final int bufferSize )
679         throws IOException JavaDoc
680     {
681         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
682         copy( in, output, encoding, bufferSize );
683     }
684
685
686     ///////////////////////////////////////////////////////////////
687
// byte[] -> String
688

689     /**
690      * Get the contents of a <code>byte[]</code> as a String.
691      * The platform's default encoding is used for the byte-to-char conversion.
692      */

693     public static String JavaDoc toString( final byte[] input )
694         throws IOException JavaDoc
695     {
696         return toString( input, DEFAULT_BUFFER_SIZE );
697     }
698
699     /**
700      * Get the contents of a <code>byte[]</code> as a String.
701      * The platform's default encoding is used for the byte-to-char conversion.
702      * @param bufferSize Size of internal buffer to use.
703      */

704     public static String JavaDoc toString( final byte[] input, final int bufferSize )
705         throws IOException JavaDoc
706     {
707         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
708         copy( input, sw, bufferSize );
709         return sw.toString();
710     }
711
712     /**
713      * Get the contents of a <code>byte[]</code> as a String.
714      * @param encoding The name of a supported character encoding. See the
715      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
716      * Charset Registry</a> for a list of valid encoding types.
717      */

718     public static String JavaDoc toString( final byte[] input, final String JavaDoc encoding )
719         throws IOException JavaDoc
720     {
721         return toString( input, encoding, DEFAULT_BUFFER_SIZE );
722     }
723
724     /**
725      * Get the contents of a <code>byte[]</code> as a String.
726      * @param encoding The name of a supported character encoding. See the
727      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
728      * Charset Registry</a> for a list of valid encoding types.
729      * @param bufferSize Size of internal buffer to use.
730      */

731     public static String JavaDoc toString( final byte[] input,
732                                    final String JavaDoc encoding,
733                                    final int bufferSize )
734         throws IOException JavaDoc
735     {
736         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
737         copy( input, sw, encoding, bufferSize );
738         return sw.toString();
739     }
740
741
742     ///////////////////////////////////////////////////////////////
743
// byte[] -> OutputStream
744

745     /**
746      * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
747      */

748     public static void copy( final byte[] input, final OutputStream JavaDoc output )
749         throws IOException JavaDoc
750     {
751         copy( input, output, DEFAULT_BUFFER_SIZE );
752     }
753
754     /**
755      * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
756      * @param bufferSize Size of internal buffer to use.
757      */

758     public static void copy( final byte[] input,
759                              final OutputStream JavaDoc output,
760                              final int bufferSize )
761         throws IOException JavaDoc
762     {
763         output.write( input );
764     }
765
766     /**
767      * Compare the contents of two Streams to determine if they are equal or not.
768      *
769      * @param input1 the first stream
770      * @param input2 the second stream
771      * @return true if the content of the streams are equal or they both don't exist, false otherwise
772      */

773     public static boolean contentEquals( final InputStream JavaDoc input1,
774                                          final InputStream JavaDoc input2 )
775         throws IOException JavaDoc
776     {
777         final InputStream JavaDoc bufferedInput1 = new BufferedInputStream JavaDoc( input1 );
778         final InputStream JavaDoc bufferedInput2 = new BufferedInputStream JavaDoc( 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