问题
I am using JNI code in an Android project in which the JNI native function requires a short[] argument. However, the original data is stored as a ByteBuffer. I'm trying the convert the data format as follows.
ByteBuffer rgbBuf = ByteBuffer.allocate(size);
...
short[] shortArray = (short[]) rgbBuf.asShortBuffer().array().clone();
But I encounter the following problem when running the second line of code shown above:
E/AndroidRuntime(23923): Caused by: java.lang.UnsupportedOperationException
E/AndroidRuntime(23923): at Java.nio.ShortToByteBufferAdapter.protectedArray(ShortToByteBufferAdapter.java:169)
Could anyone suggest a means to implement the conversion?
回答1:
The method do this is a bit odd, actually. You can do it as below; ordering it is important to convert it to a short
array.
short[] shortArray = new short[size/2];
rgbBuf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArray);
Additionally, you may have to use allocateDirect
instead of allocate
.
回答2:
I had the same error with anything that used asShortBuffer()
. Here's a way around it (adapted from 2 bytes to short java):
short[] shortArray = new short[rgbBuf.capacity() / 2]);
for (int i=0; i<shortArray.length; i++)
{
ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(rgbBuf[2*i]);
bb.put(rgbBuf[2*i + 1]);
shortArray[i] = bb.getShort(0);
}
来源:https://stackoverflow.com/questions/11930385/how-can-i-get-short-from-a-bytebuffer