Here you can find the source of copyStream(InputStream is, OutputStream os)
public static void copyStream(InputStream is, OutputStream os)
//package com.java2s; /*//from w w w .j av a 2 s .co m * Copyright (C) 2010-2015 JPEXS, All rights reserved. * * 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 3.0 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. */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyStream(InputStream is, OutputStream os) { try { final int bufSize = 4096; byte[] buf = new byte[bufSize]; int cnt = 0; while ((cnt = is.read(buf)) > 0) { os.write(buf, 0, cnt); } } catch (IOException ex) { // ignore } } public static void copyStream(InputStream is, OutputStream os, long maxLength) { try { final int bufSize = 4096; byte[] buf = new byte[bufSize]; int cnt = 0; while ((cnt = is.read(buf)) > 0) { os.write(buf, 0, cnt); maxLength -= cnt; // last chunk is smaller if (maxLength < bufSize) { buf = new byte[(int) maxLength]; } } } catch (IOException ex) { // ignore } } }