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.string; |
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 java.io.IOException; |
| 13 | import java.io.InputStream; |
| 14 | import java.io.Serializable; |
| 15 | |
| 16 | public class BerVisibleString implements Serializable { |
| 17 | |
| 18 | public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.VISIBLE_STRING_TAG); |
| 19 | private static final long serialVersionUID = 1L; |
| 20 | public byte[] value; |
| 21 | |
| 22 | public BerVisibleString() { |
| 23 | } |
| 24 | |
| 25 | public BerVisibleString(byte[] value) { |
| 26 | this.value = value; |
| 27 | } |
| 28 | |
| 29 | public BerVisibleString(String valueAsString) { |
| 30 | value = valueAsString.getBytes(); |
| 31 | } |
| 32 | |
| 33 | public int encode(BerByteArrayOutputStream os) throws IOException { |
| 34 | return encode(os, true); |
| 35 | } |
| 36 | |
| 37 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 38 | |
| 39 | os.write(value); |
| 40 | int codeLength = value.length; |
| 41 | |
| 42 | codeLength += BerLength.encodeLength(os, codeLength); |
| 43 | |
| 44 | if (withTag) { |
| 45 | codeLength += tag.encode(os); |
| 46 | } |
| 47 | |
| 48 | return codeLength; |
| 49 | } |
| 50 | |
| 51 | public int decode(InputStream is) throws IOException { |
| 52 | return decode(is, true); |
| 53 | } |
| 54 | |
| 55 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 56 | |
| 57 | int codeLength = 0; |
| 58 | |
| 59 | if (withTag) { |
| 60 | codeLength += tag.decodeAndCheck(is); |
| 61 | } |
| 62 | |
| 63 | BerLength length = new BerLength(); |
| 64 | codeLength += length.decode(is); |
| 65 | |
| 66 | value = new byte[length.val]; |
| 67 | |
| 68 | if (length.val != 0) { |
| 69 | Util.readFully(is, value); |
| 70 | codeLength += length.val; |
| 71 | } |
| 72 | |
| 73 | return codeLength; |
| 74 | |
| 75 | } |
| 76 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 77 | |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public String toString() { |
| 80 | return new String(value); |
| 81 | } |
| 82 | |
| 83 | } |