@Override
  public synchronized int compress(byte[] buffer, int off, int len) throws IOException {
    SnappyUtil.validateBuffer(buffer, off, len);

    if (needsInput()) {
      // No buffered output bytes and no input to consume, need more input
      return 0;
    }

    if (!outputBuffer.hasRemaining()) {
      // There is uncompressed input, compress it now
      int maxOutputSize = Snappy.maxCompressedLength(inputBuffer.position());
      if (maxOutputSize > outputBuffer.capacity()) {
        ByteBuffer oldBuffer = outputBuffer;
        outputBuffer = ByteBuffer.allocateDirect(maxOutputSize);
        CleanUtil.clean(oldBuffer);
      }
      // Reset the previous outputBuffer
      outputBuffer.clear();
      inputBuffer.limit(inputBuffer.position());
      inputBuffer.position(0);

      int size = Snappy.compress(inputBuffer, outputBuffer);
      outputBuffer.limit(size);
      inputBuffer.limit(0);
      inputBuffer.rewind();
    }

    // Return compressed output up to 'len'
    int numBytes = Math.min(len, outputBuffer.remaining());
    outputBuffer.get(buffer, off, numBytes);    
    bytesWritten += numBytes;
    return numBytes;	    
  }
@Override
  public synchronized int compress(byte[] buffer, int off, int len) throws IOException {
    SnappyUtil.validateBuffer(buffer, off, len);

    if (needsInput()) {
      
      return 0;
    }

    if (!outputBuffer.hasRemaining()) {
      
      int maxOutputSize = Snappy.maxCompressedLength(inputBuffer.position());
      if (maxOutputSize > outputBuffer.capacity()) {
        ByteBuffer oldBuffer = outputBuffer;
        outputBuffer = ByteBuffer.allocateDirect(maxOutputSize);
        CleanUtil.clean(oldBuffer);
      }
      
      outputBuffer.clear();
      inputBuffer.limit(inputBuffer.position());
      inputBuffer.position(0);

      int size = Snappy.compress(inputBuffer, outputBuffer);
      outputBuffer.limit(size);
      inputBuffer.limit(0);
      inputBuffer.rewind();
    }

    
    int numBytes = Math.min(len, outputBuffer.remaining());
    outputBuffer.get(buffer, off, numBytes);    
    bytesWritten += numBytes;
    return numBytes;	    
  }
@ Override public synchronized int compress ( byte [ ] buffer , int off , int len ) throws IOException { SnappyUtil . validateBuffer ( buffer , off , len ) ; if ( needsInput ( ) ) { // No buffered output bytes and no input to consume, need more input return 0 ; } if ( ! outputBuffer . hasRemaining ( ) ) { // There is uncompressed input, compress it now int maxOutputSize = Snappy . maxCompressedLength ( inputBuffer . position ( ) ) ; if ( maxOutputSize > outputBuffer . capacity ( ) ) { ByteBuffer oldBuffer = outputBuffer ; outputBuffer = ByteBuffer . allocateDirect ( maxOutputSize ) ; CleanUtil . clean ( oldBuffer ) ; } // Reset the previous outputBuffer outputBuffer . clear ( ) ; inputBuffer . limit ( inputBuffer . position ( ) ) ; inputBuffer . position ( 0 ) ; int size = Snappy . compress ( inputBuffer , outputBuffer ) ; outputBuffer . limit ( size ) ; inputBuffer . limit ( 0 ) ; inputBuffer . rewind ( ) ; } // Return compressed output up to 'len' int numBytes = Math . min ( len , outputBuffer . remaining ( ) ) ; outputBuffer . get ( buffer , off , numBytes ) ; bytesWritten += numBytes ; return numBytes ; }
Fills specified buffer with compressed data. Returns actual number
of bytes of compressed data. A return value of 0 indicates that
needsInput() should be called in order to determine if more input
data is required.

@param buffer   Buffer for the compressed data
@param off Start offset of the data
@param len Size of the buffer
@return The actual number of bytes of compressed data.
Fills specified buffer with compressed data . Returns actual number of bytes of compressed data . A return value of 0 indicates that needsInput () should be called in order to determine if more input data is required .