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