Android InputStream Copy 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  ava 2s.  c om*/
     * 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. copyContents(InputStream in, OutputStream out)
  2. copyFile(InputStream from, String target)
  3. copyFile(InputStream in, File out)
  4. copyFile(InputStream in, OutputStream out)
  5. copyFile(InputStream input, OutputStream output)
  6. writeStream(InputStream input, OutputStream output)
  7. CopyStream(InputStream is, OutputStream os)
  8. CopyStream(InputStream is, OutputStream os)
  9. copyStream(InputStream src, OutputStream dest)