Copy the contents of an InputStream into an OutputStream. - Android File Input Output

Android examples for File Input Output:Copy File

Description

Copy the contents of an InputStream into an OutputStream.

Demo Code


//package com.java2s;

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

public class Main {
    /**//from ww  w .j a  va  2  s.c  om
     * Copy the contents of an InputStream into an OutputStream.
     * 
     * @param in
     * @param out
     * @return
     * @throws java.io.IOException
     */
    static public int copy(final InputStream in, final OutputStream out)
            throws IOException {
        final int BUFFER_LENGTH = 1024;
        final byte[] buffer = new byte[BUFFER_LENGTH];
        int totalRead = 0;
        int read = 0;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            totalRead += read;
        }

        return totalRead;
    }
}

Related Tutorials