• 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 class DERVideotexString
9     extends ASN1Primitive
10     implements ASN1String
11 {
12     private final byte[] string;
13 
14     /**
15      * return a Videotex String from the passed in object
16      *
17      * @param obj a DERVideotexString or an object that can be converted into one.
18      * @exception IllegalArgumentException if the object cannot be converted.
19      * @return a DERVideotexString instance, or null.
20      */
getInstance( Object obj)21     public static DERVideotexString getInstance(
22         Object  obj)
23     {
24         if (obj == null || obj instanceof DERVideotexString)
25         {
26             return (DERVideotexString)obj;
27         }
28 
29         if (obj instanceof byte[])
30         {
31             try
32             {
33                 return (DERVideotexString)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 Videotex 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 DERVideotexString instance, or null.
53      */
getInstance( ASN1TaggedObject obj, boolean explicit)54     public static DERVideotexString getInstance(
55         ASN1TaggedObject obj,
56         boolean          explicit)
57     {
58         ASN1Primitive o = obj.getObject();
59 
60         if (explicit || o instanceof DERVideotexString)
61         {
62             return getInstance(o);
63         }
64         else
65         {
66             return new DERVideotexString(((ASN1OctetString)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      */
DERVideotexString( byte[] string)74     public DERVideotexString(
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)95     void encode(
96         ASN1OutputStream out)
97         throws IOException
98     {
99         out.writeEncoded(BERTags.VIDEOTEX_STRING, string);
100     }
101 
hashCode()102     public int hashCode()
103     {
104         return Arrays.hashCode(string);
105     }
106 
asn1Equals( ASN1Primitive o)107     boolean asn1Equals(
108         ASN1Primitive o)
109     {
110         if (!(o instanceof DERVideotexString))
111         {
112             return false;
113         }
114 
115         DERVideotexString  s = (DERVideotexString)o;
116 
117         return Arrays.areEqual(string, s.string);
118     }
119 
getString()120     public String getString()
121     {
122         return Strings.fromByteArray(string);
123     }
124 }
125