Java InputStream to OutputStream copyStream(final InputStream is, final OutputStream out, final Long amount, final int bufferSize)

Here you can find the source of copyStream(final InputStream is, final OutputStream out, final Long amount, final int bufferSize)

Description

Copies the length of bytes from the inputstream to the outputStream.

License

Open Source License

Parameter

Parameter Description
is The inputstream where the bytes will be copied from.
out The outputStream where the bytes will be copied to.
amount The amount of bytes that will be copied. If not given the inputstream will be read till the end.
bufferSize The size of the buffer which the copy will be done with.

Exception

Parameter Description
IOException if an error occurs on the output or input stream.

Return

The number of bytes that were copied.

Declaration

public static long copyStream(final InputStream is, final OutputStream out, final Long amount,
        final int bufferSize) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  w  w. j a va 2 s .co m*/
 * This file is part of Everit - Blobstore.
 *
 * Everit - Blobstore is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Everit - Blobstore is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Everit - Blobstore.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Copies the length of bytes from the inputstream to the outputStream.
     *
     * @param is
     *            The inputstream where the bytes will be copied from.
     * @param out
     *            The outputStream where the bytes will be copied to.
     * @param amount
     *            The amount of bytes that will be copied. If not given the inputstream will be read till the end.
     * @param bufferSize
     *            The size of the buffer which the copy will be done with.
     * @throws IOException
     *             if an error occurs on the output or input stream.
     * @return The number of bytes that were copied.
     */
    public static long copyStream(final InputStream is, final OutputStream out, final Long amount,
            final int bufferSize) throws IOException {
        long bytesProcessed = 0;
        byte[] buf = new byte[bufferSize];
        int bytesReadOut = 0;
        while (((amount == null) || (bytesProcessed < amount.longValue())) && (bytesReadOut > -1)) {
            int bytesToRead = bufferSize;
            if (amount != null) {
                long bytesLeft = amount.longValue() - bytesProcessed;
                if ((bytesLeft < bytesToRead)) {
                    bytesToRead = (int) (bytesLeft);
                }
            }
            bytesReadOut = is.read(buf, 0, bytesToRead);
            if (bytesReadOut > -1) {
                bytesProcessed = bytesProcessed + bytesReadOut;
                out.write(buf, 0, bytesReadOut);
            }
        }
        return bytesProcessed;
    }
}

Related

  1. copyStream(final InputStream inputStream, final OutputStream outputStream)
  2. copyStream(final InputStream inputStream, final OutputStream outputStream)
  3. copyStream(final InputStream inputStream, final OutputStream outputStream)
  4. copyStream(final InputStream is, final OutputStream os)
  5. copyStream(final InputStream is, final OutputStream os)
  6. copyStream(final InputStream source, final OutputStream target)
  7. copyStream(final InputStream src, OutputStream dest)
  8. copyStream(final OutputStream to, final InputStream from)
  9. copyStream(InputStream fis, OutputStream fos)