• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.asn1;
2 
3 import java.io.IOException;
4 
5 import org.bouncycastle.util.Arrays;
6 import org.bouncycastle.util.Strings;
7 
8 /**
9  * DER UTF8String object.
10  */
11 public class DERUTF8String
12     extends ASN1Primitive
13     implements ASN1String
14 {
15     private final byte[]  string;
16 
17     /**
18      * Return an UTF8 string from the passed in object.
19      *
20      * @param obj a DERUTF8String or an object that can be converted into one.
21      * @exception IllegalArgumentException
22      *                if the object cannot be converted.
23      * @return a DERUTF8String instance, or null
24      */
getInstance(Object obj)25     public static DERUTF8String getInstance(Object obj)
26     {
27         if (obj == null || obj instanceof DERUTF8String)
28         {
29             return (DERUTF8String)obj;
30         }
31 
32         if (obj instanceof byte[])
33         {
34             try
35             {
36                 return (DERUTF8String)fromByteArray((byte[])obj);
37             }
38             catch (Exception e)
39             {
40                 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
41             }
42         }
43 
44         throw new IllegalArgumentException("illegal object in getInstance: "
45                 + obj.getClass().getName());
46     }
47 
48     /**
49      * Return an UTF8 String from a tagged object.
50      *
51      * @param obj
52      *            the tagged object holding the object we want
53      * @param explicit
54      *            true if the object is meant to be explicitly tagged false
55      *            otherwise.
56      * @exception IllegalArgumentException
57      *                if the tagged object cannot be converted.
58      * @return a DERUTF8String instance, or null
59      */
getInstance( ASN1TaggedObject obj, boolean explicit)60     public static DERUTF8String getInstance(
61         ASN1TaggedObject obj,
62         boolean explicit)
63     {
64         ASN1Primitive o = obj.getObject();
65 
66         if (explicit || o instanceof DERUTF8String)
67         {
68             return getInstance(o);
69         }
70         else
71         {
72             return new DERUTF8String(ASN1OctetString.getInstance(o).getOctets());
73         }
74     }
75 
76     /*
77      * Basic constructor - byte encoded string.
78      */
DERUTF8String(byte[] string)79     DERUTF8String(byte[] string)
80     {
81         this.string = string;
82     }
83 
84     /**
85      * Basic constructor
86      *
87      * @param string the string to be carried in the UTF8String object,
88      */
DERUTF8String(String string)89     public DERUTF8String(String string)
90     {
91         this.string = Strings.toUTF8ByteArray(string);
92     }
93 
getString()94     public String getString()
95     {
96         return Strings.fromUTF8ByteArray(string);
97     }
98 
toString()99     public String toString()
100     {
101         return getString();
102     }
103 
hashCode()104     public int hashCode()
105     {
106         return Arrays.hashCode(string);
107     }
108 
asn1Equals(ASN1Primitive o)109     boolean asn1Equals(ASN1Primitive o)
110     {
111         if (!(o instanceof DERUTF8String))
112         {
113             return false;
114         }
115 
116         DERUTF8String s = (DERUTF8String)o;
117 
118         return Arrays.areEqual(string, s.string);
119     }
120 
isConstructed()121     boolean isConstructed()
122     {
123         return false;
124     }
125 
encodedLength()126     int encodedLength()
127         throws IOException
128     {
129         return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
130     }
131 
encode(ASN1OutputStream out)132     void encode(ASN1OutputStream out)
133         throws IOException
134     {
135         out.writeEncoded(BERTags.UTF8_STRING, string);
136     }
137 }
138