KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > StreamUtils


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.io;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Contains utility methods for handling streams.
27  */

28 public class StreamUtils {
29     private static final int BUFFERSIZE = 8000;
30
31     /**
32      * Copies length bytes from an InputStream to an OutputStream.
33      *
34      * @param in
35      * InputStream from wihch the bytes are to copied.
36      * @param out
37      * OutputStream in which the bytes are copied.
38      * @param length
39      * The number of bytes to copy
40      * @return Number of bytes which are copied.
41      * @throws IOException
42      * If the streams are unavailable.
43      */

44     public static int streamCopy(InputStream JavaDoc in, OutputStream JavaDoc out, int length)
45             throws IOException JavaDoc {
46         byte[] buffer = new byte[BUFFERSIZE];
47         int read;
48         int copied = 0;
49
50         while ((read = in
51                 .read(buffer, 0, Math.min(BUFFERSIZE, length - copied))) > 0) {
52             out.write(buffer, 0, read);
53             copied += read;
54         }
55
56         return copied;
57     }
58
59     /**
60      * Copies all bytes from an InputStream to an OutputStream. The buffer size
61      * is set to 8000 bytes.
62      *
63      * @param _isInput
64      * InputStream from wihch the bytes are to copied.
65      * @param _osOutput
66      * OutputStream in which the bytes are copied.
67      * @return Number of bytes which are copied.
68      * @throws IOException
69      * If the Streams are unavailable.
70      */

71     public static long streamCopy(InputStream JavaDoc in, OutputStream JavaDoc out)
72             throws IOException JavaDoc {
73         byte[] buffer = new byte[BUFFERSIZE];
74         int read;
75         long copied = 0;
76
77         while ((read = in.read(buffer)) > 0) {
78             out.write(buffer, 0, read);
79             copied += read;
80         }
81
82         return copied;
83     }
84
85     /**
86      * Reads a InputStream of chars into a StringBuffer.
87      *
88      * @param in
89      * the InputStream to read from
90      * @return the interpreted InputStream
91      * @throws IOException
92      */

93     public static StringBuffer JavaDoc readCharacterStream(InputStream JavaDoc in)
94             throws IOException JavaDoc {
95         StringBuffer JavaDoc result = new StringBuffer JavaDoc(in.available());
96         int read = in.read();
97
98         while (read > 0) {
99             result.append((char) read);
100             read = in.read();
101         }
102
103         in.close();
104
105         return result;
106     }
107
108     public static byte[] readInByteArray(InputStream JavaDoc in) throws IOException JavaDoc {
109         byte[] result = new byte[in.available()];
110
111         in.read(result);
112
113         in.close();
114
115         return result;
116     }
117
118     /**
119      * Copies all bytes from the given InputStream into an internal
120      * ByteArrayOutputStream and returnes a new InputStream with all bytes from
121      * the ByteArrayOutputStream. The data are real copied so this method
122      * "clones" the given Inputstream and returns a new InputStream with same
123      * data.
124      *
125      * @param from
126      * InputStream from which all data are to copy
127      * @return a new InputStream with all data from the given InputStream
128      * @throws IOException
129      */

130     public static InputStream JavaDoc streamClone(InputStream JavaDoc from) throws IOException JavaDoc {
131         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
132         streamCopy(from, out);
133
134         return new ByteArrayInputStream JavaDoc(out.toByteArray());
135     }
136 }
137
Popular Tags