Java InputStream to OutputStream copyStreamAndClose(InputStream is, OutputStream os)

Here you can find the source of copyStreamAndClose(InputStream is, OutputStream os)

Description

Copy input stream to output stream and close them both

License

Open Source License

Parameter

Parameter Description
is input stream
os output stream

Exception

Parameter Description
IOException for any error

Declaration

public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  ww  . j av a2  s.c  o m*/
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

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

public class Main {
    /**
     * Copy input stream to output stream and close them both
     *
     * @param is input stream
     * @param os output stream
     * @throws IOException for any error
     */
    public static void copyStreamAndClose(InputStream is, OutputStream os) throws IOException {
        try {
            copyStream(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignored) {
                }
            }
            if (os != null)
                os.close();
        }
    }

    /**
     * Copy input stream to output stream without closing streams.
     * Flushes output stream when done.
     *
     * @param is input stream
     * @param os output stream
     * @throws IOException for any error
     */
    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        if (is == null)
            throw new IllegalArgumentException("input stream is null");
        if (os == null)
            throw new IllegalArgumentException("output stream is null");

        try {
            byte[] buff = new byte[65536];
            int rc = is.read(buff);
            while (rc != -1) {
                os.write(buff, 0, rc);
                rc = is.read(buff);
            }
        } finally {
            os.flush();
        }
    }
}

Related

  1. copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
  2. copyStream(java.io.InputStream src, java.io.OutputStream dest)
  3. copyStream(OutputStream out, InputStream in, int bufsz)
  4. copyStream(OutputStream outStream, InputStream inStream)
  5. copyStream(Reader input, Writer output)
  6. copyStreamAndClose(InputStream is, OutputStream os)
  7. copyStreamBounded(InputStream in, OutputStream out, long length)
  8. copyStreamContent(InputStream inputStream, OutputStream outputStream)
  9. copyStreamContent(InputStream inputStream, OutputStream outputStream)