|
class bufferUtil { |
|
|
|
|
|
|
|
|
|
|
|
|
|
bufferToArray (buffer, process) { |
|
buffer = new DataView(buffer); |
|
const array = []; |
|
const processNum = typeof process === 'function' |
|
? process |
|
: num => num; |
|
for (let idx = 0; idx < buffer.byteLength; idx++) { |
|
const number = buffer.getUint8(idx); |
|
array.push(processNum(number)); |
|
} |
|
return array; |
|
} |
|
|
|
arrayToBuffer (array, patch) { |
|
patch = typeof process === 'function' |
|
? patch |
|
: num => num; |
|
const buffer = new ArrayBuffer(array.length); |
|
const view = new DataView(buffer); |
|
array.forEach((byte, offset) => { |
|
byte = patch(byte); |
|
view.setUint8(offset, byte); |
|
}); |
|
return view.buffer; |
|
} |
|
|
|
uint8ArrayToBuffer (array) { |
|
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset); |
|
} |
|
bufferToUint8Array (buffer) { |
|
return new Uint8Array(buffer); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bufferToString (buffer, spacer, process) { |
|
if (!spacer) spacer = ' '; |
|
const array = this.bufferToArray(buffer) |
|
.map(typeof process === 'function' |
|
? process |
|
: num => num.toString(16)) |
|
.map(byte => { |
|
if (byte.length < 2) return `0${byte}`; |
|
return byte; |
|
}); |
|
return array.join(spacer); |
|
} |
|
} |
|
|
|
module.exports = bufferUtil; |
|
|