blob: 34a26af4964f58ab307fcfbd0f37c568c012ba53 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08004package org.onosproject.xran.asn1lib.ber.types;
slowr60d4d102017-08-16 18:33:58 -07005
6import com.fasterxml.jackson.annotation.JsonIgnore;
7import com.fasterxml.jackson.annotation.JsonValue;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08008import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
9import org.onosproject.xran.asn1lib.ber.BerLength;
10import org.onosproject.xran.asn1lib.ber.BerTag;
slowr60d4d102017-08-16 18:33:58 -070011
12import java.io.EOFException;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.Serializable;
16
17public class BerBoolean implements Serializable {
18
19 public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.BOOLEAN_TAG);
20 private static final long serialVersionUID = 1L;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080021
22 @JsonIgnore public byte[] code = null;
slowr60d4d102017-08-16 18:33:58 -070023
24 public boolean value;
25
26 public BerBoolean() {
27 }
28
29 public BerBoolean(byte[] code) {
30 this.code = code;
31 }
32
33 public BerBoolean(boolean value) {
34 this.value = value;
35 }
36
37 public int encode(BerByteArrayOutputStream os) throws IOException {
38 return encode(os, true);
39 }
40
41 public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException {
42
43 if (code != null) {
44 for (int i = code.length - 1; i >= 0; i--) {
45 os.write(code[i]);
46 }
47 if (withTag) {
48 return tag.encode(os) + code.length;
49 }
50 return code.length;
51 }
52
53 int codeLength = 1;
54
55 if (value) {
56 os.write(0xff);
57 } else {
58 os.write(0);
59 }
60
61 codeLength += BerLength.encodeLength(os, codeLength);
62
63 if (withTag) {
64 codeLength += tag.encode(os);
65 }
66
67 return codeLength;
68 }
69
70 public int decode(InputStream is) throws IOException {
71 return decode(is, true);
72 }
73
74 public int decode(InputStream is, boolean withTag) throws IOException {
75
76 int codeLength = 0;
77
78 if (withTag) {
79 codeLength += tag.decodeAndCheck(is);
80 }
81
82 BerLength length = new BerLength();
83 codeLength += length.decode(is);
84
85 if (length.val != 1) {
86 throw new IOException("Decoded length of BerBoolean is not correct");
87 }
88
89 int nextByte = is.read();
90 if (nextByte == -1) {
91 throw new EOFException("Unexpected end of input stream.");
92 }
93
94 codeLength++;
95 if (nextByte == 0) {
96 value = false;
97 } else {
98 value = true;
99 }
100
101 return codeLength;
102 }
103
104 public void encodeAndSave(int encodingSizeGuess) throws IOException {
105 BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
106 encode(os, false);
107 code = os.getArray();
108 }
109
110 @JsonValue
111 @Override
112 public String toString() {
113 return "" + value;
114 }
115}