Created
April 9, 2012 22:33
-
-
Save lenn0x/2347094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static byte[] getArray(ByteBuffer buffer) | |
{ | |
int length = buffer.remaining(); | |
if (buffer.hasArray()) | |
{ | |
int boff = buffer.arrayOffset() + buffer.position(); | |
if (boff == 0 && length == buffer.array().length) | |
return buffer.array(); | |
else | |
return Arrays.copyOfRange(buffer.array(), boff, boff + length); | |
} | |
// else, DirectByteBuffer.get() is the fastest route | |
byte[] bytes = new byte[length]; | |
buffer.duplicate().get(bytes); | |
return bytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment