01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2003 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * ========================================================================
19: */
20: package org.apache.cactus.internal.util;
21:
22: import java.io.BufferedReader;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.InputStreamReader;
26:
27: /**
28: * Various utility methods for manipulating IO streams.
29: *
30: * @version $Id: IoUtil.java 238991 2004-05-22 11:34:50Z vmassol $
31: */
32: public class IoUtil {
33: /**
34: * @see #getText(InputStream, String)
35: */
36: public static String getText(InputStream theStream)
37: throws IOException {
38: return getText(theStream, null);
39: }
40:
41: /**
42: * Read all data in an Input stream and return them as a
43: * <code>String</code> object.
44: *
45: * @param theStream the input stream from which to read the data
46: * @param theCharsetName the charset name with which to read the data
47: * @return the string representation of the data
48: * @throws IOException if an error occurs during the read of data
49: */
50: public static String getText(InputStream theStream,
51: String theCharsetName) throws IOException {
52: StringBuffer sb = new StringBuffer();
53:
54: BufferedReader input;
55: if (theCharsetName == null) {
56: input = new BufferedReader(new InputStreamReader(theStream));
57: } else {
58: input = new BufferedReader(new InputStreamReader(theStream,
59: theCharsetName));
60: }
61:
62: char[] buffer = new char[2048];
63: int nb;
64:
65: while (-1 != (nb = input.read(buffer, 0, 2048))) {
66: sb.append(buffer, 0, nb);
67: }
68:
69: input.close();
70:
71: return sb.toString();
72: }
73: }
|