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/. */ |
| 4 | package org.onosproject.xran.codecs.ber.types; |
| 5 | |
| 6 | import com.fasterxml.jackson.annotation.JsonValue; |
| 7 | import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream; |
| 8 | import org.onosproject.xran.codecs.ber.BerLength; |
| 9 | import org.onosproject.xran.codecs.ber.BerTag; |
| 10 | import org.onosproject.xran.codecs.ber.internal.Util; |
| 11 | import org.onosproject.xran.codecs.util.HexConverter; |
| 12 | |
| 13 | import javax.xml.bind.DatatypeConverter; |
| 14 | import java.io.IOException; |
| 15 | import java.io.InputStream; |
| 16 | import java.io.Serializable; |
| 17 | |
| 18 | public 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 | } |