import {Buffer} from "buffer"; import {AxdrEnum} from "../asn1.axdr/AxdrEnum"; import {Data} from "./Data"; import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; export class Region implements AxdrType{ dataCode : Buffer | null = null; unit : AxdrEnum | null = null; sval : Data | null = null; eval : Data | null = null; constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } set_all(unit : AxdrEnum, sval : Data, evl : Data){ this.unit = unit; this.sval = sval; this.eval = evl; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; this.unit = new AxdrEnum(); this.unit.set_const(); codeLength += this.unit.decode(input); this.sval = new Data(); codeLength += this.sval.decode(input); this.eval = new Data(); codeLength += this.eval.decode(input); return codeLength; } encode(output: ReverseByteArrayOutputStream): number { let codeLength; if (this.dataCode != null) { codeLength = this.dataCode.length; for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } } else { if(this.unit != null && this.eval && this.sval != null){ codeLength = 0; codeLength += this.eval.encode(output); codeLength += this.sval.encode(output); codeLength += this.unit.encode(output); }else{ throw new Error("unit or sval or eval is null"); } } return codeLength; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } toString(): string { if (this.unit != null && this.sval != null && this.eval != null) { return "sequence: {"+ "unit: " + this.unit + ", sval: " + this.sval + ", eval: " + this.eval + "}"; } else { return "unit or sval or eval is null"; } } }