Java InputStream Read by Charset copyToString(InputStream in, Charset charset)

Here you can find the source of copyToString(InputStream in, Charset charset)

Description

Copy the contents of the given InputStream into a String.

License

Apache License

Parameter

Parameter Description
in the InputStream to copy from

Exception

Parameter Description
IOException in case of I/O errors

Return

the String that has been copied to

Declaration

public static String copyToString(InputStream in, Charset charset) throws IOException 

Method Source Code

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

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.nio.charset.Charset;

public class Main {
    public static final int BUFFER_SIZE = 4096;

    /**//from  ww  w  . j a  v  a  2s  .co m
     * Copy the contents of the given InputStream into a String.
     * Leaves the stream open when done.
     * @param in the InputStream to copy from
     * @return the String that has been copied to
     * @throws IOException in case of I/O errors
     */
    public static String copyToString(InputStream in, Charset charset) throws IOException {
        StringBuilder out = new StringBuilder();
        InputStreamReader reader = new InputStreamReader(in, charset);
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = reader.read(buffer)) != -1) {
            out.append(buffer, 0, bytesRead);
        }
        return out.toString();
    }
}

Related

  1. consume(InputStream stream, Charset encoding)
  2. convertEncoding(Charset output_charset, String input_string)
  3. convertFromUnicode(String input, String targetCharset)
  4. convertStreamToString(final InputStream is, final Charset charset)
  5. convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)
  6. createInput(String s, String charsetName)
  7. createZipInputStream(InputStream inStream, Charset charset)
  8. forceEncoding(String inputString, String targetCharset)
  9. fromUnicode(String charset, String input)