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

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

Description

to String

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
/*  Copyright (c) 2010 Xiaoyun Zhu
 * /*from w w  w. j  a v a2 s . c o  m*/
 *  Permission is hereby granted, free of charge, to any person obtaining a copy  
 *  of this software and associated documentation files (the "Software"), to deal  
 *  in the Software without restriction, including without limitation the rights  
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 *  copies of the Software, and to permit persons to whom the Software is  
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in  
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
 *  THE SOFTWARE.  
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 1024 * 64;

    public static String toString(InputStream in, Charset charset) throws IOException {
        ByteArrayOutputStream bais = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
        copy(in, bais, charset);
        return new String(bais.toByteArray());
    }

    public static void copy(byte[] input, OutputStream output) throws IOException {
        output.write(input);
    }

    public static void copy(byte[] input, Writer output) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(input);
        copy(in, output);
    }

    public static void copy(byte[] input, Writer output, Charset charset) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(input);
        copy(in, output, charset);
    }

    public static long copy(Reader input, Writer output) throws IOException {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    public static void copy(InputStream input, Writer output) throws IOException {
        InputStreamReader in = new InputStreamReader(input);
        copy(in, output);
    }

    public static void copy(InputStream input, Writer output, Charset charset) throws IOException {
        InputStreamReader in = new InputStreamReader(input, charset);
        copy(in, output);
    }

    public static void copy(InputStream input, OutputStream output, Charset charset) throws IOException {
        InputStreamReader in = new InputStreamReader(input, charset);
        copy(in, output);
    }

    public static void copy(Reader input, OutputStream output) throws IOException {
        OutputStreamWriter out = new OutputStreamWriter(output);
        copy(input, out);
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
        out.flush();
    }

    public static void copy(String input, OutputStream output) throws IOException {
        StringReader in = new StringReader(input);
        OutputStreamWriter out = new OutputStreamWriter(output);
        copy(in, out);
        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
        out.flush();
    }

    public static void copy(String input, Writer output) throws IOException {
        output.write(input);
    }

    public static long copy(InputStream input, OutputStream output) throws IOException {
        final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. toInputStream(final String string, final Charset charset)
  2. toInputStream(String input, Charset charset)
  3. toInputStream(String input, Charset cs)
  4. toString(final InputStream input, final Charset encoding)
  5. toString(InputStream in, Charset charset)
  6. toString(InputStream in, Charset charset)
  7. toString(InputStream in, Charset charset)
  8. toString(InputStream in, Charset charset)
  9. toString(InputStream input, Charset charset)