@gmod/cram
Version:
read CRAM files with pure Javascript
40 lines • 1.53 kB
JavaScript
import CramCodec from "./_base.js";
export default class ByteArrayLengthCodec extends CramCodec {
instantiateCodec;
_lengthCodecCache;
_dataCodecCache;
constructor(parameters, dataType, instantiateCodec) {
super(parameters, dataType);
this.instantiateCodec = instantiateCodec;
}
decode(slice, coreDataBlock, blocksByContentId, cursors) {
const lengthCodec = this._getLengthCodec();
const arrayLength = lengthCodec.decode(slice, coreDataBlock, blocksByContentId, cursors);
if (arrayLength > 0) {
const dataCodec = this._getDataCodec();
const subarray = dataCodec.getBytesSubarray(blocksByContentId, cursors, arrayLength);
if (subarray) {
return subarray;
}
else {
const data = new Uint8Array(arrayLength);
for (let i = 0; i < arrayLength; i += 1) {
data[i] = dataCodec.decode(slice, coreDataBlock, blocksByContentId, cursors);
}
return data;
}
}
else {
return new Uint8Array(0);
}
}
_getLengthCodec() {
this._lengthCodecCache ??= this.instantiateCodec(this.parameters.lengthsEncoding, 'int');
return this._lengthCodecCache;
}
_getDataCodec() {
this._dataCodecCache ??= this.instantiateCodec(this.parameters.valuesEncoding, 'byte');
return this._dataCodecCache;
}
}
//# sourceMappingURL=byteArrayLength.js.map