Here you can find the source of toString(InputStream is, String charset)
public static String toString(InputStream is, String charset) throws IOException
//package com.java2s; /*/*w ww . j a va 2 s .com*/ * Hello Minecraft!. * Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see {http://www.gnu.org/licenses/}. */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; public class Main { /** * Max buffer size downloading. */ public static final int MAX_BUFFER_SIZE = 4096; public static String toString(InputStream is) throws IOException { return readFully(is).toString(); } public static String toString(InputStream is, String charset) throws IOException { return readFully(is).toString(charset); } public static String toString(InputStream is, Charset charset) throws IOException { return readFully(is).toString(charset.name()); } public static ByteArrayOutputStream readFully(InputStream stream) throws IOException { byte[] data = new byte[MAX_BUFFER_SIZE]; ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream(); int len; do { len = stream.read(data); if (len <= 0) continue; entryBuffer.write(data, 0, len); } while (len != -1); return entryBuffer; } public static void write(byte[] data, OutputStream output) throws IOException { if (data != null) output.write(data); } public static void write(String data, OutputStream output, String encoding) throws IOException { if (data != null) output.write(data.getBytes(encoding)); } }