Java Text File Read by Charset readInputStream(InputStream stream, Charset cs)

Here you can find the source of readInputStream(InputStream stream, Charset cs)

Description

read Input Stream

License

Apache License

Declaration

public static String readInputStream(InputStream stream, Charset cs) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

public class Main {
    public static String readInputStream(InputStream stream, Charset cs) throws IOException {
        // No real need to close the BufferedReader/InputStreamReader
        // as they're only wrapping the stream

        try (final Reader reader = new BufferedReader(new InputStreamReader(stream, cs))) {
            final StringBuilder builder = new StringBuilder();
            final char[] buffer = new char[8192];
            int read;

            while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                builder.append(buffer, 0, read);
            }//from w  ww .j  a v  a2  s. c o m

            return builder.toString();
        }
    }
}

Related

  1. readFileToLines(String inFile, String inCharset)
  2. readFileToList(String filePath, String charsetName)
  3. readFileToString(String filename, Charset encoding)
  4. readFileToString(String path, Charset charset)
  5. readFromInputStream(InputStream stream, Charset charset)
  6. readInputStreamToString(InputStream instream, Charset charset)
  7. readLineFromStream(InputStream is, StringBuffer buffer, CharsetDecoder decoder)
  8. readLines(InputStream in, Charset cs)
  9. readLines(InputStream input, Charset charset)