Here you can find the source of readInputStreamToString(InputStream instream, Charset charset)
Parameter | Description |
---|---|
instream | to be read |
charset | is encoding of input stream's bytes |
Parameter | Description |
---|---|
IOException | when reading fails |
public static String readInputStreamToString(InputStream instream, Charset charset) throws IOException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; public class Main { /**/*from w w w .j a va2 s .c om*/ * Form a string from the contents of {@code instream} with charset * {@code charset}. * @param instream to be read * @param charset is encoding of input stream's bytes * @return String entire instream contents * @throws IOException when reading fails */ public static String readInputStreamToString(InputStream instream, Charset charset) throws IOException { return new String(readInputStreamToByteArray(instream), charset); } /** * Read the contents of {@code is} into a byte array. * @param instream to be read * @return byte[] entire instream bytes * @throws IOException when reading fails */ public static byte[] readInputStreamToByteArray(InputStream instream) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); copyStream(instream, os); return os.toByteArray(); } /** * Copy contents of {@code in} to {@code out}. * @param in is source of bytes * @param out is destination for bytes * @throws IOException if reading or writing fails */ public static void copyStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[8192]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } out.flush(); } }