Java InputStream Read by Charset toString(InputStream inputStream, Charset charset)

Here you can find the source of toString(InputStream inputStream, Charset charset)

Description

Read an InputStream into a String .

License

Open Source License

Parameter

Parameter Description
inputStream input stream
charset charset to use

Exception

Parameter Description
IOException if failed to read from stream

Return

output string

Declaration

public static String toString(InputStream inputStream, Charset charset) throws IOException 

Method Source Code


//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();
    }
}

Related

  1. toString(InputStream in, Charset charset)
  2. toString(InputStream in, Charset charset)
  3. toString(InputStream in, Charset charset)
  4. toString(InputStream input, Charset charset)
  5. toString(InputStream input, String charset)
  6. toString(InputStream is, Charset charset)
  7. toString(InputStream is, String charset)
  8. toString(InputStream source, Charset charset)
  9. toString(InputStream stream, Charset chs)