KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > IoUtil


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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 JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26
27 /**
28  * Various utility methods for manipulating IO streams.
29  *
30  * @version $Id: IoUtil.java,v 1.1 2004/05/22 11:34:48 vmassol Exp $
31  */

32 public class IoUtil
33 {
34     /**
35      * @see #getText(InputStream, String)
36      */

37     public static String JavaDoc getText(InputStream JavaDoc theStream) throws IOException JavaDoc
38     {
39         return getText(theStream, null);
40     }
41
42     /**
43      * Read all data in an Input stream and return them as a
44      * <code>String</code> object.
45      *
46      * @param theStream the input stream from which to read the data
47      * @param theCharsetName the charset name with which to read the data
48      * @return the string representation of the data
49      * @throws IOException if an error occurs during the read of data
50      */

51     public static String JavaDoc getText(InputStream JavaDoc theStream, String JavaDoc theCharsetName)
52         throws IOException JavaDoc
53     {
54         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55
56         BufferedReader JavaDoc input;
57         if (theCharsetName == null)
58         {
59             input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(theStream));
60         }
61         else
62         {
63             input = new BufferedReader JavaDoc(
64                new InputStreamReader JavaDoc(theStream, theCharsetName));
65         }
66         
67         char[] buffer = new char[2048];
68         int nb;
69
70         while (-1 != (nb = input.read(buffer, 0, 2048)))
71         {
72             sb.append(buffer, 0, nb);
73         }
74
75         input.close();
76
77         return sb.toString();
78     }
79 }
80
Popular Tags