Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 646
Some Name Avatar asked Oct 09 '18 06:10

Some Name


People also ask

How do you create a byte buffer in Java?

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.

What is a byte buffer Java?

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.

What is GZIP in Java?

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.


1 Answers

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.

like image 164
Holger Avatar answered Oct 18 '22 19:10

Holger