5

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?

Holger
  • 285,553
  • 42
  • 434
  • 765
Some Name
  • 8,555
  • 5
  • 27
  • 77

1 Answers1

8

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.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Very interesting, thanks. May I ask you about the previous version of JDK? I currently use JDK8 since Scala does not work properly on JDK 9+, so it is impossible to migrate right now. So is the only solution on JDK 8 is to call libzip explicitly? – Some Name Oct 09 '18 at 09:51
  • I don’t know of any earlier solution. So you have to call libzip directly or update the JDK. I think, knowing that there will be a simple solution in the current version discourages from spending too much effort on interfacing with native libraries in an older version… – Holger Oct 09 '18 at 10:01