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.string; |
| 5 | |
| 6 | import com.fasterxml.jackson.annotation.JsonValue; |
| 7 | import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream; |
| 8 | import org.onosproject.xran.codecs.ber.BerTag; |
| 9 | import org.onosproject.xran.codecs.ber.types.BerOctetString; |
| 10 | |
| 11 | import java.io.IOException; |
| 12 | import java.io.InputStream; |
| 13 | import java.io.UnsupportedEncodingException; |
| 14 | |
| 15 | public class BerUTF8String extends BerOctetString { |
| 16 | |
| 17 | public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.UTF8_STRING_TAG); |
| 18 | private static final long serialVersionUID = 1L; |
| 19 | |
| 20 | public BerUTF8String() { |
| 21 | } |
| 22 | |
| 23 | public BerUTF8String(byte[] value) { |
| 24 | this.value = value; |
| 25 | } |
| 26 | |
| 27 | public BerUTF8String(String valueAsString) throws UnsupportedEncodingException { |
| 28 | value = valueAsString.getBytes("UTF-8"); |
| 29 | } |
| 30 | |
| 31 | @JsonValue |
| 32 | @Override |
| 33 | public String toString() { |
| 34 | try { |
| 35 | return new String(value, "UTF-8"); |
| 36 | } catch (UnsupportedEncodingException e) { |
| 37 | return "Unsupported Encoding"; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 43 | |
| 44 | int codeLength = super.encode(os, false); |
| 45 | |
| 46 | if (withTag) { |
| 47 | codeLength += tag.encode(os); |
| 48 | } |
| 49 | |
| 50 | return codeLength; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 55 | |
| 56 | int codeLength = 0; |
| 57 | |
| 58 | if (withTag) { |
| 59 | codeLength += tag.decodeAndCheck(is); |
| 60 | } |
| 61 | |
| 62 | codeLength += super.decode(is, false); |
| 63 | |
| 64 | return codeLength; |
| 65 | } |
| 66 | |
| 67 | } |