I have
ByteBuffer buf = ByteBuffer.allocateDirect(500);
Then I use buf.put(byte) a couple of times, say 20.
I save it to sql blob via the underlying array, i.e. by calling buf.array(). However, the length of buf.array() is 500. How to get an array (byte[]) of length 20 that can be passed to other (read-only) functions? (without copying, of course.)
allocateDirect, the buffer does not have an underlying array. But even if it has an array, you can not get a portion of an array in Java in general. So there is no way of doing this without copies. If you have the possibility to change the receiving method, you could give it abytebuffer.slice(), which is a view on a portion of the byte buffer (namely, the portion between its currentpositionandlimit- which you can set accordingly before callingslice), or alternatively, let it receive the fullbyte[], anint positionand anint length(which is done often)