Java Reader Read getReaderAsString(Reader source)

Here you can find the source of getReaderAsString(Reader source)

Description

Transforms the contents of the given Reader into a String object.

License

Apache License

Parameter

Parameter Description
source an Reader the contents of which shall be transformed into a String

Exception

Parameter Description
IOException if an error occurs during the transformation

Return

a String containing the contents of the given

Declaration

public static String getReaderAsString(Reader source) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w w  w.jav  a 2 s  .co  m*/
 * Copyright (C) 2007 Roland Krueger
 * 
 * Created on 26.03.2010
 * 
 * Author: Roland Krueger (www.rolandkrueger.info)
 * 
 * This file is part of RoKlib.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.IOException;

import java.io.Reader;

public class Main {
    /**
     * Transforms the contents of the given {@link Reader} into a String object.
     * 
     * @param source
     *          an {@link Reader} the contents of which shall be transformed into a String
     * @return a String containing the contents of the given {@link Reader}
     * @throws IOException
     *           if an error occurs during the transformation
     */
    public static String getReaderAsString(Reader source) throws IOException {
        StringBuilder buffer = new StringBuilder();

        char[] charBuffer = new char[1024];
        int read;
        while ((read = source.read(charBuffer)) != -1) {
            buffer.append(charBuffer, 0, read);
        }

        source.close();
        return buffer.toString();
    }
}

Related

  1. getReaderContentAsString(Reader reader)
  2. getReaderContents(Reader reader)
  3. load(InputStream input, int bufsize)
  4. loadChars(Reader in)