Java InputStream to OutputStream copyStream(InputStream src, OutputStream dest)

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

Description

copy Stream

License

Open Source License

Declaration

public static boolean copyStream(InputStream src, OutputStream dest) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.  ja va 2  s.  c  o m*/
 * Copyright (c) 2008  Los Alamos National Security, LLC.
 *
 * Los Alamos National Laboratory
 * Research Library
 * Digital Library Research & Prototyping Team
 *
 * This library 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 St, Fifth Floor, Boston, MA 02110-1301 USA
 * 
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

public class Main {
    public static boolean copyStream(InputStream src, OutputStream dest) {
        InputStream in = null;
        OutputStream out = null;
        byte[] buf = null;
        int bufLen = 20000 * 1024;
        try {
            in = new BufferedInputStream(src);
            out = new BufferedOutputStream(dest);
            buf = new byte[bufLen];
            byte[] tmp = null;
            int len = 0;
            while ((len = in.read(buf, 0, bufLen)) != -1) {
                tmp = new byte[len];
                System.arraycopy(buf, 0, tmp, 0, len);
                out.write(tmp);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (Exception e) {
                }
            if (out != null)
                try {
                    out.close();
                } catch (Exception e) {
                }
        }
        return true;
    }
}

Related

  1. copyStream(InputStream source, OutputStream destination, byte[] buffer)
  2. copyStream(InputStream sourceInputStream, OutputStream targetOutputStream)
  3. copyStream(InputStream sourceStream, OutputStream destinationStream)
  4. copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
  5. copyStream(InputStream src, OutputStream dest)
  6. copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
  7. copyStream(InputStream src, OutputStream osstream)
  8. CopyStream(InputStream sSource, OutputStream sTarget)
  9. copyStream(int bufferSize, InputStream in, OutputStream out, boolean canStop)