01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.catalina.util;
17:
18: import java.io.InputStream;
19: import java.io.IOException;
20: import java.io.OutputStream;
21: import java.io.Reader;
22: import java.io.Writer;
23:
24: /**
25: * Contains commonly needed I/O-related methods
26: *
27: * @author Dan Sandberg
28: */
29: public class IOTools {
30: protected final static int DEFAULT_BUFFER_SIZE = 4 * 1024; //4k
31:
32: //Ensure non-instantiability
33: private IOTools() {
34: }
35:
36: /**
37: * Read input from reader and write it to writer until there is no more
38: * input from reader.
39: *
40: * @param reader the reader to read from.
41: * @param writer the writer to write to.
42: * @param buf the char array to use as a bufferx
43: */
44: public static void flow(Reader reader, Writer writer, char[] buf)
45: throws IOException {
46: int numRead;
47: while ((numRead = reader.read(buf)) >= 0) {
48: writer.write(buf, 0, numRead);
49: }
50: }
51:
52: /**
53: * @see #flow( Reader, Writer, char[] )
54: */
55: public static void flow(Reader reader, Writer writer)
56: throws IOException {
57: char[] buf = new char[DEFAULT_BUFFER_SIZE];
58: flow(reader, writer, buf);
59: }
60:
61: /**
62: * Read input from input stream and write it to output stream
63: * until there is no more input from input stream.
64: *
65: * @param is input stream the input stream to read from.
66: * @param os output stream the output stream to write to.
67: * @param buf the byte array to use as a buffer
68: */
69: public static void flow(InputStream is, OutputStream os, byte[] buf)
70: throws IOException {
71: int numRead;
72: while ((numRead = is.read(buf)) >= 0) {
73: os.write(buf, 0, numRead);
74: }
75: }
76:
77: /**
78: * @see #flow( java.io.InputStream, java.io.OutputStream, byte[] )
79: */
80: public static void flow(InputStream is, OutputStream os)
81: throws IOException {
82: byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
83: flow(is, os, buf);
84: }
85: }
|