【问题标题】:Android UsbRequest.queue(ByteBuffer) ignores position, limit, etc.?Android UsbRequest.queue(ByteBuffer) 忽略位置、限制等?
【发布时间】:2016-12-22 23:35:55
【问题描述】:

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

它似乎忽略了 ByteBuffer 的位置,总是从缓冲区中的位置 0 开始写入。然后它将缓冲区的位置设置为读取的字节?

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

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

谢谢, 马格努斯

【问题讨论】:

    标签: java android usb usb-drive


    【解决方案1】:

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

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

    好的,我找到了使用 ByteBuffer#wrap(byte[] array, 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 2014-11-20
      • 2012-03-27
      相关资源
      最近更新 更多