Here you can find the source of copyStream(InputStream in, OutputStream out)
public static long copyStream(InputStream in, OutputStream out) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static long copyStream(InputStream in, OutputStream out) throws IOException { return copyStream(in, 0, out); }//from ww w . j av a2 s. c o m public static long copyStream(InputStream in, OutputStream... outs) throws IOException { long size = 0; BufferedInputStream bis = null; BufferedOutputStream[] oses = null; try { bis = new BufferedInputStream(in); oses = new BufferedOutputStream[outs.length]; for (int i = 0; i < oses.length; i++) { oses[i] = new BufferedOutputStream(outs[i]); } int read = 0; byte[] bytes = new byte[1024]; while ((read = bis.read(bytes)) != -1) { for (BufferedOutputStream os : oses) { os.write(bytes, 0, read); } bytes = new byte[1024]; size = size + read; } } catch (IOException ex) { if (bis != null) { bis.close(); } if (oses != null) { for (BufferedOutputStream os : oses) { if (os != null) { os.flush(); os.close(); } } } } return size; } public static long copyStream(InputStream in, long skip, OutputStream out) throws IOException { long size = 0; BufferedInputStream bis = null; BufferedOutputStream os = null; try { bis = new BufferedInputStream(in); os = new BufferedOutputStream(out); int read = 0; byte[] bytes = new byte[1024]; if (skip > 0) { bis.skip(skip); } while ((read = bis.read(bytes)) != -1) { os.write(bytes, 0, read); bytes = new byte[1024]; size = size + read; } } catch (IOException ex) { throw ex; } finally { if (bis != null) { bis.close(); } if (os != null) { os.flush(); os.close(); } } return size; } public static long copyStream(InputStream in, OutputStream out, int skip, int length) throws IOException { long size = 0; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(in); bos = new BufferedOutputStream(out); if (skip > 0) { bis.skip(skip); } int read = 0; byte[] bytes = new byte[1024]; while ((read = bis.read(bytes)) != -1 && size < length) { if (length - size < read) { read = (int) (length - size); } bos.write(bytes, 0, read); bytes = new byte[1024]; size = size + read; } } catch (IOException ex) { throw ex; } finally { if (bis != null) { bis.close(); } if (bos != null) { bos.flush(); bos.close(); } } return size; } }