@gmod/cram
Version:
read CRAM files with pure Javascript
31 lines • 1.43 kB
JavaScript
import CramCodec from "./_base.js";
import { CramMalformedError } from "../../errors.js";
import { CramBufferOverrunError } from "./getBits.js";
export default class ByteArrayStopCodec extends CramCodec {
decode(_slice, _coreDataBlock, blocksByContentId, cursors) {
const { blockContentId } = this.parameters;
const contentBlock = blocksByContentId[blockContentId];
if (!contentBlock) {
throw new CramMalformedError(`no block found with content ID ${blockContentId}`);
}
const cursor = cursors.externalBlocks.getCursor(blockContentId);
return this._decodeByteArray(contentBlock, cursor);
}
_decodeByteArray(contentBlock, cursor) {
const dataBuffer = contentBlock.content;
const { stopByte } = this.parameters;
// scan to the next stop byte
const startPosition = cursor.bytePosition;
let stopPosition = cursor.bytePosition;
while (dataBuffer[stopPosition] !== stopByte &&
stopPosition < dataBuffer.length) {
if (stopPosition === dataBuffer.length) {
throw new CramBufferOverrunError('byteArrayStop reading beyond length of data buffer?');
}
stopPosition = stopPosition + 1;
}
cursor.bytePosition = stopPosition + 1;
return dataBuffer.subarray(startPosition, stopPosition);
}
}
//# sourceMappingURL=byteArrayStop.js.map