KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > IOUtils


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

16 package org.apache.commons.io;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import org.apache.commons.io.output.ByteArrayOutputStream;
27
28 /**
29  * General IO Stream manipulation.
30  * <p>
31  * This class provides static utility methods for input/output operations.
32  * </p>
33  * <p>The closeQuietly methods are expected to be used when an IOException
34  * would be meaningless. This is usually when in a catch block for an
35  * IOException. </p>
36  * <p>The toString and toByteArray methods all rely on CopyUtils.copy
37  * methods in the current implementation. </p>
38  *
39  * <p>Origin of code: Apache Avalon (Excalibur)</p>
40  *
41  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
42  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
43  * @version CVS $Revision: 1.14 $ $Date: 2004/04/24 23:49:25 $
44  */

45 public final class IOUtils
46 {
47     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
48
49     /**
50      * Instances should NOT be constructed in standard programming.
51      */

52     public IOUtils() {}
53
54     /**
55      * Unconditionally close an <code>Reader</code>.
56      * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
57      *
58      * @param input A (possibly null) Reader
59      */

60     public static void closeQuietly( Reader JavaDoc input )
61     {
62         if( input == null )
63         {
64             return;
65         }
66
67         try
68         {
69             input.close();
70         }
71         catch( IOException JavaDoc ioe )
72         {
73         }
74     }
75
76     /**
77      * Unconditionally close an <code>Writer</code>.
78      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
79      *
80      * @param output A (possibly null) Writer
81      */

82     public static void closeQuietly( Writer JavaDoc output )
83     {
84         if( output == null )
85         {
86             return;
87         }
88
89         try
90         {
91             output.close();
92         }
93         catch( IOException JavaDoc ioe )
94         {
95         }
96     }
97
98     /**
99      * Unconditionally close an <code>OutputStream</code>.
100      * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
101      * @param output A (possibly null) OutputStream
102      */

103     public static void closeQuietly( OutputStream JavaDoc output )
104     {
105         if( output == null )
106         {
107             return;
108         }
109
110         try
111         {
112             output.close();
113         }
114         catch( IOException JavaDoc ioe )
115         {
116         }
117     }
118
119     /**
120      * Unconditionally close an <code>InputStream</code>.
121      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
122      * @param input A (possibly null) InputStream
123      */

124     public static void closeQuietly( InputStream JavaDoc input )
125     {
126         if( input == null )
127         {
128             return;
129         }
130
131         try
132         {
133             input.close();
134         }
135         catch( IOException JavaDoc ioe )
136         {
137         }
138     }
139
140     /**
141      * Get the contents of an <code>InputStream</code> as a String.
142      * The platform's default encoding is used for the byte-to-char conversion.
143      * @param input the <code>InputStream</code> to read from
144      * @return the requested <code>String</code>
145      * @throws IOException In case of an I/O problem
146      */

147     public static String JavaDoc toString( InputStream JavaDoc input )
148         throws IOException JavaDoc
149     {
150         StringWriter JavaDoc sw = new StringWriter JavaDoc();
151         CopyUtils.copy( input, sw );
152         return sw.toString();
153     }
154
155     /**
156      * Get the contents of an <code>InputStream</code> as a String.
157      * @param input the <code>InputStream</code> to read from
158      * @param encoding The name of a supported character encoding. See the
159      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
160      * Charset Registry</a> for a list of valid encoding types.
161      * @return the requested <code>String</code>
162      * @throws IOException In case of an I/O problem
163      */

164     public static String JavaDoc toString( InputStream JavaDoc input,
165                                    String JavaDoc encoding )
166         throws IOException JavaDoc
167     {
168         StringWriter JavaDoc sw = new StringWriter JavaDoc();
169         CopyUtils.copy( input, sw, encoding );
170         return sw.toString();
171     }
172
173     ///////////////////////////////////////////////////////////////
174
// InputStream -> byte[]
175

176     /**
177      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
178      * @param input the <code>InputStream</code> to read from
179      * @return the requested byte array
180      * @throws IOException In case of an I/O problem
181      */

182     public static byte[] toByteArray( InputStream JavaDoc input )
183         throws IOException JavaDoc
184     {
185         ByteArrayOutputStream output = new ByteArrayOutputStream();
186         CopyUtils.copy( input, output );
187         return output.toByteArray();
188     }
189
190
191     ///////////////////////////////////////////////////////////////
192
// Derived copy methods
193
// Reader -> *
194
///////////////////////////////////////////////////////////////
195

196     ///////////////////////////////////////////////////////////////
197
// Reader -> String
198
/**
199      * Get the contents of a <code>Reader</code> as a String.
200      * @param input the <code>Reader</code> to read from
201      * @return the requested <code>String</code>
202      * @throws IOException In case of an I/O problem
203      */

204     public static String JavaDoc toString( Reader JavaDoc input )
205         throws IOException JavaDoc
206     {
207         StringWriter JavaDoc sw = new StringWriter JavaDoc();
208         CopyUtils.copy( input, sw );
209         return sw.toString();
210     }
211
212
213     ///////////////////////////////////////////////////////////////
214
// Reader -> byte[]
215
/**
216      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
217      * @param input the <code>Reader</code> to read from
218      * @return the requested byte array
219      * @throws IOException In case of an I/O problem
220      */

221     public static byte[] toByteArray( Reader JavaDoc input )
222         throws IOException JavaDoc
223     {
224         ByteArrayOutputStream output = new ByteArrayOutputStream();
225         CopyUtils.copy( input, output );
226         return output.toByteArray();
227     }
228
229
230     ///////////////////////////////////////////////////////////////
231
// Derived copy methods
232
// String -> *
233
///////////////////////////////////////////////////////////////
234

235
236     ///////////////////////////////////////////////////////////////
237
// String -> byte[]
238
/**
239      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
240      * @param input the <code>String</code> to convert
241      * @return the requested byte array
242      * @throws IOException In case of an I/O problem
243      */

244     public static byte[] toByteArray( String JavaDoc input )
245         throws IOException JavaDoc
246     {
247         ByteArrayOutputStream output = new ByteArrayOutputStream();
248         CopyUtils.copy( input, output );
249         return output.toByteArray();
250     }
251
252
253     ///////////////////////////////////////////////////////////////
254
// Derived copy methods
255
// byte[] -> *
256
///////////////////////////////////////////////////////////////
257

258     ///////////////////////////////////////////////////////////////
259
// byte[] -> String
260

261     /**
262      * Get the contents of a <code>byte[]</code> as a String.
263      * The platform's default encoding is used for the byte-to-char conversion.
264      * @param input the byte array to read from
265      * @return the requested <code>String</code>
266      * @throws IOException In case of an I/O problem
267      */

268     public static String JavaDoc toString( byte[] input )
269         throws IOException JavaDoc
270     {
271         StringWriter JavaDoc sw = new StringWriter JavaDoc();
272         CopyUtils.copy( input, sw );
273         return sw.toString();
274     }
275
276
277     /**
278      * Get the contents of a <code>byte[]</code> as a String.
279      * @param input the byte array to read from
280      * @param encoding The name of a supported character encoding. See the
281      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
282      * Charset Registry</a> for a list of valid encoding types.
283      * @return the requested <code>String</code>
284      * @throws IOException In case of an I/O problem
285      */

286     public static String JavaDoc toString( byte[] input,
287                                    String JavaDoc encoding )
288         throws IOException JavaDoc
289     {
290         StringWriter JavaDoc sw = new StringWriter JavaDoc();
291         CopyUtils.copy( input, sw, encoding );
292         return sw.toString();
293     }
294
295
296     /**
297      * Compare the contents of two Streams to determine if they are equal or not.
298      *
299      * @param input1 the first stream
300      * @param input2 the second stream
301      * @return true if the content of the streams are equal or they both don't exist, false otherwise
302      * @throws IOException In case of an I/O problem
303      */

304     public static boolean contentEquals( InputStream JavaDoc input1,
305                                          InputStream JavaDoc input2 )
306         throws IOException JavaDoc
307     {
308         InputStream JavaDoc bufferedInput1 = new BufferedInputStream JavaDoc( input1 );
309         InputStream JavaDoc bufferedInput2 = new BufferedInputStream JavaDoc( input2 );
310
311         int ch = bufferedInput1.read();
312         while( -1 != ch )
313         {
314             int ch2 = bufferedInput2.read();
315             if( ch != ch2 )
316             {
317                 return false;
318             }
319             ch = bufferedInput1.read();
320         }
321
322         int ch2 = bufferedInput2.read();
323         if( -1 != ch2 )
324         {
325             return false;
326         }
327         else
328         {
329             return true;
330         }
331     }
332 }
333
Popular Tags