Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.io.OutputStream;

public class Main {
    public static final int FILE_BUFFER_SIZE = 4 * 1024;

    /**
     * Copy the content of the input stream into the output stream, using a
     * temporary byte array buffer whose size is defined by
     * {@link #FILE_BUFFER_SIZE}.
     * 
     * @param in
     *          The input stream to copy from.
     * @param out
     *          The output stream to copy to.
     * 
     * @throws java.io.IOException
     *           If any error occurs during the copy.
     */
    public static void copyFile(InputStream in, OutputStream out, long length) throws IOException {
        long totalRead = 0;
        byte[] b = new byte[FILE_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) > 0) {
            out.write(b, 0, read);
            out.flush();

            totalRead += read;
            if (totalRead >= length)
                break;
        }
    }
}