blob: 14a8665d2ff90df62a87d93c3569767c3e9abb2a [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.string;
slowr60d4d102017-08-16 18:33:58 -07005
6import com.fasterxml.jackson.annotation.JsonValue;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08007import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
8import org.onosproject.xran.asn1lib.ber.BerLength;
9import org.onosproject.xran.asn1lib.ber.BerTag;
10import org.onosproject.xran.asn1lib.ber.internal.Util;
slowr60d4d102017-08-16 18:33:58 -070011
12import java.io.IOException;
13import java.io.InputStream;
14import java.io.Serializable;
15
16public class BerVisibleString implements Serializable {
17
18 public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.VISIBLE_STRING_TAG);
19 private static final long serialVersionUID = 1L;
20 public byte[] value;
21
22 public BerVisibleString() {
23 }
24
25 public BerVisibleString(byte[] value) {
26 this.value = value;
27 }
28
29 public BerVisibleString(String valueAsString) {
30 value = valueAsString.getBytes();
31 }
32
33 public int encode(BerByteArrayOutputStream os) throws IOException {
34 return encode(os, true);
35 }
36
37 public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException {
38
39 os.write(value);
40 int codeLength = value.length;
41
42 codeLength += BerLength.encodeLength(os, codeLength);
43
44 if (withTag) {
45 codeLength += tag.encode(os);
46 }
47
48 return codeLength;
49 }
50
51 public int decode(InputStream is) throws IOException {
52 return decode(is, true);
53 }
54
55 public int decode(InputStream is, boolean withTag) throws IOException {
56
57 int codeLength = 0;
58
59 if (withTag) {
60 codeLength += tag.decodeAndCheck(is);
61 }
62
63 BerLength length = new BerLength();
64 codeLength += length.decode(is);
65
66 value = new byte[length.val];
67
68 if (length.val != 0) {
69 Util.readFully(is, value);
70 codeLength += length.val;
71 }
72
73 return codeLength;
74
75 }
76
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080077
slowr60d4d102017-08-16 18:33:58 -070078 @Override
79 public String toString() {
80 return new String(value);
81 }
82
83}