KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > IOTools


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

17 package org.apache.catalina.util;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.Writer JavaDoc;
24
25
26 /**
27  * Contains commonly needed I/O-related methods
28  *
29  * @author Dan Sandberg
30  */

31 public class IOTools {
32     protected final static int DEFAULT_BUFFER_SIZE=4*1024; //4k
33

34     //Ensure non-instantiability
35
private IOTools() {
36     }
37
38      /**
39      * Read input from reader and write it to writer until there is no more
40      * input from reader.
41      *
42      * @param reader the reader to read from.
43      * @param writer the writer to write to.
44      * @param buf the char array to use as a bufferx
45      */

46     public static void flow( Reader JavaDoc reader, Writer JavaDoc writer, char[] buf )
47         throws IOException JavaDoc {
48         int numRead;
49         while ( (numRead = reader.read(buf) ) >= 0) {
50             writer.write(buf, 0, numRead);
51         }
52     }
53
54     /**
55      * @see #flow( Reader, Writer, char[] )
56      */

57     public static void flow( Reader JavaDoc reader, Writer JavaDoc writer )
58         throws IOException JavaDoc {
59         char[] buf = new char[DEFAULT_BUFFER_SIZE];
60         flow( reader, writer, buf );
61     }
62
63     /**
64      * Read input from input stream and write it to output stream
65      * until there is no more input from input stream.
66      *
67      * @param is input stream the input stream to read from.
68      * @param os output stream the output stream to write to.
69      * @param buf the byte array to use as a buffer
70      */

71     public static void flow( InputStream JavaDoc is, OutputStream JavaDoc os, byte[] buf )
72         throws IOException JavaDoc {
73         int numRead;
74         while ( (numRead = is.read(buf) ) >= 0) {
75             os.write(buf, 0, numRead);
76         }
77     }
78
79     /**
80      * @see #flow( java.io.InputStream, java.io.OutputStream, byte[] )
81      */

82     public static void flow( InputStream JavaDoc is, OutputStream JavaDoc os )
83         throws IOException JavaDoc {
84         byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
85         flow( is, os, buf );
86     }
87 }
88
Popular Tags