KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23
24 public class DERGeneralString
25     extends DERObject implements DERString
26 {
27     private String JavaDoc string;
28
29     public static DERGeneralString getInstance(
30         Object JavaDoc obj)
31     {
32         if (obj == null || obj instanceof DERGeneralString)
33         {
34             return (DERGeneralString) obj;
35         }
36         if (obj instanceof ASN1OctetString)
37         {
38             return new DERGeneralString(((ASN1OctetString) obj).getOctets());
39         }
40         if (obj instanceof ASN1TaggedObject)
41         {
42             return getInstance(((ASN1TaggedObject) obj).getObject());
43         }
44         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: "
45                 + obj.getClass().getName());
46     }
47
48     public static DERGeneralString getInstance(
49         ASN1TaggedObject obj,
50         boolean explicit)
51     {
52         return getInstance(obj.getObject());
53     }
54
55     public DERGeneralString(byte[] string)
56     {
57         char[] cs = new char[string.length];
58         for (int i = 0; i != cs.length; i++) {
59             cs[i] = (char) (string[i] & 0xff);
60         }
61         this.string = new String JavaDoc(cs);
62     }
63
64     public DERGeneralString(String JavaDoc string)
65     {
66         this.string = string;
67     }
68
69     public String JavaDoc getString()
70     {
71         return string;
72     }
73
74     public byte[] getOctets()
75     {
76         char[] cs = string.toCharArray();
77         byte[] bs = new byte[cs.length];
78         for (int i = 0; i != cs.length; i++)
79         {
80             bs[i] = (byte) cs[i];
81         }
82         return bs;
83     }
84
85     void encode(DEROutputStream out)
86         throws IOException JavaDoc
87     {
88         out.writeEncoded(GENERAL_STRING, this.getOctets());
89     }
90
91     public int hashCode()
92     {
93         return this.getString().hashCode();
94     }
95
96     public boolean equals(Object JavaDoc o)
97     {
98         if (!(o instanceof DERGeneralString))
99         {
100             return false;
101         }
102         DERGeneralString s = (DERGeneralString) o;
103         return this.getString().equals(s.getString());
104     }
105 }
106
Popular Tags