slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /* 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 Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 4 | package org.onosproject.xran.asn1lib.ber.types; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 5 | |
| 6 | import com.fasterxml.jackson.annotation.JsonValue; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame^] | 7 | import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream; |
| 8 | import org.onosproject.xran.asn1lib.ber.BerLength; |
| 9 | import org.onosproject.xran.asn1lib.ber.BerTag; |
| 10 | import org.onosproject.xran.asn1lib.ber.internal.Util; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 11 | |
| 12 | import javax.xml.bind.DatatypeConverter; |
| 13 | import java.io.IOException; |
| 14 | import java.io.InputStream; |
| 15 | import java.io.Serializable; |
| 16 | |
| 17 | public 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 | } |