Java InputStream to OutputStream copyStream(java.io.InputStream src, java.io.OutputStream dest)

Here you can find the source of copyStream(java.io.InputStream src, java.io.OutputStream dest)

Description

Copy an input stream to an output stream.

License

Open Source License

Parameter

Parameter Description
src Source stream
dest Destination stream

Exception

Parameter Description
IOException if an error occurs while copying

Declaration

public static int copyStream(java.io.InputStream src, java.io.OutputStream dest) throws IOException 

Method Source Code

//package com.java2s;
/*/* ww  w  . j a va 2s . c  o m*/
 * Copyright 2005--2008 Helsinki Institute for Information Technology
 *
 * This file is a part of Fuego middleware.  Fuego middleware is free
 * software; you can redistribute it and/or modify it under the terms
 * of the MIT license, included as the file MIT-LICENSE in the Fuego
 * middleware source distribution.  If you did not receive the MIT
 * license with the distribution, write to the Fuego Core project at
 * fuego-core-users@googlegroups.com.
 */

import java.io.IOException;

import java.util.LinkedList;

public class Main {
    /** Size of byte buffer used when copying streams. Currently set to {@value} */
    public static final int COPY_BUF_SIZE = 4096;
    protected static LinkedList buffers = new LinkedList();

    /** Copy an input stream to an output stream.
     * @param src Source stream
     * @param dest Destination stream
     * @throws IOException if an error occurs while copying
     */
    public static int copyStream(java.io.InputStream src, java.io.OutputStream dest) throws IOException {
        return copyStream(src, dest, Long.MAX_VALUE);
    }

    /** Copy an input stream to an output stream with maximum length
    * @param src Source stream
    * @param dest Destination stream
    * @param maxLeft maximum number of bytes to copy
    * @throws IOException if an error occurs while copying
    */
    public static int copyStream(java.io.InputStream src, java.io.OutputStream dest, long maxLeft)
            throws IOException {
        byte[] buffer = getBuf();
        int total = 0, count = 0;
        do {
            int maxchunk = (int) (maxLeft > buffer.length ? buffer.length : maxLeft);
            count = src.read(buffer, 0, maxchunk);
            if (count > 0) {
                dest.write(buffer, 0, count);
                total += count;
                maxLeft -= count;
            }
        } while (count > -1 && maxLeft > 0);
        freeBuf(buffer); // Will be gc'd if not returned, so we needn't protect by try/catch
        return total;
    }

    private static synchronized byte[] getBuf() {
        if (buffers.size() > 0)
            return (byte[]) buffers.removeFirst();
        else
            return new byte[COPY_BUF_SIZE];
    }

    private static synchronized void freeBuf(byte[] buf) {
        buffers.addLast(buf);
    }
}

Related

  1. copyStream(InputStream src, OutputStream osstream)
  2. CopyStream(InputStream sSource, OutputStream sTarget)
  3. copyStream(int bufferSize, InputStream in, OutputStream out, boolean canStop)
  4. copyStream(java.io.InputStream in, java.io.OutputStream out)
  5. copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
  6. copyStream(OutputStream out, InputStream in, int bufsz)
  7. copyStream(OutputStream outStream, InputStream inStream)
  8. copyStream(Reader input, Writer output)
  9. copyStreamAndClose(InputStream is, OutputStream os)