public static int copy(InputStream in,OutputStream out,long start,long end,boolean closeAfterDone) throws IOException {
  try {
    if (in == null || out == null)     return 0;
    byte[] bb=new byte[1024 * 4];
    int total=0;
    in.skip(start);
    int ii=(int)Math.min((end - start),bb.length);
    int len=in.read(bb,0,ii);
    while (len > 0) {
      out.write(bb,0,len);
      total+=len;
      ii=(int)Math.min((end - start - total),bb.length);
      len=in.read(bb,0,ii);
      out.flush();
    }
    return total;
  }
  finally {
    if (closeAfterDone) {
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}

public static int copy ( InputStream in , OutputStream out , long start , long end , boolean closeAfterDone ) throws IOException { try { if ( in == null || out == null ) return 0 ; byte [ ] bb = new byte [ 1024 * 4 ] ; int total = 0 ; in . skip ( start ) ; int ii = ( int ) Math . min ( ( end - start ) , bb . length ) ; int len = in . read ( bb , 0 , ii ) ; while ( len > 0 ) { out . write ( bb , 0 , len ) ; total += len ; ii = ( int ) Math . min ( ( end - start - total ) , bb . length ) ; len = in . read ( bb , 0 , ii ) ; out . flush ( ) ; } return total ; } finally { if ( closeAfterDone ) { if ( in != null ) { in . close ( ) ; } if ( out != null ) { out . close ( ) ; } } } }
copy the data in " inputstream " to " outputstream " , from start to end .
