Java OutputStream Write writeStream(InputStream inputStream, OutputStream out)

Here you can find the source of writeStream(InputStream inputStream, OutputStream out)

Description

write Stream

License

Open Source License

Declaration

public static void writeStream(InputStream inputStream, OutputStream out) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .  jav a2s  . com*/
 * Copyright (C) 2012 eXo Platform SAS.
 *
 * 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.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void writeStream(InputStream inputStream, OutputStream out) {
        // write the inputStream to a FileOutputStream
        try {

            int read = 0;
            byte[] bytes = new byte[inputStream.available()];

            while ((read = inputStream.read(bytes)) > 0) {
                out.write(bytes, 0, read);
            }

            inputStream.close();
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void writeStream(InputStream inputStream, String path) {

        try {
            File folder = new File(path).getParentFile();
            if (!folder.exists()) {
                folder.mkdirs();
            }
            OutputStream out = new FileOutputStream(new File(path));
            writeStream(inputStream, out);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Related

  1. writeStream(byte[] data, OutputStream os)
  2. writeStream(byte[] data, OutputStream out)
  3. writeStream(final OutputStream out, final String content, final boolean close)
  4. writeStream(InputStream _from, Writer _printTo)
  5. writeStream(InputStream in, File file)
  6. writeStream(OutputStream out, byte[] data)
  7. writeStream(OutputStream output, String dataStr)
  8. writeStream(OutputStream outputStream, InputStream inputStream, byte[] buffer)
  9. writeStream(String content, String charset, OutputStream outputStream)