Here you can find the source of copyToString(InputStream in, Charset charset)
Parameter | Description |
---|---|
in | the InputStream to copy from |
Parameter | Description |
---|---|
IOException | in case of I/O errors |
public static String copyToString(InputStream in, Charset charset) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; public class Main { public static final int BUFFER_SIZE = 4096; /**//from ww w . j a v a 2s .co m * Copy the contents of the given InputStream into a String. * Leaves the stream open when done. * @param in the InputStream to copy from * @return the String that has been copied to * @throws IOException in case of I/O errors */ public static String copyToString(InputStream in, Charset charset) throws IOException { StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } return out.toString(); } }