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
?
A ByteBuffer is created via the the two static factory methods: allocate(int) this will allocate a HeapByteBuffer with the capacity specified by the int argument. allocateDirect(int) this will allocate a DirectByteBuffer with the capacity specified by the int argument.
A ByteBuffer is a buffer which provides for transferring bytes from a source to a destination. In addition to storage like a buffer array, it also provides abstractions such as current position, limit, capacity, etc. A FileChannel is used for transferring data to and from a file to a ByteBuffer.
gzip is a core component of the Java platform, and many web servers have the ability to compress content independent of the files or applications it serves up. Customers can use gzip in conjunction with an Axis SOAP implementation.
Starting with Java 11, there are
Deflater.setInput(ByteBuffer input)
and
Deflater.deflate(ByteBuffer output)
resp.Deflater.deflate(java.nio.ByteBuffer output, int flush)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With