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