Here you can find the source of readAll(InputStream inputStream, Charset charset)
Parameter | Description |
---|---|
inputStream | Source stream |
charset | Convert bytes to chars according to given Charset |
Parameter | Description |
---|---|
IOException | an exception |
public static String readAll(InputStream inputStream, Charset charset) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.Charset; public class Main { private static final int DEFAULT_BUFFER_SIZE = 4096; private static final String DEFAULT_CHARSET = "UTF-8"; /**/*w ww . j av a 2 s . com*/ * Read all stream byte data into {@link String} * @param inputStream Source stream * @param charset Convert bytes to chars according to given {@link Charset} * @return Empty {@link String} if there was no data in stream * @throws IOException */ public static String readAll(InputStream inputStream, Charset charset) throws IOException { return readAll(inputStream, charset.name()); } /** * Read all stream byte data into {@link String} * @param inputStream Source stream * @param charset Convert bytes to chars according to given * @return Empty {@link String} if there was no data in stream * @throws IOException */ public static String readAll(InputStream inputStream, String charset) throws IOException { try (InputStreamReader streamReader = new InputStreamReader(inputStream, charset)) { return readAll(streamReader); } } /** * Read all stream byte data into {@link String} * @param inputStream Source stream * @return Empty {@link String} if there was no data in stream * @throws IOException */ public static String readAll(InputStream inputStream) throws IOException { return readAll(inputStream, DEFAULT_CHARSET); } /** * Read all chars into String * @param streamReader Input stream reader * @return Empty {@link String} if there was no data in stream * @throws IOException */ public static String readAll(InputStreamReader streamReader) throws IOException { StringWriter result = new StringWriter(); copy(streamReader, result); return result.toString(); } private static long copy(Reader reader, Writer writer) throws IOException { return copy(reader, writer, new char[DEFAULT_BUFFER_SIZE]); } private static long copy(Reader reader, Writer writer, char[] buffer) throws IOException { assert buffer != null; assert buffer.length > 0; long result = 0; int read = reader.read(buffer); while (read > 0) { writer.write(buffer, 0, read); result += read; read = reader.read(buffer); } return result; } }