@Override public int read(final byte[] buffer,int offset,int len) throws IOException {
  if (compressDone) {
    return -1;
  }
  byte[] readBuffer=new byte[2 * len];
  int n=0;
  int read=0;
  while (len > 0) {
    n=deflater.deflate(buffer,offset,len);
    if (n == 0) {
      if (isFinished) {
        deflater.end();
        compressDone=true;
        break;
      }
 else       if (deflater.needsInput()) {
        int m=input.read(readBuffer);
        if (m == -1) {
          deflater.finish();
          isFinished=true;
        }
 else {
          if (bytesRead != null) {
            bytesRead.addAndGet(m);
          }
          deflater.setInput(readBuffer,0,m);
        }
      }
 else {
        deflater.finish();
        isFinished=true;
      }
    }
 else {
      read+=n;
      offset+=n;
      len-=n;
    }
  }
  return read;
}

@ Override public int read ( final byte [ ] buffer , int offset , int len ) throws IOException { if ( compressDone ) { return - 1 ; } byte [ ] readBuffer = new byte [ 2 * len ] ; int n = 0 ; int read = 0 ; while ( len > 0 ) { n = deflater . deflate ( buffer , offset , len ) ; if ( n == 0 ) { if ( isFinished ) { deflater . end ( ) ; compressDone = true ; break ; } else if ( deflater . needsInput ( ) ) { int m = input . read ( readBuffer ) ; if ( m == - 1 ) { deflater . finish ( ) ; isFinished = true ; } else { if ( bytesRead != null ) { bytesRead . addAndGet ( m ) ; } deflater . setInput ( readBuffer , 0 , m ) ; } } else { deflater . finish ( ) ; isFinished = true ; } } else { read += n ; offset += n ; len -= n ; } } return read ; }
try to fill the buffer with compressed bytes . except the last effective read , this method always returns with a full buffer of compressed data .
