Java InputStream Copy nio copy(InputStream input, OutputStream output)

Here you can find the source of copy(InputStream input, OutputStream output)

Description

Copies all data from an InputStream to an OutputStream.

License

Apache License

Exception

Parameter Description
IOException if an I/O error occurs

Return

the number of bytes copied

Declaration

public static long copy(InputStream input, OutputStream output) throws IOException 

Method Source Code


//package com.java2s;
/*/*  www .j a va 2 s  . co  m*/
 * Copyright (C) 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

import java.io.BufferedWriter;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;

import java.io.Writer;
import java.nio.charset.StandardCharsets;

public class Main {
    /**
     * Copies all data from an InputStream to an OutputStream.
     *
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs
     */
    public static long copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[2 * 1024];
        long total = 0;
        int count;
        while ((count = input.read(buffer)) != -1) {
            output.write(buffer, 0, count);
            total += count;
        }

        return total;
    }

    public static long copy(Reader reader, Writer writer) throws IOException {
        char[] buffer = new char[2 * 1024];
        long total = 0;
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
            total += count;
        }

        return total;
    }

    public static long copy(InputStream input, Writer writer) throws IOException {
        return copy(new InputStreamReader(input, StandardCharsets.UTF_8), writer);
    }

    public static long copy(InputStream input, File file) throws IOException {
        FileOutputStream output = new FileOutputStream(file);
        try {
            return copy(input, output);
        } finally {
            close(output);
        }
    }

    public static void copy(String string, File file) throws IOException {
        Writer writer = new BufferedWriter(new FileWriter(file));
        try {
            writer.write(string);
        } finally {
            close(writer);
        }
    }

    /**
     * Silently closes a Closeable.
     *
     * @return the exception or null if no exception thrown
     */
    public static IOException close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException e) {
            return e;
        }

        return null;
    }
}

Related

  1. copy(InputStream in, OutputStream out)
  2. copy(InputStream in, OutputStream out)
  3. copy(InputStream in, OutputStream out)
  4. copy(InputStream in, OutputStream out)
  5. copy(InputStream in, OutputStream out, boolean closeInput, boolean closeOutput)
  6. copy(InputStream input, OutputStream output)
  7. copy(InputStream input, OutputStream output)
  8. copy(InputStream input, OutputStream output)
  9. copy(InputStream src, File dst)