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