Question

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.)

Was it helpful?

Solution

try this

ByteBuffer buf = ByteBuffer.allocate(500);
buf.putInt(1);
...
...
byte[] a = new byte[buf.position()];
buf.rewind();
buf.get(a);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top