1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5
8 public class DERIA5String
9 extends DERObject
10 implements DERString
11 {
12 String string;
13
14
19 public static DERIA5String getInstance(
20 Object obj)
21 {
22 if (obj == null || obj instanceof DERIA5String)
23 {
24 return (DERIA5String)obj;
25 }
26
27 if (obj instanceof ASN1OctetString)
28 {
29 return new DERIA5String(((ASN1OctetString)obj).getOctets());
30 }
31
32 if (obj instanceof ASN1TaggedObject)
33 {
34 return getInstance(((ASN1TaggedObject)obj).getObject());
35 }
36
37 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
38 }
39
40
49 public static DERIA5String getInstance(
50 ASN1TaggedObject obj,
51 boolean explicit)
52 {
53 return getInstance(obj.getObject());
54 }
55
56
59 public DERIA5String(
60 byte[] string)
61 {
62 char[] cs = new char[string.length];
63
64 for (int i = 0; i != cs.length; i++)
65 {
66 cs[i] = (char)(string[i] & 0xff);
67 }
68
69 this.string = new String(cs);
70 }
71
72
75 public DERIA5String(
76 String string)
77 {
78 this.string = string;
79 }
80
81 public String getString()
82 {
83 return string;
84 }
85
86 public byte[] getOctets()
87 {
88 char[] cs = string.toCharArray();
89 byte[] bs = new byte[cs.length];
90
91 for (int i = 0; i != cs.length; i++)
92 {
93 bs[i] = (byte)cs[i];
94 }
95
96 return bs;
97 }
98
99 void encode(
100 DEROutputStream out)
101 throws IOException
102 {
103 out.writeEncoded(IA5_STRING, this.getOctets());
104 }
105
106 public int hashCode()
107 {
108 return this.getString().hashCode();
109 }
110
111 public boolean equals(
112 Object o)
113 {
114 if (!(o instanceof DERIA5String))
115 {
116 return false;
117 }
118
119 DERIA5String s = (DERIA5String)o;
120
121 return this.getString().equals(s.getString());
122 }
123 }
124