Description
Copies data from an InputStream to an OutputStream.
License
Open Source License
Parameter
Parameter | Description |
---|
in | the stream to read from |
out | the stream to write to |
Exception
Parameter | Description |
---|
NullPointerException | if either in or out are null |
IOException | if an I/O error occurs |
Return
the number of bytes read from the input stream
Declaration
public static long copyStream(InputStream in, OutputStream out) throws IOException
Method Source Code
//package com.java2s;
/*/*from w ww .j av a 2 s .c om*/
* $Id$
*
* Copyright (C) 2003-2015 JNode.org
*
* This library 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 2.1 of the License, or
* (at your option) any later version.
*
* This library 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 this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import java.io.Flushable;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Main {
private static final int BUFFER_SIZE = 4096;
private static final String ex_null_param = "A required paramater is null";
/**
* Copies data from an InputStream to an OutputStream.
*
* This method allocates a 4096 byte buffer each time it is called.
*
* At the end of writing, the OutputStream will be flushed, but no streams will be closed.
*
* @param in the stream to read from
* @param out the stream to write to
* @return the number of bytes read from the input stream
* @throws NullPointerException if either in or out are null
* @throws IOException if an I/O error occurs
*/
public static long copyStream(InputStream in, OutputStream out) throws IOException {
return copyStream(in, out, new byte[BUFFER_SIZE]);
}
/**
* Copies data from an InputStream to an OutputStream.
*
* This method allocates a buffer of 'bufferSize' when it is called.
*
* At the end of writing, the OutputStream will be flushed, but no streams will be closed.
*
* @param in the stream to read from
* @param out the stream to write to
* @param bufferSize the size of buffer to use for the copy
* @return the number of bytes read from the input stream
* @throws NullPointerException if either in or out are null
* @throws IOException if an I/O error occurs
*/
public static long copyStream(InputStream in, OutputStream out, int bufferSize) throws IOException {
return copyStream(in, out, new byte[bufferSize]);
}
/**
* Copies data from an InputStream to an OutputStream
*
* If copying multiple streams, this method may be prefered to the others
* as a way to use the same buffer instead of allocating a new buffer on each call.
*
* At the end of writing, the OutputStream will be flushed, but no streams will be closed.
*
* @param in the stream to read from
* @param out the stream to write to
* @param buffer a pre-allocated buffer to use.
* @return the number of bytes read from the input stream
* @throws NullPointerException if either in, out or buffer are null
* @throws IOException if an I/O error occurs
*/
public static long copyStream(InputStream in, OutputStream out, byte[] buffer) throws IOException {
checkNull(in, out, buffer);
long totalBytes = 0;
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
totalBytes += len;
}
out.flush();
return totalBytes;
}
/**
* Check for null objects in a list of objects.
*/
private static void checkNull(Object... objs) {
for (Object obj : objs) {
if (obj == null)
throw new NullPointerException(ex_null_param);
}
}
/**
* Call the flush method of a list of Flushable objects.
*
* This method will not throw a NullPointerException if any of the objects are null.
*
* This method will trap the IOException from flush.
*
* @param objs one or more Flushable objects
*/
public static void flush(Flushable... objs) {
for (Flushable obj : objs) {
if (obj != null) {
try {
obj.flush();
} catch (IOException e) {
// ignore
}
}
}
}
}
Related
- copyStream(InputStream in, OutputStream os)
- copyStream(InputStream in, OutputStream os)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)
- copyStream(InputStream in, OutputStream out)