2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

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