Skip to content

Commit

Permalink
feat: add concatArrayBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Dec 22, 2024
1 parent 35c79f5 commit da51c78
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function clamp(min: number, max: number, n: number): number {

/**
* @see https://stackoverflow.com/a/49129872/1806628
* @deprecated This will be superceeded by concatArrayBuffers
*/
export function concatUint8Arrays(buffers: Uint8Array[]): Uint8Array {
if (buffers.length === 0) {
Expand All @@ -97,3 +98,26 @@ export function concatUint8Arrays(buffers: Uint8Array[]): Uint8Array {

return combinedBuffer
}

/**
* @see https://stackoverflow.com/a/49129872/1806628
*/
export function concatArrayBuffers(buffers: ArrayBuffer[]): ArrayBuffer {
if (buffers.length === 0) {
return new ArrayBuffer(0)
}

const totalLength = buffers.reduce((sum, buffer) => {
return sum + buffer.byteLength
}, 0)

const combinedBuffer = new Uint8Array(totalLength)

let offset = 0
buffers.forEach((buffer) => {
combinedBuffer.set(new Uint8Array(buffer), offset)
offset = offset + buffer.byteLength
})

return combinedBuffer.buffer
}

0 comments on commit da51c78

Please sign in to comment.