2016-09-15 37 views
0

我需要將2個現有arraybuffers的2個部分合併爲一個新的。Javascript組合ArrayBuffer部件

我正在構建一個解析器,並且數據以隨機大小的數組出現,數據將溢出到另一個的末尾。所以我需要創建一個新的輸出緩衝區並複製一個緩衝區的一部分和另一個緩衝區的一部分。輸出將只是一個Arraybuffer。

從這個演示開始,我打算用一些偏移量來製作Uint8Arrays然後使用set,問題是某些組合拋出Invalid typed array length。我不會事先知道每個陣列的長度或偏移量。

var buffer1 = new ArrayBuffer(8); 
var buffer2 = new ArrayBuffer(8); 
var buffer3 = new ArrayBuffer(8); 

var uint8_1 = new Uint8Array(buffer1); 
var uint8_2 = new Uint8Array(buffer2); 
var uint8_3 = new Uint8Array(buffer3); 

uint8_1.fill(1); 
uint8_2.fill(2); 

var uint8_1_slice = new Uint8Array(buffer1 , 0 , 3); 
var uint8_2_slice = new Uint8Array(buffer2 , 4, 7); 

對於這個演示需要得到buffer3爲1,1,1,1,2,2,2,2。

無法使用切片

回答

1

在該演示中需要得到buffer3是1,1,1,1,2,2,2,2。

您可以使用for循環,設置uint8_3uint8_1值,如果變量n小於uint8_1.byteLength/2,否則設置uint8_3uint8_2重視。

var len = 8; 
 

 
var buffer1 = new ArrayBuffer(len); 
 
var buffer2 = new ArrayBuffer(len); 
 
var buffer3 = new ArrayBuffer(len); 
 

 
var uint8_1 = new Uint8Array(buffer1); 
 
var uint8_2 = new Uint8Array(buffer2); 
 
var uint8_3 = new Uint8Array(buffer3); 
 

 
uint8_1.fill(1); 
 
uint8_2.fill(2); 
 
// `len` : uint8_1.byteLength/2 + uint8_2.byteLength/2 
 
for (var n = 0; n < len; n++) { 
 
    uint8_3[n] = n < len/2 ? uint8_1[n] : uint8_2[n]; 
 
} 
 

 
console.log(uint8_3);

+0

這是在正確的軌道上,它應該使用set,因爲如果不是,你一次只複製1個字節。 – brianxautumn

+0

其實對不起,不能使用slice,它會返回一個新的數組。 – brianxautumn

+0

@brianxautumn _「其實對不起,不能使用分片,它會返回一個新的數組」_不確定你的意思?是否需要不創建一個新的數組? – guest271314

0

我看到有些人只使用array.length。只要數組每個元素只有1個字節就沒問題。如果其他類型的數組被填滿也是好的,但在這個例子中a2不是。這就是爲什麼使用byteLength更好,這也是Blob構造函數如何連接部分。

// Concatenate a mix of typed arrays 
function concatenate(...arrays) { 
    // Calculate byteSize from all arrays 
    let size = arrays.reduce((a,b) => a + b.byteLength, 0) 
    // Allcolate a new buffer 
    let result = new Uint8Array(size) 

    // Build the new array 
    let offset = 0 
    for (let arr of arrays) { 
    result.set(arr, offset) 
    offset += arr.byteLength 
    } 

    return result 
} 

// the total length of 1-3 = 5 
// the total byteLength of 1-3 = 6 
let a1 = Uint8Array.of(1, 2) // [1, 2] 
let a2 = Uint16Array.of(3) // [3] just for the fun of it 16 takes up 2 bytes 
let a3 = Uint8Array.of(4, 5) // [4, 5] 

concatenate(a1, a2, a3) // [1, 2, 3, 0, 4, 5] 

/********/ 
var blob = new Blob([a1, a2, a3]) 
var res = new Response(blob) 
res.arrayBuffer().then(buffer => console.log(new Uint8Array(buffer))) 
// [1, 2, 3, 0, 4, 5]