Java InputStream to OutputStream copyStream(InputStream in, OutputStream out)

Here you can find the source of copyStream(InputStream in, OutputStream out)

Description

Copies data from one stream to another, until the input stream ends.

License

Open Source License

Parameter

Parameter Description
in Stream to read from
out Stream to write to

Exception

Parameter Description
IOException if there is some problem reading or writing

Return

The number of bytes copied

Declaration

public static long copyStream(InputStream in, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.io.OutputStream;

public class Main {
    private static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;

    /**//w  w w. j a  v a2 s .  c o  m
     * Copies data from one stream to another, until the input stream ends. Uses an
     * internal 16K buffer.
     * 
     * @param in Stream to read from
     * @param out Stream to write to
     * @return The number of bytes copied
     * @throws IOException if there is some problem reading or writing
     */
    public static long copyStream(InputStream in, OutputStream out) throws IOException {
        long total = 0;
        int len;
        byte[] buffer = new byte[DEFAULT_READ_BUFFER_SIZE];

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
            total += len;
        }

        return total;
    }
}

Related

  1. copyStream(InputStream in, OutputStream out)
  2. copyStream(InputStream in, OutputStream out)
  3. copyStream(InputStream in, OutputStream out)
  4. copyStream(InputStream in, OutputStream out)
  5. copyStream(InputStream in, OutputStream out)
  6. copyStream(InputStream in, OutputStream out)
  7. copyStream(InputStream in, OutputStream out)
  8. copyStream(InputStream in, OutputStream out)
  9. copyStream(InputStream in, OutputStream out)