01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
20: import java.io.IOException;
21: import java.io.OutputStream;
22: import java.io.Reader;
23: import java.io.Writer;
24:
25: /**
26: * Contains commonly needed I/O-related methods
27: *
28: * @author Dan Sandberg
29: */
30: public class IOTools {
31: protected final static int DEFAULT_BUFFER_SIZE = 4 * 1024; //4k
32:
33: //Ensure non-instantiability
34: private IOTools() {
35: }
36:
37: /**
38: * Read input from reader and write it to writer until there is no more
39: * input from reader.
40: *
41: * @param reader the reader to read from.
42: * @param writer the writer to write to.
43: * @param buf the char array to use as a bufferx
44: */
45: public static void flow(Reader reader, Writer writer, char[] buf)
46: throws IOException {
47: int numRead;
48: while ((numRead = reader.read(buf)) >= 0) {
49: writer.write(buf, 0, numRead);
50: }
51: }
52:
53: /**
54: * @see #flow( Reader, Writer, char[] )
55: */
56: public static void flow(Reader reader, Writer writer)
57: throws IOException {
58: char[] buf = new char[DEFAULT_BUFFER_SIZE];
59: flow(reader, writer, buf);
60: }
61:
62: /**
63: * Read input from input stream and write it to output stream
64: * until there is no more input from input stream.
65: *
66: * @param is input stream the input stream to read from.
67: * @param os output stream the output stream to write to.
68: * @param buf the byte array to use as a buffer
69: */
70: public static void flow(InputStream is, OutputStream os, byte[] buf)
71: throws IOException {
72: int numRead;
73: while ((numRead = is.read(buf)) >= 0) {
74: os.write(buf, 0, numRead);
75: }
76: }
77:
78: /**
79: * @see #flow( java.io.InputStream, java.io.OutputStream, byte[] )
80: */
81: public static void flow(InputStream is, OutputStream os)
82: throws IOException {
83: byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
84: flow(is, os, buf);
85: }
86: }
|