Java InputStream to OutputStream copyStream(InputStream in, OutputStream out)

Here you can find the source of copyStream(InputStream in, OutputStream out)

Description

copy Stream

License

Apache License

Declaration

public static long copyStream(InputStream in, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/*// w  w  w.  j  a v  a 2  s.  c o  m
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    public static long copyStream(InputStream in, OutputStream out) throws IOException {
        return copyStream(in, out, new byte[8192]);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf) throws IOException {
        return copyStream(in, out, buf, null, null);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf, Logger logger, Level level)
            throws IOException {
        return copyStream(in, out, buf, logger, level, true);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf, Logger logger, Level level,
            boolean closeStream) throws IOException {
        return copyStream(in, out, buf, closeStream, logger, level);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf, boolean close, Logger log,
            Level level) throws IOException {
        return copyStream(in, out, buf, close, log, level, Long.MAX_VALUE);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf, boolean closeInputStream)
            throws IOException {
        return copyStream(in, out, buf, closeInputStream, null, null, Long.MAX_VALUE);
    }

    public static long copyStream(InputStream in, OutputStream out, byte[] buf, boolean closeStream, Logger logger,
            Level level, final long numBytesToCopy) throws IOException {
        if (buf == null) {
            buf = new byte[8192];
        }
        try {
            int len;
            long copied = 0;
            while (numBytesToCopy > copied
                    && (len = in.read(buf, 0, (int) Math.min(buf.length, (numBytesToCopy - copied)))) != -1) {
                out.write(buf, 0, len);
                if (logger != null && logger.isLoggable(level)) {
                    logger.log(level, new String(buf, 0, len));
                }
                copied += len;
            }
            if (numBytesToCopy != Long.MAX_VALUE && numBytesToCopy != copied) {
                throw new IOException("expected to copy " + numBytesToCopy + ", actually copied " + copied);
            }
            return copied;
        } finally {
            try {
                out.flush();
            } finally {
                if (closeStream) {
                    in.close();
                }
            }
        }
    }

    public static void copyStream(Reader in, Writer out) throws IOException {
        char[] buf = new char[8192];
        try {
            int len;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
        } finally {
            in.close();
        }
    }
}

Related

  1. copyStream(InputStream in, boolean closeIn, OutputStream out, boolean closeOut)
  2. copyStream(InputStream in, File outputFile)
  3. copyStream(InputStream in, FileOutputStream out, IProgressMonitor monitor, int length)
  4. copyStream(InputStream in, OutputStream os)
  5. copyStream(InputStream in, OutputStream os)
  6. copyStream(InputStream in, OutputStream out)
  7. copyStream(InputStream in, OutputStream out)
  8. copyStream(InputStream in, OutputStream out)
  9. copyStream(InputStream in, OutputStream out)