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