Here you can find the source of getStreamAsString(InputStream source, Charset charset)
public static String getStreamAsString(InputStream source, Charset charset) throws IOException
//package com.java2s; /*//www.ja v a 2 s .c om * 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.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.Charset; public class Main { /** * Transforms the contents of the given {@link InputStream} into a String object. * * @param source * an {@link InputStream} the contents of which shall be transformed into a String * @return a String containing the contents of the given {@link InputStream} * @throws IOException * if an error occurs during the transformation */ public static String getStreamAsString(InputStream source) throws IOException { String result = getReaderAsString(new InputStreamReader(source)); source.close(); return result; } public static String getStreamAsString(InputStream source, Charset charset) throws IOException { String result = getReaderAsString(new InputStreamReader(source, charset)); source.close(); return result; } public static String getStreamAsString(InputStream source, String charsetName) throws IOException { String result = getReaderAsString(new InputStreamReader(source, charsetName)); source.close(); return result; } /** * 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(); } }