Android UsbRequest.queue(ByteBuffer)忽略位置,限制等?

时间:2016-12-22 23:35:55

标签: java android usb usb-drive

我在使用UsbRequest.queue(ByteBuffer,int)时遇到了问题。

它似乎忽略了ByteBuffer的位置,并始终在缓冲区中的位置0处开始写入。然后它将缓冲区的位置设置为读取的字节数?

https://android.googlesource.com/platform/frameworks/base.git/+/a3665ba95d806fcb6780d29d49bd0f1032e8bc86%5E%21/#F0

怎么可能,这只是被忽略了?关于如何在ByteBuffer中指定起始位置的任何想法?

谢谢, 马格努斯

1 个答案:

答案 0 :(得分:0)

EDIT2:所以我现在基本上正在做的是使用临时缓冲区,然后将数据相应地复制到最终缓冲区。

编辑:这不是解决方案! wrap只是将ByteBuffer的位置设置为offset。

好的,我找到了一个解决方法,使用ByteBuffer #wrap(byte []数组,int offset,int length)

    int length = dest.remaining();

    // workaround: UsbRequest.queue always writes at position 0 :/
    ByteBuffer tmp = ByteBuffer.wrap(dest.array(), dest.position(), length);
    if(!inRequest.queue(tmp, length)) {
        throw new IOException("Error queueing request.");
    }

    UsbRequest request = deviceConnection.requestWait();
    if (request == inRequest) {
        dest.position(dest.position() + tmp.position());
        return tmp.position();
    }

    throw new IOException("requestWait failed! Request: " + request);