@foxglove/ros1
Version:
Standalone TypeScript implementation of the ROS 1 (Robot Operating System) protocol with a pluggable transport layer
23 lines (20 loc) • 566 B
text/typescript
export function concatData(chunks: Uint8Array[]): Uint8Array {
if (chunks.length === 1 && chunks[0] != null) {
return chunks[0];
}
const totalLength = chunks.reduce((len, chunk) => len + chunk.length, 0);
let result: Uint8Array | undefined;
try {
result = new Uint8Array(totalLength);
} catch (err) {
throw new Error(
`failed to allocate ${totalLength} bytes to concatenate ${chunks.length} chunks`,
);
}
let idx = 0;
for (const chunk of chunks) {
result.set(chunk, idx);
idx += chunk.length;
}
return result;
}