public long copy(String filename,OutputStream out,long offset,long size) throws IOException, InterruptedException {
  if (size < 0) {
    return 0;
  }
  BufferedInputStream is=new BufferedInputStream(getFileInputStream(filename));
  byte[] buf=new byte[64 * 1024];
  int bytesRead;
  long pos=0;
  long remaining=size;
  try {
    if (offset != 0) {
      is.skip(offset);
      pos+=offset;
    }
    while (remaining != 0) {
      int max=buf.length;
      if (max > remaining) {
        max=(int)remaining;
      }
      bytesRead=is.read(buf,0,max);
      if (bytesRead == -1) {
        break;
      }
      out.write(buf,0,bytesRead);
      pos+=bytesRead;
      remaining-=bytesRead;
    }
    out.flush();
  }
 catch (  Exception e) {
    out.flush();
  }
 finally {
    try {
      is.close();
    }
 catch (    Exception e) {
    }
  }
  return pos;
}

public long copy ( String filename , OutputStream out , long offset , long size ) throws IOException , InterruptedException { if ( size < 0 ) { return 0 ; } BufferedInputStream is = new BufferedInputStream ( getFileInputStream ( filename ) ) ; byte [ ] buf = new byte [ 64 * 1024 ] ; int bytesRead ; long pos = 0 ; long remaining = size ; try { if ( offset != 0 ) { is . skip ( offset ) ; pos += offset ; } while ( remaining != 0 ) { int max = buf . length ; if ( max > remaining ) { max = ( int ) remaining ; } bytesRead = is . read ( buf , 0 , max ) ; if ( bytesRead == - 1 ) { break ; } out . write ( buf , 0 , bytesRead ) ; pos += bytesRead ; remaining -= bytesRead ; } out . flush ( ) ; } catch ( Exception e ) { out . flush ( ) ; } finally { try { is . close ( ) ; } catch ( Exception e ) { } } return pos ; }
copies the given file to the output stream continously , i . e . not stop when end of file is reached , but rather wait for additional data to be appended to the file .
