KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > stream > Streams


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.stream;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.BufferedOutputStream JavaDoc;
29
30 import org.jboss.logging.Logger;
31
32 /**
33  * A collection of stream related utility methods.
34  *
35  * <p>Exceptions that are thrown and not explicitly declared are ignored.
36  *
37  * @version <tt>$Revision: 1958 $</tt>
38  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
39  */

40 public final class Streams
41 {
42    private static final Logger log = Logger.getLogger(Streams.class);
43    
44    /////////////////////////////////////////////////////////////////////////
45
// Closing //
46
/////////////////////////////////////////////////////////////////////////
47

48    /**
49     * Attempt to close an <tt>InputStream</tt>.
50     *
51     * @param stream <tt>InputStream</tt> to attempt to close.
52     * @return <tt>True</tt> if stream was closed (or stream was null),
53     * or <tt>false</tt> if an exception was thrown.
54     */

55    public static boolean close(final InputStream JavaDoc stream) {
56       // do not attempt to close null stream, but return sucess
57
if (stream == null) {
58          return true;
59       }
60       
61       boolean success = true;
62
63       try {
64          stream.close();
65       }
66       catch (IOException JavaDoc e) {
67          success = false;
68       }
69
70       return success;
71    }
72
73    /**
74     * Attempt to close an <tt>OutputStream</tt>.
75     *
76     * @param stream <tt>OutputStream</tt> to attempt to close.
77     * @return <tt>True</tt> if stream was closed (or stream was null),
78     * or <tt>false</tt> if an exception was thrown.
79     */

80    public static boolean close(final OutputStream JavaDoc stream) {
81       // do not attempt to close null stream, but return sucess
82
if (stream == null) {
83          return true;
84       }
85
86       boolean success = true;
87
88       try {
89          stream.close();
90       }
91       catch (IOException JavaDoc e) {
92          success = false;
93       }
94
95       return success;
96    }
97
98    /**
99     * Attempt to close an <tt>InputStream</tt> or <tt>OutputStream</tt>.
100     *
101     * @param stream Stream to attempt to close.
102     * @return <tt>True</tt> if stream was closed (or stream was null),
103     * or <tt>false</tt> if an exception was thrown.
104     *
105     * @throws IllegalArgumentException Stream is not an <tt>InputStream</tt>
106     * or <tt>OuputStream</tt>.
107     */

108    public static boolean close(final Object JavaDoc stream) {
109       boolean success = false;
110
111       if (stream instanceof InputStream JavaDoc) {
112          success = close((InputStream JavaDoc)stream);
113       }
114       else if (stream instanceof OutputStream JavaDoc) {
115          success = close((OutputStream JavaDoc)stream);
116       }
117       else {
118          throw new IllegalArgumentException JavaDoc
119             ("stream is not an InputStream or OutputStream");
120       }
121
122       return success;
123    }
124
125    /**
126     * Attempt to close an array of <tt>InputStream</tt>s.
127     *
128     * @param streams Array of <tt>InputStream</tt>s to attempt to close.
129     * @return <tt>True</tt> if all streams were closed, or <tt>false</tt>
130     * if an exception was thrown.
131     */

132    public static boolean close(final InputStream JavaDoc[] streams) {
133       boolean success = true;
134
135       for (int i=0; i<streams.length; i++) {
136          boolean rv = close(streams[i]);
137          if (!rv) success = false;
138       }
139
140       return success;
141    }
142
143    /**
144     * Attempt to close an array of <tt>OutputStream</tt>s.
145     *
146     * @param streams Array of <tt>OutputStream</tt>s to attempt to close.
147     * @return <tt>True</tt> if all streams were closed, or <tt>false</tt>
148     * if an exception was thrown.
149     */

150    public static boolean close(final OutputStream JavaDoc[] streams) {
151       boolean success = true;
152
153       for (int i=0; i<streams.length; i++) {
154          boolean rv = close(streams[i]);
155          if (!rv) success = false;
156       }
157
158       return success;
159    }
160
161    /**
162     * Attempt to close an array of <tt>InputStream</tt>a and/or
163     * <tt>OutputStream</tt>s.
164     *
165     * @param streams Array of streams to attempt to close.
166     * @return <tt>True</tt> if all streams were closed, or <tt>false</tt>
167     * if an exception was thrown.
168     *
169     * @throws IllegalArgumentException Stream is not an <tt>InputStream</tt>
170     * or <tt>OuputStream</tt>. Closing
171     * stops at the last valid stream
172     * object in this case.
173     */

174    public static boolean close(final Object JavaDoc[] streams) {
175       boolean success = true;
176
177       for (int i=0; i<streams.length; i++) {
178          boolean rv = close(streams[i]);
179          if (!rv) success = false;
180       }
181
182       return success;
183    }
184
185    /**
186     * Attempt to flush and close an <tt>OutputStream</tt>.
187     *
188     * @param stream <tt>OutputStream</tt> to attempt to flush and close.
189     * @return <tt>True</tt> if stream was flushed and closed, or
190     * <tt>false</tt> if an exception was thrown.
191     */

192    public static boolean fclose(final OutputStream JavaDoc stream) {
193        return flush(stream) && close(stream);
194    }
195
196    /**
197     * Attempt to flush and close an array of <tt>OutputStream</tt>s.
198     *
199     * @param streams <tt>OutputStream</tt>s to attempt to flush and close.
200     * @return <tt>True</tt> if all streams were flushed and closed,
201     * or <tt>false</tt> if an exception was thrown.
202     */

203    public static boolean fclose(final OutputStream JavaDoc[] streams) {
204       boolean success = true;
205
206       for (int i=0; i<streams.length; i++) {
207          boolean rv = fclose(streams[i]);
208          if (!rv) success = false;
209       }
210
211       return success;
212    }
213     
214
215    /////////////////////////////////////////////////////////////////////////
216
// Flushing //
217
/////////////////////////////////////////////////////////////////////////
218

219    /**
220     * Attempt to flush an <tt>OutputStream</tt>.
221     *
222     * @param stream <tt>OutputStream</tt> to attempt to flush.
223     * @return <tt>True</tt> if stream was flushed (or stream was null),
224     * or <tt>false</tt> if an exception was thrown.
225     */

226    public static boolean flush(final OutputStream JavaDoc stream) {
227       // do not attempt to close null stream, but return sucess
228
if (stream == null) {
229          return true;
230       }
231       
232       boolean success = true;
233
234       try {
235          stream.flush();
236       }
237       catch (IOException JavaDoc e) {
238          success = false;
239       }
240
241       return success;
242    }
243
244    /**
245     * Attempt to flush an array of <tt>OutputStream</tt>s.
246     *
247     * @param streams <tt>OutputStream</tt>s to attempt to flush.
248     * @return <tt>True</tt> if all streams were flushed, or <tt>false</tt>
249     * if an exception was thrown.
250     */

251    public static boolean flush(final OutputStream JavaDoc[] streams) {
252       boolean success = true;
253
254       for (int i=0; i<streams.length; i++) {
255          boolean rv = flush(streams[i]);
256          if (!rv) success = false;
257       }
258
259       return success;
260    }
261
262
263    /////////////////////////////////////////////////////////////////////////
264
// Misc //
265
/////////////////////////////////////////////////////////////////////////
266

267    /** The default buffer size that will be used for buffered operations. */
268    public static final int DEFAULT_BUFFER_SIZE = 2048;
269
270    /**
271     * Copy all of the bytes from the input stream to the output stream.
272     *
273     * @param input Stream to read bytes from.
274     * @param output Stream to write bytes to.
275     * @param buffer The buffer to use while copying.
276     * @return The total number of bytes copied.
277     *
278     * @throws IOException Failed to copy bytes.
279     */

280    public static long copy(final InputStream JavaDoc input,
281                            final OutputStream JavaDoc output,
282                            final byte buffer[])
283       throws IOException JavaDoc
284    {
285       long total = 0;
286       int read;
287
288       boolean trace = log.isTraceEnabled();
289       if (trace) {
290          log.trace("copying " + input + " to " + output + " with buffer size: " + buffer.length);
291       }
292       
293       while ((read = input.read(buffer)) != -1) {
294          output.write(buffer, 0, read);
295          total += read;
296
297          if (trace) {
298             log.trace("bytes read: " + read + "; total bytes read: " + total);
299          }
300       }
301
302       return total;
303    }
304
305    /**
306     * Copy all of the bytes from the input stream to the output stream.
307     *
308     * @param input Stream to read bytes from.
309     * @param output Stream to write bytes to.
310     * @param size The size of the buffer to use while copying.
311     * @return The total number of bytes copied.
312     *
313     * @throws IOException Failed to copy bytes.
314     */

315    public static long copy(final InputStream JavaDoc input,
316                            final OutputStream JavaDoc output,
317                            final int size)
318       throws IOException JavaDoc
319    {
320       return copy(input, output, new byte[size]);
321    }
322
323    /**
324     * Copy all of the bytes from the input stream to the output stream.
325     *
326     * @param input Stream to read bytes from.
327     * @param output Stream to write bytes to.
328     * @return The total number of bytes copied.
329     *
330     * @throws IOException Failed to copy bytes.
331     */

332    public static long copy(final InputStream JavaDoc input,
333                            final OutputStream JavaDoc output)
334       throws IOException JavaDoc
335    {
336       return copy(input, output, DEFAULT_BUFFER_SIZE);
337    }
338
339    /**
340     * Copy all of the bytes from the input stream to the output stream
341     * wrapping streams in buffers as needed.
342     *
343     * @param input Stream to read bytes from.
344     * @param output Stream to write bytes to.
345     * @return The total number of bytes copied.
346     *
347     * @throws IOException Failed to copy bytes.
348     */

349    public static long copyb(InputStream JavaDoc input,
350                             OutputStream JavaDoc output)
351       throws IOException JavaDoc
352    {
353       if (!(input instanceof BufferedInputStream JavaDoc)) {
354          input = new BufferedInputStream JavaDoc(input);
355       }
356       
357       if (!(output instanceof BufferedOutputStream JavaDoc)) {
358          output = new BufferedOutputStream JavaDoc(output);
359       }
360
361       long bytes = copy(input, output, DEFAULT_BUFFER_SIZE);
362
363       output.flush();
364
365       return bytes;
366    }
367    
368    /**
369     * Copy a limited number of bytes from the input stream to the
370     * output stream.
371     *
372     * @param input Stream to read bytes from.
373     * @param output Stream to write bytes to.
374     * @param buffer The buffer to use while copying.
375     * @param length The maximum number of bytes to copy.
376     * @return The total number of bytes copied.
377     *
378     * @throws IOException Failed to copy bytes.
379     */

380    public static long copySome(final InputStream JavaDoc input,
381                                final OutputStream JavaDoc output,
382                                final byte buffer[],
383                                final long length)
384       throws IOException JavaDoc
385    {
386       long total = 0;
387       int read;
388       int readLength;
389
390       boolean trace = log.isTraceEnabled();
391       
392       // setup the initial readLength, if length is less than the buffer
393
// size, then we only want to read that much
394
readLength = Math.min((int)length, buffer.length);
395       if (trace) {
396          log.trace("initial read length: " + readLength);
397       }
398
399       while (readLength != 0 && (read = input.read(buffer, 0, readLength)) != -1)
400       {
401          if (trace) log.trace("read bytes: " + read);
402          output.write(buffer, 0, read);
403          total += read;
404          if (trace) log.trace("total bytes read: " + total);
405
406          // update the readLength
407
readLength = Math.min((int)(length - total), buffer.length);
408          if (trace) log.trace("next read length: " + readLength);
409       }
410
411       return total;
412    }
413
414    /**
415     * Copy a limited number of bytes from the input stream to the
416     * output stream.
417     *
418     * @param input Stream to read bytes from.
419     * @param output Stream to write bytes to.
420     * @param size The size of the buffer to use while copying.
421     * @param length The maximum number of bytes to copy.
422     * @return The total number of bytes copied.
423     *
424     * @throws IOException Failed to copy bytes.
425     */

426    public static long copySome(final InputStream JavaDoc input,
427                                final OutputStream JavaDoc output,
428                                final int size,
429                                final long length)
430       throws IOException JavaDoc
431    {
432       return copySome(input, output, new byte[size], length);
433    }
434
435    /**
436     * Copy a limited number of bytes from the input stream to the
437     * output stream.
438     *
439     * @param input Stream to read bytes from.
440     * @param output Stream to write bytes to.
441     * @param length The maximum number of bytes to copy.
442     * @return The total number of bytes copied.
443     *
444     * @throws IOException Failed to copy bytes.
445     */

446    public static long copySome(final InputStream JavaDoc input,
447                                final OutputStream JavaDoc output,
448                                final long length)
449       throws IOException JavaDoc
450    {
451       return copySome(input, output, DEFAULT_BUFFER_SIZE, length);
452    }
453 }
454
Popular Tags