Java InputStream Read by Charset streamString(InputStream in, boolean closeIn, String charset)

Here you can find the source of streamString(InputStream in, boolean closeIn, String charset)

Description

stream String

License

GNU General Public License

Declaration

public static String streamString(InputStream in, boolean closeIn, String charset) throws IOException 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 * Version: GPL 2.0// w  ww .  ja v a  2  s.  c o m
 *
 * The contents of this file are subject to the GNU General Public
 * License Version 2 or later (the "GPL").
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *   MiniG.org project members
 *
 * ***** END LICENSE BLOCK ***** */

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.nio.charset.Charset;

public class Main {
    private static final int BUFF_SIZE = 100000;

    public static String streamString(InputStream in, boolean closeIn) throws IOException {
        return new String(streamBytes(in, closeIn), "utf-8");
    }

    public static String streamString(InputStream in, boolean closeIn, String charset) throws IOException {
        return new String(streamBytes(in, closeIn), Charset.forName(charset));
    }

    public static byte[] streamBytes(InputStream in, boolean closeIn) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        transfer(in, out, closeIn);
        return out.toByteArray();
    }

    /**
     * Fast stream transfer method
     * 
     * @param in
     * @param out
     * @throws IOException
     */
    public static void transfer(InputStream in, OutputStream out, boolean closeIn) throws IOException {
        final byte[] buffer = new byte[BUFF_SIZE];

        try {
            while (true) {
                int amountRead = in.read(buffer);
                if (amountRead == -1) {
                    break;
                }
                out.write(buffer, 0, amountRead);
            }
        } finally {
            if (closeIn) {
                in.close();
            }
            out.flush();
            out.close();
        }
    }
}

Related

  1. isCharsetMisInterpreted(String input, String encoding)
  2. lines(InputStream in, Charset cs)
  3. loadInputStream(InputStream in, Charset cs)
  4. processSubstitute(CharBuffer cb, String replacement, boolean endOfInput, String outputCharset, OutputStream os)
  5. stream2Bytes(InputStream is, Charset charset)
  6. streamToString(final InputStream is, final Charset charset)
  7. streamToString(InputStream inputStream, Charset encoding)
  8. stringFromStream(InputStream in, Charset cs)
  9. toInputStream(final String string, final Charset charset)