Description
Copies the contents of an java.io.InputStream to an java.io.OutputStream , using buffer size provided.
License
Open Source License
Parameter
Parameter | Description |
---|
in | The InputStream to copy. |
out | The OutputStream to copy to. |
bufferSize | The size of the buffer to use when copying the stream. |
Exception
Parameter | Description |
---|
IOException | If a problem occurs copying the InputStream to theOutputStream. |
Declaration
public static void copyStream(final InputStream in, final OutputStream out, final int bufferSize)
throws IOException
Method Source Code
//package com.java2s;
/*// w w w . j a v a 2 s . c o m
* Copyright Matt Palmer 2011-2012, All rights reserved.
*
* This code is licensed under a standard 3-clause BSD license:
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * The names of its contributors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
private static final int DEFAULT_BUFFER_SIZE = 4096;
/**
* Copies the contents of an {@link java.io.InputStream} to an
* {@link java.io.OutputStream}, using the default buffer size of 4096.
*
* @param in
* The InputStream to copy.
* @param out
* The OutputStream to copy to.
* @throws IOException
* If a problem occurs copying the InputStream to the
* OutputStream.
*/
public static void copyStream(final InputStream in, final OutputStream out) throws IOException {
copyStream(in, out, DEFAULT_BUFFER_SIZE);
}
/**
* Copies the contents of an {@link java.io.InputStream} to an
* {@link java.io.OutputStream}, using buffer size provided.
*
* @param in
* The InputStream to copy.
* @param out
* The OutputStream to copy to.
* @param bufferSize
* The size of the buffer to use when copying the stream.
* @throws IOException
* If a problem occurs copying the InputStream to the
* OutputStream.
*/
public static void copyStream(final InputStream in, final OutputStream out, final int bufferSize)
throws IOException {
final byte[] buffer = new byte[bufferSize];
int byteRead = 0;
while ((byteRead = in.read(buffer)) >= 0) {
out.write(buffer, 0, byteRead);
}
}
}
Related
- copyStream(@Nonnull InputStream in, @Nonnull OutputStream out)
- copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target)
- copyStream(final InputStream in, final OutputStream out)
- copyStream(final InputStream in, final OutputStream out)
- copyStream(final InputStream in, final OutputStream out, int iMax)
- copyStream(final InputStream input, final OutputStream output, long count)
- copyStream(final InputStream inputStream, final OutputStream out)
- copyStream(final InputStream inputStream, final OutputStream outputStream)