Here you can find the source of copyStream(InputStream in, OutputStream out, boolean closeOut)
InputStream
and copy all data to an OutputStream
.
Parameter | Description |
---|---|
in | the Input Stream |
out | the Output Stream |
closeOut | if true, out is closed when the copy has finished |
Parameter | Description |
---|---|
IOException | if an I/O error occurs. |
public static void copyStream(InputStream in, OutputStream out, boolean closeOut) throws IOException
//package com.java2s; /*//from w ww. j a va 2 s .c om * Copyright 2004-2009 Luciano Vernaschi * * This file is part of MeshCMS. * * MeshCMS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MeshCMS 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MeshCMS. If not, see <http://www.gnu.org/licenses/>. */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * A standard size for buffers. */ public static final int BUFFER_SIZE = 2048; /** * Reads an <code>InputStream</code> and copy all data to an * <code>OutputStream</code>. * * @param in the Input Stream * @param out the Output Stream * @param closeOut if true, out is closed when the copy has finished * * @throws IOException if an I/O error occurs. */ public static void copyStream(InputStream in, OutputStream out, boolean closeOut) throws IOException { byte b[] = new byte[BUFFER_SIZE]; int n; try { while ((n = in.read(b)) != -1) { out.write(b, 0, n); } } finally { try { in.close(); } finally { if (closeOut) { out.close(); } } } } }