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