KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > StreamUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.*;
6
7 /**
8  * Optimized byte and character stream utilities.
9  */

10 public class StreamUtil {
11
12     /**
13      * Buffer size is set to 32 KB.
14      */

15     public static int BUFFER_SIZE = 32768;
16
17
18     // ---------------------------------------------------------------- silent close
19

20     /**
21      * Closes an input stream and releases any system resources associated with
22      * this stream. No exception will be thrown if an I/O error occurs.
23      */

24     public static void close(InputStream in) {
25         if (in != null) {
26             try {
27                 in.close();
28             } catch (IOException ioe) {
29                 // ignore
30
}
31         }
32     }
33
34     /**
35      * Closes an output stream and releases any system resources associated with
36      * this stream. No exception will be thrown if an I/O error occurs.
37      */

38     public static void close(OutputStream out) {
39         if (out != null) {
40             try {
41                 out.close();
42             } catch (IOException ioe) {
43                 // ignore
44
}
45         }
46     }
47
48     /**
49      * Closes a character-input stream and releases any system resources
50      * associated with this stream. No exception will be thrown if an I/O error
51      * occurs.
52      */

53     public static void close(Reader in) {
54         if (in != null) {
55             try {
56                 in.close();
57             } catch (IOException ioe) {
58                 // ignore
59
}
60         }
61     }
62
63     /**
64      * Closes a character-output stream and releases any system resources
65      * associated with this stream. No exception will be thrown if an I/O error
66      * occurs.
67      */

68     public static void close(Writer out) {
69         if (out != null) {
70             try {
71                 out.close();
72             } catch (IOException ioe) {
73                 // ignore
74
}
75         }
76     }
77
78
79     // ---------------------------------------------------------------- copy
80

81     /**
82      * Copies input stream to output stream using buffer.
83      */

84     public static int copy(InputStream input, OutputStream output) throws IOException {
85         byte[] buffer = new byte[BUFFER_SIZE];
86         int count = 0;
87         int read;
88         while (true) {
89             read = input.read(buffer, 0, BUFFER_SIZE);
90             if (read == -1) {
91                 break;
92             }
93             output.write(buffer, 0, read);
94             count += read;
95         }
96         return count;
97     }
98     /**
99      * Copies specified number of bytes from input stream to output stream using buffer.
100      */

101     public static int copy(InputStream input, OutputStream output, int byteCount) throws IOException {
102         byte buffer[] = new byte[BUFFER_SIZE];
103         int count = 0;
104         int read;
105         while (byteCount > 0) {
106             if (byteCount < BUFFER_SIZE) {
107                 read = input.read(buffer, 0, byteCount);
108             } else {
109                 read = input.read(buffer, 0, BUFFER_SIZE);
110             }
111             if (read == -1) {
112                 break;
113             }
114             byteCount -= read;
115             count += read;
116             output.write(buffer, 0, read);
117         }
118         return count;
119     }
120
121
122
123
124     /**
125      * Copies input stream to writer using buffer.
126      */

127     public static void copy(InputStream input, Writer output) throws IOException {
128         copy(new InputStreamReader(input), output);
129     }
130     /**
131      * Copies specified number of bytes from input stream to writer using buffer.
132      */

133     public static void copy(InputStream input, Writer output, int byteCount) throws IOException {
134         copy(new InputStreamReader(input), output, byteCount);
135     }
136     /**
137      * Copies input stream to writter using buffer and specified encoding.
138      */

139     public static void copy(InputStream input, Writer output, String JavaDoc encoding) throws IOException {
140         if (encoding == null) {
141             copy(input, output);
142         } else {
143             copy(new InputStreamReader(input, encoding), output);
144         }
145     }
146     /**
147      * Copies specified number of bytes from input stream to writter using buffer and specified encoding.
148      */

149     public static void copy(InputStream input, Writer output, String JavaDoc encoding, int byteCount) throws IOException {
150         if (encoding == null) {
151             copy(input, output, byteCount);
152         } else {
153             copy(new InputStreamReader(input, encoding), output, byteCount);
154         }
155     }
156
157
158
159     /**
160      * Copies reader to writer using buffer.
161      */

162     public static int copy(Reader input, Writer output) throws IOException {
163         char[] buffer = new char[BUFFER_SIZE];
164         int count = 0;
165         int read;
166         while ((read = input.read(buffer, 0, BUFFER_SIZE)) >= 0) {
167             output.write(buffer, 0, read);
168             count += read;
169         }
170         output.flush();
171         return count;
172     }
173     /**
174      * Copies specified number of characters from reader to writer using buffer.
175      */

176     public static int copy(Reader input, Writer output, int charCount) throws IOException {
177         char buffer[] = new char[BUFFER_SIZE];
178         int count = 0;
179         int read;
180         while (charCount > 0) {
181             if (charCount < BUFFER_SIZE) {
182                 read = input.read(buffer, 0, charCount);
183             } else {
184                 read = input.read(buffer, 0, BUFFER_SIZE);
185             }
186             if (read == -1) {
187                 break;
188             }
189             charCount -= read;
190             count += read;
191             output.write(buffer, 0, read);
192         }
193         return count;
194     }
195
196
197
198     /**
199      * Copies reader to output stream using buffer.
200      */

201     public static void copy(Reader input, OutputStream output) throws IOException {
202         Writer out = new OutputStreamWriter(output);
203         copy(input, out);
204         out.flush();
205     }
206     /**
207      * Copies specified number of characters from reader to output stream using buffer.
208      */

209     public static void copy(Reader input, OutputStream output, int charCount) throws IOException {
210         Writer out = new OutputStreamWriter(output);
211         copy(input, out, charCount);
212         out.flush();
213     }
214     /**
215      * Copies reader to output stream using buffer and specified encoding.
216      */

217     public static void copy(Reader input, OutputStream output, String JavaDoc encoding) throws IOException {
218         if (encoding == null) {
219             copy(input, output);
220         } else {
221             Writer out = new OutputStreamWriter(output);
222             copy(input, out);
223             out.flush();
224         }
225     }
226     /**
227      * Copies specified number of characters from reader to output stream using buffer and specified encoding.
228      */

229     public static void copy(Reader input, OutputStream output, String JavaDoc encoding, int charCount) throws IOException {
230         if (encoding == null) {
231             copy(input, output, charCount);
232         } else {
233             Writer out = new OutputStreamWriter(output);
234             copy(input, out, charCount);
235             out.flush();
236         }
237     }
238
239
240     // ---------------------------------------------------------------- read bytes
241

242     /**
243      * Reads all availiable bytes from InputStream as a byte array.
244      * Uses <code>in.availiable()</code> to determine the size of input stream.
245      * This is the fastest method for reading input stream to byte array, but
246      * depends on stream implementation of <code>availiable()</code>.
247      * Buffered internally.
248      */

249     public static byte[] readAvailableBytes(InputStream in) throws IOException {
250         int l = in.available();
251         byte byteArray[] = new byte[l];
252         int i = 0, j;
253         while ((i < l) && (j = in.read(byteArray, i, l - i)) >= 0) {
254             i +=j;
255         }
256         if (i < l) {
257             throw new IOException("Could not completely read from input stream.");
258         }
259         return byteArray;
260     }
261
262     public static byte[] readBytes(InputStream input) throws IOException {
263         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
264         copy(input, output);
265         return output.toByteArray();
266     }
267     public static byte[] readBytes(InputStream input, int byteCount) throws IOException {
268         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
269         copy(input, output, byteCount);
270         return output.toByteArray();
271     }
272
273     public static byte[] readBytes(Reader input) throws IOException {
274         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
275         copy(input, output);
276         return output.toByteArray();
277     }
278     public static byte[] readBytes(Reader input, int byteCount) throws IOException {
279         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
280         copy(input, output, byteCount);
281         return output.toByteArray();
282     }
283     public static byte[] readBytes(Reader input, String JavaDoc encoding) throws IOException {
284         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
285         copy(input, output, encoding);
286         return output.toByteArray();
287     }
288     public static byte[] readBytes(Reader input, String JavaDoc encoding, int byteCount) throws IOException {
289         FastByteArrayOutputStream output = new FastByteArrayOutputStream();
290         copy(input, output, encoding, byteCount);
291         return output.toByteArray();
292     }
293
294     // ---------------------------------------------------------------- read chars
295

296     public static char[] readChars(InputStream input) throws IOException {
297         FastCharArrayWriter output = new FastCharArrayWriter();
298         copy(input, output);
299         return output.toCharArray();
300     }
301     public static char[] readChars(InputStream input, int charCount) throws IOException {
302         FastCharArrayWriter output = new FastCharArrayWriter();
303         copy(input, output, charCount);
304         return output.toCharArray();
305     }
306
307     public static char[] readChars(InputStream input, String JavaDoc encoding) throws IOException {
308         FastCharArrayWriter output = new FastCharArrayWriter();
309         copy(input, output, encoding);
310         return output.toCharArray();
311     }
312     public static char[] readChars(InputStream input, String JavaDoc encoding, int charCount) throws IOException {
313         FastCharArrayWriter output = new FastCharArrayWriter();
314         copy(input, output, encoding, charCount);
315         return output.toCharArray();
316     }
317
318     public static char[] readChars(Reader input) throws IOException {
319         FastCharArrayWriter output = new FastCharArrayWriter();
320         copy(input, output);
321         return output.toCharArray();
322     }
323     public static char[] readChars(Reader input, int charCount) throws IOException {
324         FastCharArrayWriter output = new FastCharArrayWriter();
325         copy(input, output, charCount);
326         return output.toCharArray();
327     }
328
329
330     // ---------------------------------------------------------------- compare content
331

332     /**
333      * Compares the content of two byte streams.
334      *
335      * @return <code>true</code> if the content of the first stream is equal
336      * to the content of the second stream.
337      */

338     public static boolean compare(InputStream input1, InputStream input2) throws IOException {
339         if (!(input1 instanceof BufferedInputStream)) {
340             input1 = new BufferedInputStream(input1);
341         }
342         if (!(input2 instanceof BufferedInputStream)) {
343             input2 = new BufferedInputStream(input2);
344         }
345         int ch = input1.read();
346         while (ch != -1) {
347             int ch2 = input2.read();
348             if (ch != ch2) {
349                 return false;
350             }
351             ch = input1.read();
352         }
353         int ch2 = input2.read();
354         return (ch2 == -1);
355     }
356     /**
357      * Compares the content of two character streams.
358      *
359      * @return <code>true</code> if the content of the first stream is equal
360      * to the content of the second stream.
361      */

362     public static boolean compare(Reader input1, Reader input2) throws IOException {
363         if (!(input1 instanceof BufferedReader)) {
364             input1 = new BufferedReader(input1);
365         }
366         if (!(input2 instanceof BufferedReader)) {
367             input2 = new BufferedReader(input2);
368         }
369
370         int ch = input1.read();
371         while (ch != -1) {
372             int ch2 = input2.read();
373             if (ch != ch2) {
374                 return false;
375             }
376             ch = input1.read();
377         }
378         int ch2 = input2.read();
379         return (ch2 == -1);
380     }
381
382 }
383
Popular Tags