Android InputStream Copy copy(InputStream in, OutputStream out)

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

Description

Copies the content of the InputStream to the OutputStream.

Declaration

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

Method Source Code

//package com.java2s;

import java.io.Closeable;

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

import java.io.OutputStream;

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

    /**//from   w ww.j a  v a2s  .c o m
     * Copies the content of the InputStream to the OutputStream.
     * Both streams are closed automatically.
     */
    public static long copy(InputStream in, OutputStream out)
            throws IOException {
        long filesize = 0;
        try {
            byte[] buffer = new byte[BUFF_SIZE];
            int bytesRead = in.read(buffer);
            while (bytesRead != -1) {
                filesize += bytesRead;
                out.write(buffer, 0, bytesRead);
                bytesRead = in.read(buffer);
            }
        } finally {
            closeQuitely(out);
            closeQuitely(in);
        }
        return filesize;
    }

    public static void closeQuitely(Closeable c) {
        if (c == null)
            return;

        try {
            c.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

Related

  1. CopyStream(InputStream is, OutputStream os)
  2. copy(InputStream in, BufferedOutputStream out)
  3. copy(InputStream in, OutputStream out)
  4. copy(InputStream in, OutputStream out)
  5. copy(InputStream input, OutputStream output)
  6. copy(InputStream input, Writer output)
  7. copy(InputStream input, Writer output, String encoding)
  8. copy(InputStream inputStream, OutputStream outputStream)