Java InputStream Copy copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)

Here you can find the source of copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)

Description

Copy uninterpreted bytes from an input stream to an output stream.

License

Open Source License

Parameter

Parameter Description
in InputStream from which to read
out OutputStream to which to write
bufferSize Size of transfer buffer
close If true, try to close streams at end

Exception

Parameter Description
IOException Thrown if the copy fails for any reason, includingdue to inability to close streams

Declaration

public static void copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)
        throws IOException 

Method Source Code

//package com.java2s;
/**//w w  w .  j  av a 2s .  c o  m
 * VMware Continuent Tungsten Replicator
 * Copyright (C) 2015 VMware, Inc. All rights reserved.
 *
 * 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.
 *
 * Initial developer(s): Robert Hodges
 * Contributor(s): 
 */

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

public class Main {
    /**
     * Copy uninterpreted bytes from an input stream to an output stream. This
     * routine uses the current positions of each stream whatever those happen
     * to be.
     * 
     * @param in InputStream from which to read
     * @param out OutputStream to which to write
     * @param bufferSize Size of transfer buffer
     * @param close If true, try to close streams at end
     * @throws IOException Thrown if the copy fails for any reason, including
     *             due to inability to close streams
     */
    public static void copyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)
            throws IOException {
        BufferedInputStream from = new BufferedInputStream(in);
        BufferedOutputStream to = new BufferedOutputStream(out);

        // Copy data.
        byte[] bytes = new byte[bufferSize];
        int size;
        while ((size = from.read(bytes, 0, bufferSize)) >= 0) {
            to.write(bytes, 0, size);
        }

        // Close streams if requested.
        if (close) {
            from.close();
            to.flush();
            to.close();
        }
    }
}

Related

  1. copyBytes(DataInput in, DataOutput out, int length, byte[] buf)
  2. copyBytes(final byte[] bytes, final OutputStream outputStream)
  3. copyBytes(final InputStream in, final OutputStream out, final int buffSize, final boolean close)
  4. copyBytes(final InputStream is, final OutputStream os)
  5. copyBytes(InputStream in, OutputStream out)
  6. copyBytes(InputStream in, OutputStream out, int buffSize)
  7. copyLarge(final InputStream input, final OutputStream output)
  8. copyLarge(final InputStream input, final OutputStream output)
  9. copyLarge(InputStream in, OutputStream out, byte[] buffer)