Convert ByteBuffer To Byte Array Java


Answer :

ByteBuffer exposes the bulk get(byte[]) method which transfers bytes from the buffer into the array. You'll need to instantiate an array of length equal to the number of remaining bytes in the buffer.



ByteBuffer buf = ...
byte[] arr = new byte[buf.remaining()];
buf.get(arr);


If hasArray() reports false then, calling array() will throw an exception.



In that case, the only way to get the data in a byte[] is to allocate a byte[] and copy the bytes to the byte[] using get(byte) or similar.



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable