Java InputStream Copy copyStreamToWriter(InputStream in, Writer out, String encoding, boolean close)

Here you can find the source of copyStreamToWriter(InputStream in, Writer out, String encoding, boolean close)

Description

Copies an InputStream to a Writer.

License

Apache License

Parameter

Parameter Description
in The input byte stream of data.
out The Writer to receive the streamed data as characters.
encoding The encoding used in the byte stream.
close true if the Reader and OutputStream should be closed after the completion.

Exception

Parameter Description
IOException If an underlying I/O Exception occurs.

Declaration

public static void copyStreamToWriter(InputStream in, Writer out, String encoding, boolean close)
        throws IOException 

Method Source Code

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

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

import java.io.Reader;
import java.io.Writer;

public class Main {
    /**/*from www  .j a  va2s .co m*/
     * Copies an InputStream to a Writer.
     * @param in The input byte stream of data.
     * @param out The Writer to receive the streamed data as characters.
     * @param encoding The encoding used in the byte stream.
     * @param close true if the Reader and OutputStream should be closed after
     *        the completion.
     * @throws IOException If an underlying I/O Exception occurs.
     */
    public static void copyStreamToWriter(InputStream in, Writer out, String encoding, boolean close)
            throws IOException {
        InputStreamReader reader = new InputStreamReader(in, encoding);
        copyReaderToWriter(reader, out, close);
    }

    /**
     * Copies the Reader to the Writer.
     * @param input The input characters.
     * @param output The destination of the characters.
     * @param close true if the reader and writer should be closed after the
     *        operation is completed.
     * @throws IOException If an underlying I/O problem occured.
     */
    public static void copyReaderToWriter(Reader input, Writer output, boolean close) throws IOException {
        try {
            BufferedReader in = bufferInput(input);
            BufferedWriter out = bufferOutput(output);
            int ch = in.read();
            while (ch != -1) {
                out.write(ch);
                ch = in.read();
            }
            out.flush();
            out.close();
        } finally {
            if (close) {
                input.close();
                output.close();
            }
        }
    }

    /**
     * Wraps the Reader in a BufferedReaderm unless it already is a
     * BufferedReader.
     * @param reader The Reader to check and possibly wrap.
     * @return A BufferedReader.
     */
    private static BufferedReader bufferInput(Reader reader) {
        BufferedReader in;
        if (reader instanceof BufferedReader) {
            in = (BufferedReader) reader;
        } else {
            in = new BufferedReader(reader);
        }
        return in;
    }

    /**
     * Wraps the Writer in a BufferedWriter, unless it already is a
     * BufferedWriter.
     * @param writer The Writer to check and possibly wrap.
     * @return A BufferedWriter.
     */
    private static BufferedWriter bufferOutput(Writer writer) {
        BufferedWriter out;
        if (writer instanceof BufferedWriter) {
            out = (BufferedWriter) writer;
        } else {
            out = new BufferedWriter(writer);
        }
        return out;
    }
}

Related

  1. copyStreamToBytes(InputStream inputStream)
  2. copyStreamToString(InputStream input)
  3. copyStreamToString(InputStream input)
  4. copyStreamToString(InputStream input)
  5. copyStreamToString(InputStream inputStream, String encoding)
  6. copyStreamToWriter(InputStream inStream, Writer writer)