Java Text File Read by Charset readAll(InputStream inputStream, Charset charset)

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

Description

Read all stream byte data into String

License

Open Source License

Parameter

Parameter Description
inputStream Source stream
charset Convert bytes to chars according to given Charset

Exception

Parameter Description
IOException an exception

Return

Empty if there was no data in stream

Declaration

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

Method Source Code


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

Related

  1. read(InputStream _is, CharsetDecoder _decoder)
  2. read(InputStream in, Charset charset)
  3. read(InputStream in, Charset cs, Appendable appendable, int capacity)
  4. read(InputStream in, String charsetName)
  5. readAll(InputStream in, Charset charset)
  6. readAll(InputStream inputStream, Charset encoding)
  7. readAllLines(File file, Charset cs, String newLineDelimiter)
  8. readAllLines(InputStream stream, Charset charset)
  9. readAllLines(Path path, Charset cs)