将字节数组分割成相等的队列字节数组

huangapple go评论74阅读模式
英文:

Partition ByteArray to equally ByteArray of Queue

问题

所以,我从服务器得到一个动态大小的字节数组,我需要将其分成15个等大小的部分,并将其添加到我的Queue<ByteArray>中。那么我该如何做呢?

实际上,我需要为我的BLE设备做这个。我正在进行固件更新,我需要将字节数组均匀地分成每个包含20字节的部分,以便更新过程更加平稳进行。

例如,从服务器收到的字节数组长度为256。我希望我的:

queue[0]=bytes[0-15] onpeek() 
queue[1]=bytes[16-30] onPeek()
queue[2]=bytes[31-45] onPeek()
...
....
queue[n]= bytes[240-255] onPeek()

我的代码:

private val sendQueue: Queue<ByteArray> = ConcurrentLinkedQueue()

@Volatile
private var isWriting = false

fun send(
    dataByte: ByteArray,
    gatt: BluetoothGatt
): Int {
    var data = dataByte
    while (data.count() > 15) {

        // 待办事项:将字节数组分成20个等大小的部分,并将其添加到sendQueue中。

    }
    sendQueue.add(data)

    if (!isWriting) _send(gatt)
    return 0 // 0
}

(注意:以上为您提供的代码翻译,代码中的注释仍然是英文的,因为您要求不翻译代码部分的内容。)

英文:

So, I have a ByteArray of dynamic size from the server and I need to divide it into 15 equal size and add it to my Queue&lt;ByteArray&gt;. So how do I do this?

I need this for my BLE device actually. I'm doing a firmware update and I need to convert the bytearray into equally 20 bytes each inside the Queue so that it happens smoothly?

for example bytearray received from the server is 256. I want to have my

queue[0]=bytes[0-15] onpeek() 
queue[1]=bytes[16-30] onPeek()
queue[2]=bytes[31-45]onPeek()
 ...
....
queue[n]= bytes[240-255] onPeek()

My code:

private val sendQueue: Queue&lt;ByteArray&gt; = ConcurrentLinkedQueue()

    @Volatile
    private var isWriting = false


    fun send(
        dataByte: ByteArray,
        gatt: BluetoothGatt
    ): Int {
        var data = dataByte
        while (data.count() &gt; 15) {

//            todo divide into 20 equal byte array and add it to sendQueue.
           
        }
        sendQueue.add(data)

        if (!isWriting) _send(gatt)
        return 0 //0
    }

答案1

得分: 1

你可以尝试类似这样的代码(草稿,未经检查)

var partCount = 20;
var data = dataByte;
var len = data.getLength();
var partSize = len / partCount;  
for (int i = 0; i < partCount - 1; i++) {    
    var newArray = Arrays.copyOfRange(bytes, i * partSize, (i + 1) * partSize);
    sendQueue.add(newArray);
}
// 并且我们添加了最后一部分(如果 "len % 20 != 0",它可能会比其他部分稍大)。
var newArray = Arrays.copyOfRange(bytes, partSize * (partCount - 1), len); 
sendQueue.add(newArray);
英文:

You can try something like this (draft, not checking)

var partCount = 20;
var data = dataByte;
var len = data.getLength();
var partSize = len / partCount;  
for (int i = 0; i &lt; partCount - 1; i++) {    
    var newArray = Arrays.copyOfRange(bytes, i * partSize, (i + 1) * partSize);
    sendQueue.add(newArray);
}
// and we added last part (may be a little bigger then other parts if &quot;len % 20 != 0&quot;).
var newArray = Arrays.copyOfRange(bytes, partSize * (partCount - 1), len); 
sendQueue.add(newArray);

答案2

得分: 1

我不确定是否理解您的问题,但这可以指引方向:

dataByte.asIterable().chunked(15).forEach { 
    // 'it' 是一个 List<Byte>,将其转换为 ByteArray 以便添加到队列。
    sendQueue.add(it.toByteArray())
}

dataByte定义为Iterable将允许您将其按您想要的大小进行分块,然后您只需遍历其结果并将其添加到队列中。

英文:

I'm not sure if understood your question but this can give a direction

dataByte.asIterable().chunked(15).forEach { 
    // &#39;it&#39; is a List&lt;Byte&gt;, convert it to ByteArray in order to add it to queue.
    sendQueue.add(it.toByteArray())
}

referring dataByte as Iterable will allow you to chunk it to the size you want,
then all you need is to iterate over it's results and add it to your queue.

huangapple
  • 本文由 发表于 2020年10月20日 20:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64445694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定