KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > IOUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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

84
85 /*
86  * Behold, intrepid explorers; a map of this class:
87  *
88  * Method Input Output Dependency
89  * ------ ----- ------ -------
90  * 1 copy InputStream OutputStream (primitive)
91  * 2 copy Reader Writer (primitive)
92  *
93  * 3 copy InputStream Writer 2
94  * 4 toString InputStream String 3
95  * 5 toByteArray InputStream byte[] 1
96  *
97  * 6 copy Reader OutputStream 2
98  * 7 toString Reader String 2
99  * 8 toByteArray Reader byte[] 6
100  *
101  * 9 copy String OutputStream 2
102  * 10 copy String Writer (trivial)
103  * 11 toByteArray String byte[] 9
104  *
105  * 12 copy byte[] Writer 3
106  * 13 toString byte[] String 12
107  * 14 copy byte[] OutputStream (trivial)
108  *
109  *
110  * Note that only the first two methods shuffle bytes; the rest use these two, or (if possible) copy
111  * using native Java copy methods. As there are method variants to specify buffer size and encoding,
112  * each row may correspond to up to 4 methods.
113  *
114  */

115
116 public final class IOUtil
117 {
118     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
119
120     /**
121      * Private constructor to prevent instantiation.
122      */

123     private IOUtil()
124     {
125     }
126
127     /**
128      * Unconditionally close an <code>Reader</code>.
129      * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
130      *
131      * @param input A (possibly null) Reader
132      */

133     public static void shutdownReader( final Reader JavaDoc input )
134     {
135         if( null == input )
136         {
137             return;
138         }
139
140         try
141         {
142             input.close();
143         }
144         catch( final IOException JavaDoc ioe )
145         {
146         }
147     }
148
149     /**
150      * Unconditionally close an <code>Writer</code>.
151      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
152      *
153      * @param output A (possibly null) Writer
154      */

155     public static void shutdownWriter( final Writer JavaDoc output )
156     {
157         if( null == output )
158         {
159             return;
160         }
161
162         try
163         {
164             output.close();
165         }
166         catch( final IOException JavaDoc ioe )
167         {
168         }
169     }
170
171     /**
172      * Unconditionally close an <code>OutputStream</code>.
173      * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
174      * @param output A (possibly null) OutputStream
175      */

176     public static void shutdownStream( final OutputStream JavaDoc output )
177     {
178         if( null == output )
179         {
180             return;
181         }
182
183         try
184         {
185             output.close();
186         }
187         catch( final IOException JavaDoc ioe )
188         {
189         }
190     }
191
192     /**
193      * Unconditionally close an <code>InputStream</code>.
194      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
195      * @param input A (possibly null) InputStream
196      */

197     public static void shutdownStream( final InputStream JavaDoc input )
198     {
199         if( null == input )
200         {
201             return;
202         }
203
204         try
205         {
206             input.close();
207         }
208         catch( final IOException JavaDoc ioe )
209         {
210         }
211     }
212
213     ///////////////////////////////////////////////////////////////
214
// Core copy methods
215
///////////////////////////////////////////////////////////////
216

217     /**
218      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
219      */

220     public static void copy( final InputStream JavaDoc input, final OutputStream JavaDoc output )
221         throws IOException JavaDoc
222     {
223         copy( input, output, DEFAULT_BUFFER_SIZE );
224     }
225
226     /**
227      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
228      * @param bufferSize Size of internal buffer to use.
229      */

230     public static void copy( final InputStream JavaDoc input,
231                              final OutputStream JavaDoc output,
232                              final int bufferSize )
233         throws IOException JavaDoc
234     {
235         final byte[] buffer = new byte[ bufferSize ];
236         int n = 0;
237         while( -1 != ( n = input.read( buffer ) ) )
238         {
239             output.write( buffer, 0, n );
240         }
241     }
242
243     /**
244      * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
245      */

246     public static void copy( final Reader JavaDoc input, final Writer JavaDoc output )
247         throws IOException JavaDoc
248     {
249         copy( input, output, DEFAULT_BUFFER_SIZE );
250     }
251
252     /**
253      * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
254      * @param bufferSize Size of internal buffer to use.
255      */

256     public static void copy( final Reader JavaDoc input, final Writer JavaDoc output, final int bufferSize )
257         throws IOException JavaDoc
258     {
259         final char[] buffer = new char[ bufferSize ];
260         int n = 0;
261         while( -1 != ( n = input.read( buffer ) ) )
262         {
263             output.write( buffer, 0, n );
264         }
265     }
266
267     ///////////////////////////////////////////////////////////////
268
// Derived copy methods
269
// InputStream -> *
270
///////////////////////////////////////////////////////////////
271

272
273     ///////////////////////////////////////////////////////////////
274
// InputStream -> Writer
275

276     /**
277      * Copy and convert bytes from an <code>InputStream</code> to chars on a
278      * <code>Writer</code>.
279      * The platform's default encoding is used for the byte-to-char conversion.
280      */

281     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output )
282         throws IOException JavaDoc
283     {
284         copy( input, output, DEFAULT_BUFFER_SIZE );
285     }
286
287     /**
288      * Copy and convert bytes from an <code>InputStream</code> to chars on a
289      * <code>Writer</code>.
290      * The platform's default encoding is used for the byte-to-char conversion.
291      * @param bufferSize Size of internal buffer to use.
292      */

293     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output, final int bufferSize )
294         throws IOException JavaDoc
295     {
296         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input );
297         copy( in, output, bufferSize );
298     }
299
300     /**
301      * Copy and convert bytes from an <code>InputStream</code> to chars on a
302      * <code>Writer</code>, using the specified encoding.
303      * @param encoding The name of a supported character encoding. See the
304      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
305      * Charset Registry</a> for a list of valid encoding types.
306      */

307     public static void copy( final InputStream JavaDoc input, final Writer JavaDoc output, final String JavaDoc encoding )
308         throws IOException JavaDoc
309     {
310         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input, encoding );
311         copy( in, output );
312     }
313
314     /**
315      * Copy and convert bytes from an <code>InputStream</code> to chars on a
316      * <code>Writer</code>, using the specified encoding.
317      * @param encoding The name of a supported character encoding. See the
318      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
319      * Charset Registry</a> for a list of valid encoding types.
320      * @param bufferSize Size of internal buffer to use.
321      */

322     public static void copy( final InputStream JavaDoc input,
323                              final Writer JavaDoc output,
324                              final String JavaDoc encoding,
325                              final int bufferSize )
326         throws IOException JavaDoc
327     {
328         final InputStreamReader JavaDoc in = new InputStreamReader JavaDoc( input, encoding );
329         copy( in, output, bufferSize );
330     }
331
332
333     ///////////////////////////////////////////////////////////////
334
// InputStream -> String
335

336     /**
337      * Get the contents of an <code>InputStream</code> as a String.
338      * The platform's default encoding is used for the byte-to-char conversion.
339      */

340     public static String JavaDoc toString( final InputStream JavaDoc input )
341         throws IOException JavaDoc
342     {
343         return toString( input, DEFAULT_BUFFER_SIZE );
344     }
345
346     /**
347      * Get the contents of an <code>InputStream</code> as a String.
348      * The platform's default encoding is used for the byte-to-char conversion.
349      * @param bufferSize Size of internal buffer to use.
350      */

351     public static String JavaDoc toString( final InputStream JavaDoc input, final int bufferSize )
352         throws IOException JavaDoc
353     {
354         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
355         copy( input, sw, bufferSize );
356         return sw.toString();
357     }
358
359     /**
360      * Get the contents of an <code>InputStream</code> as a String.
361      * @param encoding The name of a supported character encoding. See the
362      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
363      * Charset Registry</a> for a list of valid encoding types.
364      */

365     public static String JavaDoc toString( final InputStream JavaDoc input, final String JavaDoc encoding )
366         throws IOException JavaDoc
367     {
368         return toString( input, encoding, DEFAULT_BUFFER_SIZE );
369     }
370
371     /**
372      * Get the contents of an <code>InputStream</code> as a String.
373      * @param encoding The name of a supported character encoding. See the
374      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
375      * Charset Registry</a> for a list of valid encoding types.
376      * @param bufferSize Size of internal buffer to use.
377      */

378     public static String JavaDoc toString( final InputStream JavaDoc input,
379                                    final String JavaDoc encoding,
380                                    final int bufferSize )
381         throws IOException JavaDoc
382     {
383         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
384         copy( input, sw, encoding, bufferSize );
385         return sw.toString();
386     }
387
388     ///////////////////////////////////////////////////////////////
389
// InputStream -> byte[]
390

391     /**
392      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
393      */

394     public static byte[] toByteArray( final InputStream JavaDoc input )
395         throws IOException JavaDoc
396     {
397         return toByteArray( input, DEFAULT_BUFFER_SIZE );
398     }
399
400     /**
401      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
402      * @param bufferSize Size of internal buffer to use.
403      */

404     public static byte[] toByteArray( final InputStream JavaDoc input, final int bufferSize )
405         throws IOException JavaDoc
406     {
407         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
408         copy( input, output, bufferSize );
409         return output.toByteArray();
410     }
411
412
413     ///////////////////////////////////////////////////////////////
414
// Derived copy methods
415
// Reader -> *
416
///////////////////////////////////////////////////////////////
417

418     ///////////////////////////////////////////////////////////////
419
// Reader -> OutputStream
420
/**
421      * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
422      * flush the <code>OutputStream</code>.
423      */

424     public static void copy( final Reader JavaDoc input, final OutputStream JavaDoc output )
425         throws IOException JavaDoc
426     {
427         copy( input, output, DEFAULT_BUFFER_SIZE );
428     }
429
430     /**
431      * Serialize chars from a <code>Reader</code> to bytes on an <code>OutputStream</code>, and
432      * flush the <code>OutputStream</code>.
433      * @param bufferSize Size of internal buffer to use.
434      */

435     public static void copy( final Reader JavaDoc input, final OutputStream JavaDoc output, final int bufferSize )
436         throws IOException JavaDoc
437     {
438         final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc( output );
439         copy( input, out, bufferSize );
440         // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
441
// here.
442
out.flush();
443     }
444
445     ///////////////////////////////////////////////////////////////
446
// Reader -> String
447
/**
448      * Get the contents of a <code>Reader</code> as a String.
449      */

450     public static String JavaDoc toString( final Reader JavaDoc input )
451         throws IOException JavaDoc
452     {
453         return toString( input, DEFAULT_BUFFER_SIZE );
454     }
455
456     /**
457      * Get the contents of a <code>Reader</code> as a String.
458      * @param bufferSize Size of internal buffer to use.
459      */

460     public static String JavaDoc toString( final Reader JavaDoc input, final int bufferSize )
461         throws IOException JavaDoc
462     {
463         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
464         copy( input, sw, bufferSize );
465         return sw.toString();
466     }
467
468
469     ///////////////////////////////////////////////////////////////
470
// Reader -> byte[]
471
/**
472      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
473      */

474     public static byte[] toByteArray( final Reader JavaDoc input )
475         throws IOException JavaDoc
476     {
477         return toByteArray( input, DEFAULT_BUFFER_SIZE );
478     }
479
480     /**
481      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
482      * @param bufferSize Size of internal buffer to use.
483      */

484     public static byte[] toByteArray( final Reader JavaDoc input, final int bufferSize )
485         throws IOException JavaDoc
486     {
487         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
488         copy( input, output, bufferSize );
489         return output.toByteArray();
490     }
491
492
493     ///////////////////////////////////////////////////////////////
494
// Derived copy methods
495
// String -> *
496
///////////////////////////////////////////////////////////////
497

498
499     ///////////////////////////////////////////////////////////////
500
// String -> OutputStream
501

502     /**
503      * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
504      * flush the <code>OutputStream</code>.
505      */

506     public static void copy( final String JavaDoc input, final OutputStream JavaDoc output )
507         throws IOException JavaDoc
508     {
509         copy( input, output, DEFAULT_BUFFER_SIZE );
510     }
511
512     /**
513      * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
514      * flush the <code>OutputStream</code>.
515      * @param bufferSize Size of internal buffer to use.
516      */

517     public static void copy( final String JavaDoc input, final OutputStream JavaDoc output, final int bufferSize )
518         throws IOException JavaDoc
519     {
520         final StringReader JavaDoc in = new StringReader JavaDoc( input );
521         final OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc( output );
522         copy( in, out, bufferSize );
523         // NOTE: Unless anyone is planning on rewriting OutputStreamWriter, we have to flush
524
// here.
525
out.flush();
526     }
527
528
529
530     ///////////////////////////////////////////////////////////////
531
// String -> Writer
532

533     /**
534      * Copy chars from a <code>String</code> to a <code>Writer</code>.
535      */

536     public static void copy( final String JavaDoc input, final Writer JavaDoc output )
537         throws IOException JavaDoc
538     {
539         output.write( input );
540     }
541
542     /**
543      * Copy bytes from an <code>InputStream</code> to an
544      * <code>OutputStream</code>, with buffering.
545      * This is equivalent to passing a
546      * {@link java.io.BufferedInputStream} and
547      * {@link java.io.BufferedOutputStream} to {@link #copy(InputStream, OutputStream)},
548      * and flushing the output stream afterwards. The streams are not closed
549      * after the copy.
550      * @deprecated Buffering streams is actively harmful! See the class description as to why. Use
551      * {@link #copy(InputStream, OutputStream)} instead.
552      */

553     public static void bufferedCopy( final InputStream JavaDoc input, final OutputStream JavaDoc output )
554         throws IOException JavaDoc
555     {
556         final BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc( input );
557         final BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc( output );
558         copy( in, out );
559         out.flush();
560     }
561
562
563     ///////////////////////////////////////////////////////////////
564
// String -> byte[]
565
/**
566      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
567      */

568     public static byte[] toByteArray( final String JavaDoc input )
569         throws IOException JavaDoc
570     {
571         return toByteArray( input, DEFAULT_BUFFER_SIZE );
572     }
573
574     /**
575      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
576      * @param bufferSize Size of internal buffer to use.
577      */

578     public static byte[] toByteArray( final String JavaDoc input, final int bufferSize )
579         throws IOException JavaDoc
580     {
581         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
582         copy( input, output, bufferSize );
583         return output.toByteArray();
584     }
585
586
587
588     ///////////////////////////////////////////////////////////////
589
// Derived copy methods
590
// byte[] -> *
591
///////////////////////////////////////////////////////////////
592

593
594     ///////////////////////////////////////////////////////////////
595
// byte[] -> Writer
596

597     /**
598      * Copy and convert bytes from a <code>byte[]</code> to chars on a
599      * <code>Writer</code>.
600      * The platform's default encoding is used for the byte-to-char conversion.
601      */

602     public static void copy( final byte[] input, final Writer JavaDoc output )
603         throws IOException JavaDoc
604     {
605         copy( input, output, DEFAULT_BUFFER_SIZE );
606     }
607
608     /**
609      * Copy and convert bytes from a <code>byte[]</code> to chars on a
610      * <code>Writer</code>.
611      * The platform's default encoding is used for the byte-to-char conversion.
612      * @param bufferSize Size of internal buffer to use.
613      */

614     public static void copy( final byte[] input, final Writer JavaDoc output, final int bufferSize )
615         throws IOException JavaDoc
616     {
617         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
618         copy( in, output, bufferSize );
619     }
620
621     /**
622      * Copy and convert bytes from a <code>byte[]</code> to chars on a
623      * <code>Writer</code>, using the specified encoding.
624      * @param encoding The name of a supported character encoding. See the
625      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
626      * Charset Registry</a> for a list of valid encoding types.
627      */

628     public static void copy( final byte[] input, final Writer JavaDoc output, final String JavaDoc encoding )
629         throws IOException JavaDoc
630     {
631         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
632         copy( in, output, encoding );
633     }
634
635     /**
636      * Copy and convert bytes from a <code>byte[]</code> to chars on a
637      * <code>Writer</code>, using the specified encoding.
638      * @param encoding The name of a supported character encoding. See the
639      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
640      * Charset Registry</a> for a list of valid encoding types.
641      * @param bufferSize Size of internal buffer to use.
642      */

643     public static void copy( final byte[] input,
644                              final Writer JavaDoc output,
645                              final String JavaDoc encoding,
646                              final int bufferSize )
647         throws IOException JavaDoc
648     {
649         final ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc( input );
650         copy( in, output, encoding, bufferSize );
651     }
652
653
654     ///////////////////////////////////////////////////////////////
655
// byte[] -> String
656

657     /**
658      * Get the contents of a <code>byte[]</code> as a String.
659      * The platform's default encoding is used for the byte-to-char conversion.
660      */

661     public static String JavaDoc toString( final byte[] input )
662         throws IOException JavaDoc
663     {
664         return toString( input, DEFAULT_BUFFER_SIZE );
665     }
666
667     /**
668      * Get the contents of a <code>byte[]</code> as a String.
669      * The platform's default encoding is used for the byte-to-char conversion.
670      * @param bufferSize Size of internal buffer to use.
671      */

672     public static String JavaDoc toString( final byte[] input, final int bufferSize )
673         throws IOException JavaDoc
674     {
675         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
676         copy( input, sw, bufferSize );
677         return sw.toString();
678     }
679
680     /**
681      * Get the contents of a <code>byte[]</code> as a String.
682      * @param encoding The name of a supported character encoding. See the
683      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
684      * Charset Registry</a> for a list of valid encoding types.
685      */

686     public static String JavaDoc toString( final byte[] input, final String JavaDoc encoding )
687         throws IOException JavaDoc
688     {
689         return toString( input, encoding, DEFAULT_BUFFER_SIZE );
690     }
691
692     /**
693      * Get the contents of a <code>byte[]</code> as a String.
694      * @param encoding The name of a supported character encoding. See the
695      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
696      * Charset Registry</a> for a list of valid encoding types.
697      * @param bufferSize Size of internal buffer to use.
698      */

699     public static String JavaDoc toString( final byte[] input,
700                                    final String JavaDoc encoding,
701                                    final int bufferSize )
702         throws IOException JavaDoc
703     {
704         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
705         copy( input, sw, encoding, bufferSize );
706         return sw.toString();
707     }
708
709
710     ///////////////////////////////////////////////////////////////
711
// byte[] -> OutputStream
712

713     /**
714      * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
715      */

716     public static void copy( final byte[] input, final OutputStream JavaDoc output )
717         throws IOException JavaDoc
718     {
719         copy( input, output, DEFAULT_BUFFER_SIZE );
720     }
721
722     /**
723      * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
724      * @param bufferSize Size of internal buffer to use.
725      */

726     public static void copy( final byte[] input,
727                              final OutputStream JavaDoc output,
728                              final int bufferSize )
729         throws IOException JavaDoc
730     {
731         output.write( input );
732     }
733
734     /**
735      * Compare the contents of two Streams to determine if they are equal or not.
736      *
737      * @param input1 the first stream
738      * @param input2 the second stream
739      * @return true if the content of the streams are equal or they both don't exist, false otherwise
740      */

741     public static boolean contentEquals( final InputStream JavaDoc input1,
742                                          final InputStream JavaDoc input2 )
743         throws IOException JavaDoc
744     {
745         final InputStream JavaDoc bufferedInput1 = new BufferedInputStream JavaDoc( input1 );
746         final InputStream JavaDoc bufferedInput2 = new BufferedInputStream JavaDoc( input2 );
747
748         int ch = bufferedInput1.read();
749         while( -1 != ch )
750         {
751             final int ch2 = bufferedInput2.read();
752             if( ch != ch2 )
753             {
754                 return false;
755             }
756             ch = bufferedInput1.read();
757         }
758
759         final int ch2 = bufferedInput2.read();
760         if( -1 != ch2 )
761         {
762             return false;
763         }
764         else
765         {
766             return true;
767         }
768     }
769 }
770
Popular Tags