Here you can find the source of copyStream(InputStream in, OutputStream os)
Parameter | Description |
---|---|
in | a parameter |
os | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyStream(InputStream in, OutputStream os) throws IOException
//package com.java2s; /****************************************************************************** * Copyright (c) 2013, Linagora//from ww w.j av a2 s .co m * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Linagora - initial API and implementation *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Copies the content from in into os. * <p> * Neither <i>in</i> nor <i>os</i> are closed by this method.<br /> * They must be explicitly closed after this method is called. * </p> * * @param in * @param os * @throws IOException */ public static void copyStream(InputStream in, OutputStream os) throws IOException { byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { os.write(buf, 0, len); } } /** * Copies the content from in into outputFile. * <p> * <i>in</i> is not closed by this method.<br /> * It must be explicitly closed after this method is called. * </p> * * @param in * @param outputFile will be created if it does not exist * @throws IOException */ public static void copyStream(InputStream in, File outputFile) throws IOException { if (!outputFile.exists() && !outputFile.createNewFile()) throw new IOException("Failed to create " + outputFile.getAbsolutePath() + "."); OutputStream os = new FileOutputStream(outputFile); copyStream(in, os); os.close(); } /** * Copies the content from inputFile into outputFile. * * @param inputFile * @param outputFile will be created if it does not exist * @throws IOException */ public static void copyStream(File inputFile, File outputFile) throws IOException { InputStream is = new FileInputStream(inputFile); copyStream(is, outputFile); is.close(); } /** * Copies the content from inputFile into an output stream. * * @param inputFile * @param os the output stream * @throws IOException */ public static void copyStream(File inputFile, OutputStream os) throws IOException { InputStream is = new FileInputStream(inputFile); copyStream(is, os); is.close(); } }