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

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

Description

The actual procedure of copying Streams (like binary files)

License

Open Source License

Parameter

Parameter Description
in The stream to read data from
out The stream to write data to

Return

true, if everything is ok

Declaration

public static boolean copyStreams(InputStream in, OutputStream out) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2002-2011 by XMLVM.org
 *
 * Project Info:  http://www.xmlvm.org//from www.  j a v a 2 s  . c  o  m
 *
 * This program 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.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * The actual procedure of copying Streams (like binary files)
     * 
     * @param in
     *            The stream to read data from
     * @param out
     *            The stream to write data to
     * @return true, if everything is ok
     */
    public static boolean copyStreams(InputStream in, OutputStream out) {
        if (in == null || out == null)
            return false;
        try {
            byte[] buf = new byte[4096];
            int len;
            while ((len = in.read(buf)) > 0)
                out.write(buf, 0, len);
            in.close();
            out.close();
            return true;
        } catch (IOException e) {
            try {
                in.close();
            } catch (IOException ex1) {
            }
            try {
                out.close();
            } catch (IOException ex1) {
            }
            e.printStackTrace();
        }
        return false;
    }
}

Related

  1. copyStreamNoClose(InputStream in, OutputStream out)
  2. copyStreamPortion(java.io.InputStream inputStream, java.io.OutputStream outputStream, int portionSize, int bufferSize)
  3. copyStreams(final InputStream in, final OutputStream out)
  4. copyStreams(final InputStream input, final OutputStream output)
  5. copyStreams(InputStream from, OutputStream to, int blockSize)
  6. copyStreams(InputStream in, OutputStream out)
  7. copyStreams(InputStream in, OutputStream out)
  8. copyStreams(InputStream in, OutputStream out, int buf)
  9. copyStreams(InputStream input, OutputStream output)