Java InputStream Copy nio 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.

License

Apache License

Return

the number of bytes copied

Declaration

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

Method Source Code

//package com.java2s;
/*//  w  ww.ja  v a 2s  .  c o m
 * FileUtil.java
 *
 * This file is part of SQL Workbench/J, http://www.sql-workbench.net
 *
 * Copyright 2002-2017, Thomas Kellerer
 *
 * Licensed under a modified Apache License, Version 2.0
 * that restricts the use for certain governments.
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at.
 *
 *     http://sql-workbench.net/manual/license.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * To contact the author please send an email to: support@sql-workbench.net
 *
 */

import java.io.Closeable;
import java.io.File;

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

import java.nio.file.Files;

import java.nio.file.StandardCopyOption;

public class Main {
    /**
     * The size of the buffer used by copy()
     */
    private static final int BUFF_SIZE = 2048 * 1024;

    /**
     * Copies the source file to the destination file.
     *
     * @param source
     * @param destination
     * 
     * @throws java.io.IOException
     *
     * @see Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)
     */
    public static void copy(File source, File destination) throws IOException {
        if (source == null || destination == null)
            return;
        Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

    /**
     * Copies the content of the InputStream to the OutputStream.
     * Both streams are closed automatically.
     *
     * @return the number of bytes copied
     */
    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 {
            closeQuietely(out);
            closeQuietely(in);
        }
        return filesize;
    }

    /**
     * Closes a Closeable without throwing an IOException.
     *
     * @param c the Closeable to close
     */
    public static void closeQuietely(Closeable c) {
        if (c == null)
            return;

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

Related

  1. copy(InputStream in, File f, long startAt)
  2. copy(InputStream in, OutputStream out)
  3. copy(InputStream in, OutputStream out)
  4. copy(InputStream in, OutputStream out)
  5. copy(InputStream in, OutputStream out)
  6. copy(InputStream in, OutputStream out, boolean closeInput, boolean closeOutput)