Java Utililty Methods InputStream Copy

List of utility methods to do InputStream Copy

Description

The list of methods to do InputStream Copy are organized into topic(s).

Method

intcopyByte(InputStream input, OutputStream output)
Copies one byte from input to output.
int b = input.read();
if (b < 0) {
    throw new IOException("Unexpected EOF.");
if (output != null) {
    output.write(b);
return b & 0xFF;
...
voidcopyBytes(DataInput in, DataOutput out, int length, byte[] buf)
Copies the specified number of bytes from the given DataInput to the given DataOutput.
int numLoops = length >> BUF_LEN_LOG_2;
for (int count = 0; count < numLoops; count++) {
    in.readFully(buf);
    out.write(buf);
in.readFully(buf, 0, length & BUF_LEN_MASK);
out.write(buf, 0, length & BUF_LEN_MASK);
voidcopyBytes(final byte[] bytes, final OutputStream outputStream)
copy Bytes
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
voidcopyBytes(final InputStream in, final OutputStream out, final int buffSize, final boolean close)
Copies from one stream to another.
@SuppressWarnings("resource")
final PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null;
final byte[] buf = new byte[buffSize];
try {
    int bytesRead = in.read(buf);
    while (bytesRead >= 0) {
        out.write(buf, 0, bytesRead);
        if ((ps != null) && ps.checkError()) {
...
voidcopyBytes(final InputStream is, final OutputStream os)
Reads bytes from the given input stream and writes them to the given output stream until the end of the input stream is reached.
while (true) {
    final int b = is.read();
    if (b == -1) {
        break;
    os.write(b);
intcopyBytes(InputStream in, OutputStream out)
copy Bytes
if (in == null || out == null) {
    throw new IllegalArgumentException("In/OutStream cannot be null");
int total = 0;
byte[] buffer = new byte[BUFFERSIZE];
for (int bytesRead; (bytesRead = in.read(buffer)) != -1;) {
    out.write(buffer, 0, bytesRead);
    total += bytesRead;
...
voidcopyBytes(InputStream in, OutputStream out, int bufferSize, boolean close)
Copy uninterpreted bytes from an input stream to an output stream.
BufferedInputStream from = new BufferedInputStream(in);
BufferedOutputStream to = new BufferedOutputStream(out);
byte[] bytes = new byte[bufferSize];
int size;
while ((size = from.read(bytes, 0, bufferSize)) >= 0) {
    to.write(bytes, 0, size);
if (close) {
...
voidcopyBytes(InputStream in, OutputStream out, int buffSize)
Copies from one stream to another.
PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null;
byte buf[] = new byte[buffSize];
int bytesRead = in.read(buf);
while (bytesRead >= 0) {
    out.write(buf, 0, bytesRead);
    if ((ps != null) && ps.checkError()) {
        throw new IOException("Unable to write to output stream.");
    bytesRead = in.read(buf);
longcopyLarge(final InputStream input, final OutputStream output)
copy Large
return copy(input, output, DEFAULT_BUFFER_SIZE);
longcopyLarge(final InputStream input, final OutputStream output)
Copies bytes from a large (over 2GB) InputStream to an OutputStream.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
    count += n;
return count;
...