slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /* 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/. */ |
| 4 | package org.onosproject.xran.codecs.ber.types; |
| 5 | |
| 6 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 7 | import com.fasterxml.jackson.annotation.JsonValue; |
| 8 | import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream; |
| 9 | import org.onosproject.xran.codecs.ber.BerLength; |
| 10 | import org.onosproject.xran.codecs.ber.BerTag; |
| 11 | |
| 12 | import java.io.EOFException; |
| 13 | import java.io.IOException; |
| 14 | import java.io.InputStream; |
| 15 | import java.io.Serializable; |
| 16 | |
| 17 | public 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; |
| 21 | @JsonIgnore |
| 22 | public byte[] code = null; |
| 23 | |
| 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 | } |