KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERUniversalString


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * DER UniversalString object.
27  */

28 public class DERUniversalString
29     extends DERObject
30     implements DERString
31 {
32     private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
33     private byte[] string;
34
35     /**
36      * return a Universal String from the passed in object.
37      *
38      * @exception IllegalArgumentException if the object cannot be converted.
39      */

40     public static DERUniversalString getInstance(
41         Object JavaDoc obj)
42     {
43         if (obj == null || obj instanceof DERUniversalString)
44         {
45             return (DERUniversalString)obj;
46         }
47
48         if (obj instanceof ASN1OctetString)
49         {
50             return new DERUniversalString(((ASN1OctetString)obj).getOctets());
51         }
52
53         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
54     }
55
56     /**
57      * return a Universal String from a tagged object.
58      *
59      * @param obj the tagged object holding the object we want
60      * @param explicit true if the object is meant to be explicitly
61      * tagged false otherwise.
62      * @exception IllegalArgumentException if the tagged object cannot
63      * be converted.
64      */

65     public static DERUniversalString getInstance(
66         ASN1TaggedObject obj,
67         boolean explicit)
68     {
69         return getInstance(obj.getObject());
70     }
71
72     /**
73      * basic constructor - byte encoded string.
74      */

75     public DERUniversalString(
76         byte[] string)
77     {
78         this.string = string;
79     }
80
81     public String JavaDoc getString()
82     {
83         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("#");
84         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
85         ASN1OutputStream aOut = new ASN1OutputStream(bOut);
86
87         try
88         {
89             aOut.writeObject(this);
90         }
91         catch (IOException JavaDoc e)
92         {
93            throw new RuntimeException JavaDoc("internal error encoding BitString");
94         }
95
96         byte[] string = bOut.toByteArray();
97
98         for (int i = 0; i != string.length; i++)
99         {
100             buf.append(table[(string[i] >>> 4) % 0xf]);
101             buf.append(table[string[i] & 0xf]);
102         }
103
104         return buf.toString();
105     }
106
107     public byte[] getOctets()
108     {
109         return string;
110     }
111
112     void encode(
113         DEROutputStream out)
114         throws IOException JavaDoc
115     {
116         out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
117     }
118
119     public boolean equals(
120         Object JavaDoc o)
121     {
122         if ((o == null) || !(o instanceof DERUniversalString))
123         {
124             return false;
125         }
126
127         return this.getString().equals(((DERUniversalString)o).getString());
128     }
129 }
130
Popular Tags