public bool compress(ByteBuffer @in, ByteBuffer @out, ByteBuffer overflow) { int inBytes = @in.remaining(); // I should work on a patch for Snappy to support an overflow buffer // to prevent the extra buffer copy. byte[] compressed = new byte[Snappy.maxCompressedLength(inBytes)]; int outBytes = Snappy.compress(@in.array(), @in.arrayOffset() + @in.position(), inBytes, compressed, 0); if (outBytes < inBytes) { int remaining = @out.remaining(); if (remaining >= outBytes) { Array.Copy(compressed, 0, @out.array(), @out.arrayOffset() + @out.position(), outBytes); @out.position(@out.position() + outBytes); } else { Array.Copy(compressed, 0, @out.array(), @out.arrayOffset() + @out.position(), remaining); @out.position(@out.limit()); Array.Copy(compressed, remaining, overflow.array(), overflow.arrayOffset(), outBytes - remaining); overflow.position(outBytes - remaining); } return(true); } else { return(false); } }