Search code examples
javacompressiongzipnio

compression on java nio direct buffers


The gzip input/output stream dont operate on Java direct buffers.

Is there any compression algorithm implementation out there that operates directly on direct buffers?

This way there would be no overhead of copying a direct buffer to a java byte array for compression.


Solution

  • I don't mean to detract from your question, but is this really a good optimization point in your program? Have you verified with a profiler that you indeed have a problem? Your question as stated implies you have not done any research, but are merely guessing that you will have a performance or memory problem by allocating a byte[]. Since all the answers in this thread are likely to be hacks of some sort, you should really verify that you actually have a problem before fixing it.

    Back to the question, if you're wanting to compress the data "in place" in on a ByteBuffer, the answer is no, there is no capability to do that built into Java.

    If you allocated your buffer like the following:

    byte[] bytes = getMyData();
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    

    You can filter your byte[] through a ByteBufferInputStream as the previous answer suggested.