KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6 /**
7  * DER UniversalString object.
8  */

9 public class DERUniversalString
10     extends DERObject
11     implements DERString
12 {
13     private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
14     private byte[] string;
15     
16     /**
17      * return a Universal String from the passed in object.
18      *
19      * @exception IllegalArgumentException if the object cannot be converted.
20      */

21     public static DERUniversalString getInstance(
22         Object obj)
23     {
24         if (obj == null || obj instanceof DERUniversalString)
25         {
26             return (DERUniversalString)obj;
27         }
28
29         if (obj instanceof ASN1OctetString)
30         {
31             return new DERUniversalString(((ASN1OctetString)obj).getOctets());
32         }
33
34         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
35     }
36
37     /**
38      * return a Universal String from a tagged object.
39      *
40      * @param obj the tagged object holding the object we want
41      * @param explicit true if the object is meant to be explicitly
42      * tagged false otherwise.
43      * @exception IllegalArgumentException if the tagged object cannot
44      * be converted.
45      */

46     public static DERUniversalString getInstance(
47         ASN1TaggedObject obj,
48         boolean explicit)
49     {
50         return getInstance(obj.getObject());
51     }
52
53     /**
54      * basic constructor - byte encoded string.
55      */

56     public DERUniversalString(
57         byte[] string)
58     {
59         this.string = string;
60     }
61
62     public String getString()
63     {
64         StringBuffer buf = new StringBuffer("#");
65         ByteArrayOutputStream bOut = new ByteArrayOutputStream();
66         ASN1OutputStream aOut = new ASN1OutputStream(bOut);
67         
68         try
69         {
70             aOut.writeObject(this);
71         }
72         catch (IOException e)
73         {
74            throw new RuntimeException("internal error encoding BitString");
75         }
76         
77         byte[] string = bOut.toByteArray();
78         
79         for (int i = 0; i != string.length; i++)
80         {
81             buf.append(table[(string[i] >>> 4) % 0xf]);
82             buf.append(table[string[i] & 0xf]);
83         }
84         
85         return buf.toString();
86     }
87
88     public byte[] getOctets()
89     {
90         return string;
91     }
92
93     void encode(
94         DEROutputStream out)
95         throws IOException
96     {
97         out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
98     }
99     
100     public boolean equals(
101         Object o)
102     {
103         if ((o == null) || !(o instanceof DERUniversalString))
104         {
105             return false;
106         }
107
108         return this.getString().equals(((DERUniversalString)o).getString());
109     }
110 }
111
Popular Tags