KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > IOs


1 /*
2  * CopyrightPlugin (c) 2004 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ioc.util;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.ByteArrayOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.OutputStreamWriter JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.Writer JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
21  */

22
23 public class IOs {
24     /**
25      * The name says it all.
26      */

27     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
28
29     /**
30      * Instances should NOT be constructed in standard programming.
31      */

32     private IOs() {
33     }
34
35
36     // ----------------------------------------------------------------
37
// byte[] -> Writer
38
// ----------------------------------------------------------------
39

40     /**
41      * Copy and convert bytes from a <code>byte[]</code> to chars on a
42      * <code>Writer</code>.
43      * The platform's default encoding is used for the byte-to-char conversion.
44      *
45      * @param input the byte array to read from
46      * @param output the <code>Writer</code> to write to
47      * @throws IOException In case of an I/O problem
48      */

49     public static void write(byte[] input, Writer JavaDoc output)
50             throws IOException JavaDoc {
51         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(input);
52         write(in, output);
53     }
54
55
56     /**
57      * Copy and convert bytes from a <code>byte[]</code> to chars on a
58      * <code>Writer</code>, using the specified encoding.
59      *
60      * @param input the byte array to read from
61      * @param output the <code>Writer</code> to write to
62      * @param encoding The name of a supported character encoding. See the
63      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
64      * Charset Registry</a> for a list of valid encoding types.
65      * @throws IOException In case of an I/O problem
66      */

67     public static void write(byte[] input,
68                              Writer JavaDoc output,
69                              String JavaDoc encoding)
70             throws IOException JavaDoc {
71         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(input);
72         write(in, output, encoding);
73     }
74
75
76     // ----------------------------------------------------------------
77
// Core write methods
78
// ----------------------------------------------------------------
79

80     /**
81      * Copy bytes from an <code>InputStream</code> to an <code>OutputStream</code>.
82      *
83      * @param input the <code>InputStream</code> to read from
84      * @param output the <code>OutputStream</code> to write to
85      * @return the number of bytes copied
86      * @throws IOException In case of an I/O problem
87      */

88     public static int write(InputStream JavaDoc input,
89                             OutputStream JavaDoc output)
90             throws IOException JavaDoc {
91         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
92         int count = 0;
93         int n = 0;
94         while(-1 != (n = input.read(buffer))) {
95             output.write(buffer, 0, n);
96             count += n;
97         }
98         return count;
99     }
100
101     // ----------------------------------------------------------------
102
// Reader -> Writer
103
// ----------------------------------------------------------------
104

105     /**
106      * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
107      *
108      * @param input the <code>Reader</code> to read from
109      * @param output the <code>Writer</code> to write to
110      * @return the number of characters copied
111      * @throws IOException In case of an I/O problem
112      */

113     public static int write(Reader JavaDoc input,
114                             Writer JavaDoc output)
115             throws IOException JavaDoc {
116         char[] buffer = new char[DEFAULT_BUFFER_SIZE];
117         int count = 0;
118         int n = 0;
119         while(-1 != (n = input.read(buffer))) {
120             output.write(buffer, 0, n);
121             count += n;
122         }
123         return count;
124     }
125
126     // ----------------------------------------------------------------
127
// InputStream -> Writer
128
// ----------------------------------------------------------------
129

130     /**
131      * Copy and convert bytes from an <code>InputStream</code> to chars on a
132      * <code>Writer</code>.
133      * The platform's default encoding is used for the byte-to-char conversion.
134      *
135      * @param input the <code>InputStream</code> to read from
136      * @param output the <code>Writer</code> to write to
137      * @throws IOException In case of an I/O problem
138      */

139     public static void write(InputStream JavaDoc input,
140                              Writer JavaDoc output)
141             throws IOException JavaDoc {
142         InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(input);
143         write(in, output);
144     }
145
146     /**
147      * Copy and convert bytes from an <code>InputStream</code> to chars on a
148      * <code>Writer</code>, using the specified encoding.
149      *
150      * @param input the <code>InputStream</code> to read from
151      * @param output the <code>Writer</code> to write to
152      * @param encoding The name of a supported character encoding. See the
153      * <a HREF="http://www.iana.org/assignments/character-sets">IANA
154      * Charset Registry</a> for a list of valid encoding types.
155      * @throws IOException In case of an I/O problem
156      */

157     public static void write(InputStream JavaDoc input,
158                              Writer JavaDoc output,
159                              String JavaDoc encoding)
160             throws IOException JavaDoc {
161         InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(input, encoding);
162         write(in, output);
163     }
164
165
166     // ----------------------------------------------------------------
167
// Reader -> OutputStream
168
// ----------------------------------------------------------------
169

170     /**
171      * Serialize chars from a <code>Reader</code> to bytes on an
172      * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
173      *
174      * @param input the <code>Reader</code> to read from
175      * @param output the <code>OutputStream</code> to write to
176      * @throws IOException In case of an I/O problem
177      */

178     public static void write(Reader JavaDoc input,
179                              OutputStream JavaDoc output)
180             throws IOException JavaDoc {
181         OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc(output);
182         write(input, out);
183         // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
184
out.flush();
185     }
186
187     // ----------------------------------------------------------------
188
// String -> OutputStream
189
// ----------------------------------------------------------------
190

191     /**
192      * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and
193      * flush the <code>OutputStream</code>.
194      *
195      * @param input the <code>String</code> to read from
196      * @param output the <code>OutputStream</code> to write to
197      * @throws IOException In case of an I/O problem
198      */

199     public static void write(String JavaDoc input,
200                              OutputStream JavaDoc output)
201             throws IOException JavaDoc {
202         StringReader JavaDoc in = new StringReader JavaDoc(input);
203         OutputStreamWriter JavaDoc out = new OutputStreamWriter JavaDoc(output);
204         write(in, out);
205         // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
206
out.flush();
207     }
208
209     // ----------------------------------------------------------------
210
// String -> Writer
211
// ----------------------------------------------------------------
212

213     /**
214      * Copy chars from a <code>String</code> to a <code>Writer</code>.
215      *
216      * @param input the <code>String</code> to read from
217      * @param output the <code>Writer</code> to write to
218      * @throws IOException In case of an I/O problem
219      */

220     public static void write(String JavaDoc input, Writer JavaDoc output)
221             throws IOException JavaDoc {
222         output.write(input);
223     }
224
225
226     /**
227      * Unconditionally close an <code>Reader</code>.
228      * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
229      *
230      * @param input A (possibly null) Reader
231      */

232     public static void close(Reader JavaDoc input) {
233         if(input == null) {
234             return;
235         }
236
237         try {
238             input.close();
239         }
240         catch(IOException JavaDoc ioe) {
241         }
242     }
243
244     /**
245      * Unconditionally close an <code>Writer</code>.
246      * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
247      *
248      * @param output A (possibly null) Writer
249      */

250     public static void close(Writer JavaDoc output) {
251         if(output == null) {
252             return;
253         }
254
255         try {
256             output.close();
257         }
258         catch(IOException JavaDoc ioe) {
259         }
260     }
261
262     /**
263      * Unconditionally close an <code>OutputStream</code>.
264      * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
265      *
266      * @param output A (possibly null) OutputStream
267      */

268     public static void close(OutputStream JavaDoc output) {
269         if(output == null) {
270             return;
271         }
272
273         try {
274             output.close();
275         }
276         catch(IOException JavaDoc ioe) {
277         }
278     }
279
280     /**
281      * Unconditionally close an <code>InputStream</code>.
282      * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
283      *
284      * @param input A (possibly null) InputStream
285      */

286     public static void close(InputStream JavaDoc input) {
287         if(input == null) {
288             return;
289         }
290
291         try {
292             input.close();
293         }
294         catch(IOException JavaDoc ioe) {
295         }
296     }
297
298     /**
299      * Get the contents of an <code>InputStream</code> as a String.
300      * The platform's default encoding is used for the byte-to-char conversion.
301      *
302      * @param input the <code>InputStream</code> to read from
303      * @return the requested <code>String</code>
304      * @throws IOException In case of an I/O problem
305      */

306     public static String JavaDoc toString(InputStream JavaDoc input)
307             throws IOException JavaDoc {
308         StringWriter JavaDoc sw = new StringWriter JavaDoc();
309         write(input, sw);
310         return sw.toString();
311     }
312
313     /**
314      * Get the contents of an <code>InputStream</code> as a String.
315      *
316      * @param input the <code>InputStream</code> to read from
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      * @return the requested <code>String</code>
321      * @throws IOException In case of an I/O problem
322      */

323     public static String JavaDoc toString(InputStream JavaDoc input,
324                                   String JavaDoc encoding)
325             throws IOException JavaDoc {
326         StringWriter JavaDoc sw = new StringWriter JavaDoc();
327         write(input, sw, encoding);
328         return sw.toString();
329     }
330
331     ///////////////////////////////////////////////////////////////
332
// InputStream -> byte[]
333

334     /**
335      * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
336      *
337      * @param input the <code>InputStream</code> to read from
338      * @return the requested byte array
339      * @throws IOException In case of an I/O problem
340      */

341     public static byte[] toByteArray(InputStream JavaDoc input)
342             throws IOException JavaDoc {
343         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
344         write(input, output);
345         return output.toByteArray();
346     }
347
348
349     ///////////////////////////////////////////////////////////////
350
// Derived write methods
351
// Reader -> *
352
///////////////////////////////////////////////////////////////
353

354     ///////////////////////////////////////////////////////////////
355
// Reader -> String
356
/**
357      * Get the contents of a <code>Reader</code> as a String.
358      *
359      * @param input the <code>Reader</code> to read from
360      * @return the requested <code>String</code>
361      * @throws IOException In case of an I/O problem
362      */

363     public static String JavaDoc toString(Reader JavaDoc input)
364             throws IOException JavaDoc {
365         StringWriter JavaDoc sw = new StringWriter JavaDoc();
366         write(input, sw);
367         return sw.toString();
368     }
369
370
371     ///////////////////////////////////////////////////////////////
372
// Reader -> byte[]
373
/**
374      * Get the contents of a <code>Reader</code> as a <code>byte[]</code>.
375      *
376      * @param input the <code>Reader</code> to read from
377      * @return the requested byte array
378      * @throws IOException In case of an I/O problem
379      */

380     public static byte[] toByteArray(Reader JavaDoc input)
381             throws IOException JavaDoc {
382         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
383         write(input, output);
384         return output.toByteArray();
385     }
386
387
388     ///////////////////////////////////////////////////////////////
389
// Derived write methods
390
// String -> *
391
///////////////////////////////////////////////////////////////
392

393
394     ///////////////////////////////////////////////////////////////
395
// String -> byte[]
396
/**
397      * Get the contents of a <code>String</code> as a <code>byte[]</code>.
398      *
399      * @param input the <code>String</code> to convert
400      * @return the requested byte array
401      * @throws IOException In case of an I/O problem
402      */

403     public static byte[] toByteArray(String JavaDoc input)
404             throws IOException JavaDoc {
405         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
406         write(input, output);
407         return output.toByteArray();
408     }
409
410
411     ///////////////////////////////////////////////////////////////
412
// Derived write methods
413
// byte[] -> *
414
///////////////////////////////////////////////////////////////
415

416     ///////////////////////////////////////////////////////////////
417
// byte[] -> String
418

419
420     public static void main(String JavaDoc[] args) {
421
422     }
423 }
424
425
Popular Tags