blob: b27b82cbf699cc2ad8e8eee7d2f909e95433ff14 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/* 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 Mavrommatis96b255a2017-12-06 13:09:25 -08004package org.onosproject.xran.asn1lib.ber.types.string;
slowr60d4d102017-08-16 18:33:58 -07005
6import com.fasterxml.jackson.annotation.JsonValue;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08007import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
8import org.onosproject.xran.asn1lib.ber.BerTag;
9import org.onosproject.xran.asn1lib.ber.types.BerOctetString;
slowr60d4d102017-08-16 18:33:58 -070010
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.UnsupportedEncodingException;
14
15public 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
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080031
slowr60d4d102017-08-16 18:33:58 -070032 @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}