KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERGeneralString


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4
5 public class DERGeneralString
6     extends DERObject implements DERString
7 {
8     private String string;
9
10     public static DERGeneralString getInstance(
11         Object obj)
12     {
13         if (obj == null || obj instanceof DERGeneralString)
14         {
15             return (DERGeneralString) obj;
16         }
17         if (obj instanceof ASN1OctetString)
18         {
19             return new DERGeneralString(((ASN1OctetString) obj).getOctets());
20         }
21         if (obj instanceof ASN1TaggedObject)
22         {
23             return getInstance(((ASN1TaggedObject) obj).getObject());
24         }
25         throw new IllegalArgumentException("illegal object in getInstance: "
26                 + obj.getClass().getName());
27     }
28
29     public static DERGeneralString getInstance(
30         ASN1TaggedObject obj,
31         boolean explicit)
32     {
33         return getInstance(obj.getObject());
34     }
35
36     public DERGeneralString(byte[] string)
37     {
38         char[] cs = new char[string.length];
39         for (int i = 0; i != cs.length; i++) {
40             cs[i] = (char) (string[i] & 0xff);
41         }
42         this.string = new String(cs);
43     }
44
45     public DERGeneralString(String string)
46     {
47         this.string = string;
48     }
49     
50     public String getString()
51     {
52         return string;
53     }
54     
55     public byte[] getOctets()
56     {
57         char[] cs = string.toCharArray();
58         byte[] bs = new byte[cs.length];
59         for (int i = 0; i != cs.length; i++)
60         {
61             bs[i] = (byte) cs[i];
62         }
63         return bs;
64     }
65     
66     void encode(DEROutputStream out)
67         throws IOException
68     {
69         out.writeEncoded(GENERAL_STRING, this.getOctets());
70     }
71     
72     public int hashCode()
73     {
74         return this.getString().hashCode();
75     }
76     
77     public boolean equals(Object o)
78     {
79         if (!(o instanceof DERGeneralString))
80         {
81             return false;
82         }
83         DERGeneralString s = (DERGeneralString) o;
84         return this.getString().equals(s.getString());
85     }
86 }
87
Popular Tags