Java InputStream Read by Charset getContentFromInputStream(InputStream in, String charset)

Here you can find the source of getContentFromInputStream(InputStream in, String charset)

Description

get string content form inputStream with given charset

License

Open Source License

Parameter

Parameter Description
in a parameter
charset a parameter

Declaration

public static String getContentFromInputStream(InputStream in, String charset) 

Method Source Code


//package com.java2s;
import java.io.InputStream;
import java.nio.charset.Charset;

public class Main {
    /**//from   w  w w.j ava2  s .c om
     * <p>
     * get string content form inputStream with default charset ant return.
     * </p>
     *
     * @param in
     * @return
     */
    public static String getContentFromInputStream(InputStream in) {
        return getContentFromInputStream(in, Charset.defaultCharset().name());
    }

    /**
     * <p>
     * get string content form inputStream with given charset
     * </p>
     *
     * @param in
     * @param charset
     * @return
     */
    public static String getContentFromInputStream(InputStream in, String charset) {
        StringBuffer dist = new StringBuffer();
        byte[] data = new byte[1024];
        int readNum = -1;
        try {
            while ((readNum = in.read(data)) != -1) {
                dist.append(new String(data, 0, readNum, charset));
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dist.toString();
    }
}

Related

  1. copyToString(InputStream in, Charset charset)
  2. createInput(String s, String charsetName)
  3. createZipInputStream(InputStream inStream, Charset charset)
  4. forceEncoding(String inputString, String targetCharset)
  5. fromUnicode(String charset, String input)
  6. getHeaderLen(FileInputStream in, Charset encoding)
  7. getInputCharset()
  8. getStreamAsString(InputStream source, Charset charset)
  9. getText(InputStream stream, Charset charset)