@Override
  public int compress(byte[] b, int off, int len) throws IOException {
    // If we have previously compressed our input and still have some buffered bytes
    // remaining, provide them to the caller.
    if (outBuf.hasRemaining()) {
      int remaining = outBuf.remaining(), n = Math.min(remaining, len);
      outBuf.get(b, off, n);
      return n;
    }
    // We don't actually begin compression until our caller calls finish().
    // The aircompressor compressors operate over a range of input in one shot.
    if (finish) {
      if (inBuf.position() > 0) {
        inBuf.flip();
        int uncompressed = inBuf.remaining();
        // If we don't have enough capacity in our currently allocated output buffer,
        // allocate a new one which does.
        int needed = maxCompressedLength(uncompressed);
        // Can we decompress directly into the provided array?
        ByteBuffer writeBuffer;
        boolean direct = false;
        if (len <= needed) {
          writeBuffer = ByteBuffer.wrap(b, off, len);
          direct = true;
        } else {
          if (outBuf.capacity() < needed) {
            needed = CompressionUtil.roundInt2(needed);
            outBuf = ByteBuffer.allocate(needed);
          } else {
            outBuf.clear();
          }
          writeBuffer = outBuf;
        }
        final int oldPos = writeBuffer.position();
        compressor.compress(inBuf, writeBuffer);
        final int written = writeBuffer.position() - oldPos;
        bytesWritten += written;
        inBuf.clear();
        finished = true;
        if (!direct) {
          outBuf.flip();
          int n = Math.min(written, len);
          outBuf.get(b, off, n);
          return n;
        } else {
          return written;
        }
      } else {
        finished = true;
      }
    }
    return 0;
  }
Hadoop compressor glue for aircompressor compressors.
hbase-compression/hbase-compression-aircompressor/src/main/java/org/apache/hadoop/hbase/io/compress/aircompressor/HadoopCompressor.java