Java InputStream Copy nio copy(InputStream in, OutputStream out)

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

Description

Verbatim copy an entry from input to output stream.

License

Open Source License

Parameter

Parameter Description
in the source.
out the destination.

Exception

Parameter Description
IOException if an error occurs while reading or writing.

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w ww . j a  va 2 s.c om*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class Main {
    private static final Charset UTF8 = Charset.forName("UTF-8");

    /**
     * Verbatim copy an entry from input to output stream.
     * 
     * @param in the source.
     * @param out the destination.
     * @throws IOException if an error occurs while reading or writing.
     */
    public static void copy(InputStream in, OutputStream out) throws IOException {
        copy(in, out, 512 * 1024);
    }

    public static void copy(InputStream in, OutputStream out, final int bufferSize) throws IOException {
        final byte[] buffer = new byte[bufferSize];
        while (true) {
            int count = in.read(buffer);
            if (count == -1)
                break;
            out.write(buffer, 0, count);
        }
    }

    public static void copy(InputStream ins, File out) throws IOException {
        // buffered since read() in copy(InputStream, OutputStream) may return 1 byte at a time
        final OutputStream ous = new BufferedOutputStream(new FileOutputStream(out));
        try {
            copy(ins, ous);
        } finally {
            ous.close();
        }
    }

    public static void write(final String s, final OutputStream out) throws IOException {
        write(s, out, UTF8);
    }

    public static void write(final String s, final OutputStream out, Charset charset) throws IOException {
        out.write(s.getBytes(charset));
    }
}

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)
  7. copy(InputStream input, OutputStream output)