Here you can find the source of copyStreams(InputStream source, OutputStream destination)
Copies the data from the given InputStream to the given OutputStream .
Parameter | Description |
---|---|
source | a byte data source |
destination | the target of the copy operation |
Parameter | Description |
---|---|
IOException | if an error occurs while copying |
public static void copyStreams(InputStream source, OutputStream destination) throws IOException
//package com.java2s; /*//from w w w . j a va 2 s.c o m * Copyright (C) 2007 Roland Krueger * * Created on 26.03.2010 * * Author: Roland Krueger (www.rolandkrueger.info) * * This file is part of RoKlib. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * <p> * Copies the data from the given {@link InputStream} to the given {@link OutputStream}. * <p> * Both source and destination stream will be closed after the copy process. * * @param source * a byte data source * @param destination * the target of the copy operation * @throws IOException * if an error occurs while copying */ public static void copyStreams(InputStream source, OutputStream destination) throws IOException { copyStreams(source, destination, true, true); } /** * <p> * Copies the data from the given {@link InputStream} to the given {@link OutputStream}. The two boolean parameters * let you define whether you want the source and destination streams to be closed after the operation. * * @param source * a byte data source * @param destination * the target of the copy operation * @param closeSource * source stream will be closed if <code>true</code> * @param closeDestination * destination stream will be closed if <code>true</code> * @throws IOException * if an error occurs while copying */ public static void copyStreams(InputStream source, OutputStream destination, boolean closeSource, boolean closeDestination) throws IOException { byte[] buffer = new byte[4096]; int read; while ((read = source.read(buffer)) != -1) { destination.write(buffer, 0, read); } destination.flush(); if (closeDestination) destination.close(); if (closeSource) source.close(); } }