blob: 64458628630e17fe946ebd6053f3fe7819b9b7ed [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 BerAny implements Serializable {
18
19 private static final long serialVersionUID = 1L;
20
21 public byte[] value;
22
23 public BerAny() {
24 }
25
26 public BerAny(byte[] value) {
27 this.value = value;
28 }
29
30 public int encode(BerByteArrayOutputStream os) throws IOException {
31 os.write(value);
32 return value.length;
33 }
34
35 public int decode(InputStream is) throws IOException {
36
37 return decode(is, null);
38 }
39
40 public int decode(InputStream is, BerTag tag) throws IOException {
41
42 int decodedLength = 0;
43
44 int tagLength;
45
46 if (tag == null) {
47 tag = new BerTag();
48 tagLength = tag.decode(is);
49 decodedLength += tagLength;
50 } else {
51 tagLength = tag.encode(new BerByteArrayOutputStream(10));
52 }
53
54 BerLength lengthField = new BerLength();
55 int lengthLength = lengthField.decode(is);
56 decodedLength += lengthLength + lengthField.val;
57
58 value = new byte[tagLength + lengthLength + lengthField.val];
59
60 Util.readFully(is, value, tagLength + lengthLength, lengthField.val);
61 BerByteArrayOutputStream os = new BerByteArrayOutputStream(value, tagLength + lengthLength - 1);
62 BerLength.encodeLength(os, lengthField.val);
63 tag.encode(os);
64
65 return decodedLength;
66 }
67
68 @JsonValue
69 @Override
70 public String toString() {
71 return DatatypeConverter.printHexBinary(value);
72 }
73
74}