Here you can find the source of toString(InputStream inputStream, Charset charset)
Parameter | Description |
---|---|
inputStream | input stream |
charset | charset to use |
Parameter | Description |
---|---|
IOException | if failed to read from stream |
public static String toString(InputStream inputStream, Charset charset) throws IOException
//package com.java2s; /*/*from ww w . j a va 2 s .co m*/ * ==================================================================== * JMH utils :: profilers :: utils * ==================================================================== * Copyright (C) 2014 Julien Nicoulaud <julien.nicoulaud@gmail.com> * ==================================================================== * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * ==================================================================== */ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; public class Main { private static final int BUFFER_SIZE = 4 * 1024; /** * Read an {@link InputStream} into a {@link String}. * * @param inputStream input stream * @return output string * @throws IOException if failed to read from stream */ public static String toString(InputStream inputStream) throws IOException { return toString(inputStream, Charset.defaultCharset()); } /** * Read an {@link InputStream} into a {@link String}. * * @param inputStream input stream * @param charset charset to use * @return output string * @throws IOException if failed to read from stream */ public static String toString(InputStream inputStream, Charset charset) throws IOException { final StringBuilder builder = new StringBuilder(); final InputStreamReader reader = new InputStreamReader(inputStream, charset); final char[] buffer = new char[BUFFER_SIZE]; int length; while ((length = reader.read(buffer)) != -1) builder.append(buffer, 0, length); return builder.toString(); } }