Java InputStream to OutputStream copyStream(Reader input, Writer output)

Here you can find the source of copyStream(Reader input, Writer output)

Description

Coping data(character type) from input reader to output writer.

License

Open Source License

Parameter

Parameter Description
input source of data.
output destination of data.

Exception

Parameter Description
IOException if any errors occur during copying process.

Declaration

public static void copyStream(Reader input, Writer output)
        throws IOException 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    /**/*  ww  w. j av a  2s  .com*/
     * Coping data(character type) from {@code input} reader to {@code output} writer.
     *
     * @param input source of data.
     * @param output destination of data.
     * @throws IOException if any errors occur during copying process.
     */
    public static void copyStream(Reader input, Writer output)
            throws IOException {
        copyStream(input, output, 4096);
    }

    /**
     * Coping data(binary type) from {@code input} stream to {@code output} stream.
     *
     * @param input input source of data.
     * @param output destination of data.
     * @throws IOException if any errors occur during copying process.
     */
    public static void copyStream(InputStream input, OutputStream output)
            throws IOException {
        copyStream(input, output, 4096);
    }

    /**
     * Coping data(character type) from {@code input} reader to {@code output} writer.
     *
     * @param input input source of data.
     * @param output destination of data.
     * @param buffersize size of buffer with is using for data copy.
     * @throws IOException if any errors occur during copying process.
     * @throws IllegalArgumentException if {@code buffersize} less then 1.
     */
    public static void copyStream(Reader input, Writer output,
            int buffersize) throws IOException, IllegalArgumentException {
        if (buffersize < 1) {
            throw new IllegalArgumentException(
                    "buffersize must be greater than 0");
        }
        char[] buffer = new char[buffersize];
        int n;
        while ((n = input.read(buffer)) >= 0) {
            output.write(buffer, 0, n);
        }
    }

    /**
     * Coping data(binary type) from {@code input} stream to {@code output} stream.
     *
     * @param input input source of data.
     * @param output destination of data.
     * @param buffersize size of buffer with is using for data copy.
     * @throws IOException if any errors occur during copying process.
     * @throws IllegalArgumentException if {@code buffersize} less then 1.
     */
    public static void copyStream(InputStream input, OutputStream output,
            int buffersize) throws IOException, IllegalArgumentException {
        if (buffersize < 1) {
            throw new IllegalArgumentException(
                    "buffersize must be greater than 0");
        }
        byte[] buffer = new byte[buffersize];
        int n;
        while ((n = input.read(buffer)) >= 0) {
            output.write(buffer, 0, n);
        }
    }
}

Related

  1. copyStream(java.io.InputStream in, java.io.OutputStream out)
  2. copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
  3. copyStream(java.io.InputStream src, java.io.OutputStream dest)
  4. copyStream(OutputStream out, InputStream in, int bufsz)
  5. copyStream(OutputStream outStream, InputStream inStream)
  6. copyStreamAndClose(InputStream is, OutputStream os)
  7. copyStreamAndClose(InputStream is, OutputStream os)
  8. copyStreamBounded(InputStream in, OutputStream out, long length)
  9. copyStreamContent(InputStream inputStream, OutputStream outputStream)