import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; import {Buffer} from "buffer"; import {AxdrEnum} from "../asn1.axdr/AxdrEnum"; import {Data} from "./Data"; export enum Choice { _ERR_NONE_SELECTED = -1, DAR = 0, DATA = 1, } export class Get_Result implements AxdrType{ dataCode : Buffer | null = null; choice : Choice = Choice._ERR_NONE_SELECTED; dar : AxdrEnum | null = null; data : Data | null = null; constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } setDar(dar : AxdrEnum){ this.resetChoices(); this.choice = Choice.DAR; this.dar = dar; } setData(data : Data){ this.resetChoices(); this.choice = Choice.DATA; this.data = data; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; let choosen = new AxdrEnum(); choosen.set_const(); codeLength += choosen.decode(input); this.resetChoices(); this.choice = choosen.getValue(); if (this.choice == Choice.DAR) { this.dar = new AxdrEnum(); this.dar.set_const(); codeLength += this.dar.decode(input); return codeLength; } if (this.choice == Choice.DATA) { this.data = new Data(); codeLength += this.data.decode(input); return codeLength; } throw new Error("Error decoding AxdrChoice: Identifier matched to no item."); } encode(output: ReverseByteArrayOutputStream): number { if (this.dataCode != null) { for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } return this.dataCode.length; } if (this.choice == Choice._ERR_NONE_SELECTED) { throw new Error("Error encoding AxdrChoice: No item in choice was selected."); } let codeLength = 0; if (this.choice == Choice.DATA) { codeLength += this.data.encode(output); let c = new AxdrEnum(); c.set_min_max_val(1) codeLength += c.encode(output); return codeLength; } if (this.choice == Choice.DAR) { codeLength += this.dar.encode(output); let c = new AxdrEnum(); c.set_min_max_val(0) codeLength += c.encode(output); return codeLength; } throw new Error("Error encoding AxdrChoice: No item in choice was encoded."); } resetChoices() { this.choice = Choice._ERR_NONE_SELECTED; this.dar = null; this.data = null; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } getChoiceIndex(): Choice { return this.choice; } toString(): string { if (this.choice == Choice.DAR) { if (this.dar != null) { return "choice: {dar: " + this.dar + "}"; } else { return "choice is dar but dar is null"; } }else if(this.choice == Choice.DATA){ if(this.data != null){ return "choice: {data: " + this.data + "}"; }else{ return "choice is data but data is null"; } } else { return "unknown"; } } }