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 public class DERGraphicString
9     extends ASN1Primitive
10     implements ASN1String
11 {
12     private final byte[] string;
13 
14     /**
15      * return a Graphic String from the passed in object
16      *
17      * @param obj a DERGraphicString or an object that can be converted into one.
18      * @exception IllegalArgumentException if the object cannot be converted.
19      * @return a DERGraphicString instance, or null.
20      */
getInstance( Object obj)21     public static DERGraphicString getInstance(
22         Object  obj)
23     {
24         if (obj == null || obj instanceof DERGraphicString)
25         {
26             return (DERGraphicString)obj;
27         }
28 
29         if (obj instanceof byte[])
30         {
31             try
32             {
33                 return (DERGraphicString)fromByteArray((byte[])obj);
34             }
35             catch (Exception e)
36             {
37                 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
38             }
39         }
40 
41         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
42     }
43 
44     /**
45      * return a Graphic String from a tagged object.
46      *
47      * @param obj the tagged object holding the object we want
48      * @param explicit true if the object is meant to be explicitly
49      *              tagged false otherwise.
50      * @exception IllegalArgumentException if the tagged object cannot
51      *               be converted.
52      * @return a DERGraphicString instance, or null.
53      */
getInstance( ASN1TaggedObject obj, boolean explicit)54     public static DERGraphicString getInstance(
55         ASN1TaggedObject obj,
56         boolean          explicit)
57     {
58         ASN1Primitive o = obj.getObject();
59 
60         if (explicit || o instanceof DERGraphicString)
61         {
62             return getInstance(o);
63         }
64         else
65         {
66             return new DERGraphicString(ASN1OctetString.getInstance(o).getOctets());
67         }
68     }
69 
70     /**
71      * basic constructor - with bytes.
72      * @param string the byte encoding of the characters making up the string.
73      */
DERGraphicString( byte[] string)74     public DERGraphicString(
75         byte[]   string)
76     {
77         this.string = Arrays.clone(string);
78     }
79 
getOctets()80     public byte[] getOctets()
81     {
82         return Arrays.clone(string);
83     }
84 
isConstructed()85     boolean isConstructed()
86     {
87         return false;
88     }
89 
encodedLength()90     int encodedLength()
91     {
92         return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
93     }
94 
encode(ASN1OutputStream out, boolean withTag)95     void encode(ASN1OutputStream out, boolean withTag) throws IOException
96     {
97         out.writeEncoded(withTag, BERTags.GRAPHIC_STRING, string);
98     }
99 
hashCode()100     public int hashCode()
101     {
102         return Arrays.hashCode(string);
103     }
104 
asn1Equals( ASN1Primitive o)105     boolean asn1Equals(
106         ASN1Primitive o)
107     {
108         if (!(o instanceof DERGraphicString))
109         {
110             return false;
111         }
112 
113         DERGraphicString  s = (DERGraphicString)o;
114 
115         return Arrays.areEqual(string, s.string);
116     }
117 
getString()118     public String getString()
119     {
120         return Strings.fromByteArray(string);
121     }
122 }
123