Compression on java nio direct buffers

The gzip I / O stream does not work with direct Java buffers.

Is there any implementation of a compression algorithm out there that works directly with direct buffers?

Thus, there would be no overhead of copying the direct buffer to the java byte array for compression.

+7
source share
2 answers

I do not want to be distracted from your question, but is this really a good point of optimization in your program? Have you checked with the profiler that you really have a problem? Your question, as said, implies that you have not done any research, but simply assume that you will have a performance or memory problem by highlighting byte []. Since all the answers in this thread are likely to be some kind of hacks, you should really make sure that you really have a problem before fixing it.

Back to the question, if you want to compress the data "in place" in ByteBuffer, the answer will be negative, there is no way to do this, built into Java.

If you allocated your buffer as follows:

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

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

+2
source

If you use ByteBuffers, you can use some simple Input / OutputStream wrappers, such as:

 public class ByteBufferInputStream extends InputStream { private ByteBuffer buffer = null; public ByteBufferInputStream( ByteBuffer b) { this.buffer = b; } @Override public int read() throws IOException { return (buffer.get() & 0xFF); } } public class ByteBufferOutputStream extends OutputStream { private ByteBuffer buffer = null; public ByteBufferOutputStream( ByteBuffer b) { this.buffer = b; } @Override public void write(int b) throws IOException { buffer.put( (byte)(b & 0xFF) ); } } 

Test:

 ByteBuffer buffer = ByteBuffer.allocate( 1000 ); ByteBufferOutputStream bufferOutput = new ByteBufferOutputStream( buffer ); GZIPOutputStream output = new GZIPOutputStream( bufferOutput ); output.write("stackexchange".getBytes()); output.close(); buffer.position( 0 ); byte[] result = new byte[ 1000 ]; ByteBufferInputStream bufferInput = new ByteBufferInputStream( buffer ); GZIPInputStream input = new GZIPInputStream( bufferInput ); input.read( result ); System.out.println( new String(result)); 
0
source

All Articles