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.
Jonathan S. Fisher
source share