Here you can find the source of readAsString(InputStream in, Charset charset)
public static String readAsString(InputStream in, Charset charset) throws IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; public class Main { private static final int BUFFER_SIZE = 4096; public static String readAsString(InputStream in) throws IOException { return new String(readAsBytes(in)); }/*from w ww . j a v a2 s . co m*/ public static String readAsString(InputStream in, long length) throws IOException { return new String(readAsBytes(in, length)); } public static String readAsString(InputStream in, Charset charset) throws IOException { return new String(readAsBytes(in), charset); } public static String readAsString(InputStream in, long length, Charset charset) throws IOException { return new String(readAsBytes(in, length), charset); } public static byte[] readAsBytes(InputStream in, long maxSize) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { int byteCount = 0; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; while ((byteCount < maxSize) && (bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); return out.toByteArray(); } finally { try { if (null != in) in.close(); } catch (IOException ex) { } try { if (null != out) out.close(); } catch (IOException ex) { } } } public static byte[] readAsBytes(InputStream in) throws IOException { return readAsBytes(in, Long.MAX_VALUE); } }