blob: 16a085ac14787e7da07d898582ccff8d306de978 [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.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 javax.xml.bind.DatatypeConverter;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.Serializable;
16
17public class BerOctetString implements Serializable {
18
19 public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.OCTET_STRING_TAG);
20 private static final long serialVersionUID = 1L;
21 public byte[] value;
22
23 public BerOctetString() {
24 }
25
26 public BerOctetString(byte[] value) {
27 this.value = value;
28 }
29
30 public int encode(BerByteArrayOutputStream os) throws IOException {
31 return encode(os, true);
32 }
33
34 public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException {
35
36 os.write(value);
37 int codeLength = value.length;
38
39 codeLength += BerLength.encodeLength(os, codeLength);
40
41 if (withTag) {
42 codeLength += tag.encode(os);
43 }
44
45 return codeLength;
46 }
47
48 public int decode(InputStream is) throws IOException {
49 return decode(is, true);
50 }
51
52 public int decode(InputStream is, boolean withTag) throws IOException {
53
54 int codeLength = 0;
55
56 if (withTag) {
57 codeLength += tag.decodeAndCheck(is);
58 }
59
60 BerLength length = new BerLength();
61 codeLength += length.decode(is);
62
63 value = new byte[length.val];
64
65 if (length.val != 0) {
66 Util.readFully(is, value);
67 codeLength += length.val;
68 }
69
70 return codeLength;
71
72 }
73
74 @JsonValue
75 @Override
76 public String toString() {
77 return DatatypeConverter.printHexBinary(value);
78 }
79
80}