Here you can find the source of read(InputStream in, Charset charset)
Parameter | Description |
---|---|
in | InputStream to read from. |
charset | name of supported charset to use |
Parameter | Description |
---|---|
IOException | in case of IO error |
IllegalArgumentException | if stream is null |
public static String read(InputStream in, Charset charset) throws IOException
//package com.java2s; //License from project: LGPL import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class Main { /**/* www .j a v a 2s . c o m*/ * Reads contents of the input stream fully and returns it as String. Sets UTF-8 encoding internally. * * @param in InputStream to read from. * @return contents of the input stream fully as String. * @throws IOException in case of IO error * @throws IllegalArgumentException if stream is null */ public static String read(InputStream in) throws IOException { return read(in, StandardCharsets.UTF_8); } /** * Reads contents of the input stream fully and returns it as String. * * @param in InputStream to read from. * @param charset name of supported charset to use * @return contents of the input stream fully as String. * @throws IOException in case of IO error * @throws IllegalArgumentException if stream is null */ public static String read(InputStream in, Charset charset) throws IOException { if (in == null) throw new IllegalArgumentException("input stream cannot be null"); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[1024]; StringBuilder sb = new StringBuilder(); for (int x = reader.read(buffer); x != -1; x = reader.read(buffer)) { sb.append(buffer, 0, x); } return sb.toString(); } }