Search code examples
javajvmgzipniobytebuffer

Is there a way to gzip a byte buffer in Java?


I have pretty huge DirectByteBuffer and I would like to produce a gzipped DirectByteBuffer from it without transferring its content to the heap.

The standard java.util.Deflater cannot be helpful since it operates on byte[] which is on-heap by definition.

Is there a way to do this in Java? Or I have to call libzip directly through JNI?


Solution

  • Starting with Java 11, there are

    and

    allowing to specify input and output as byte buffers. Of course, it’s implementation dependent whether this actually allows a direct off-heap processing, but a quick look into OpenJDK revealed that it has a native method for the buffer-to-buffer processing.

    Technically, it’s not GZip, unless you’re also writing the artifacts of that file format, but I suppose, you’re mainly interested in the compression rather than the file format.