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

有帮助吗?

解决方案

try this

ByteBuffer buf = ByteBuffer.allocate(500);
buf.putInt(1);
...
...
byte[] a = new byte[buf.position()];
buf.rewind();
buf.get(a);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top