KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigInteger JavaDoc;
24
25 public class DEREnumerated
26     extends DERObject
27 {
28     byte[] bytes;
29
30     /**
31      * return an integer from the passed in object
32      *
33      * @exception IllegalArgumentException if the object cannot be converted.
34      */

35     public static DEREnumerated getInstance(
36         Object JavaDoc obj)
37     {
38         if (obj == null || obj instanceof DEREnumerated)
39         {
40             return (DEREnumerated)obj;
41         }
42
43         if (obj instanceof ASN1OctetString)
44         {
45             return new DEREnumerated(((ASN1OctetString)obj).getOctets());
46         }
47
48         if (obj instanceof ASN1TaggedObject)
49         {
50             return getInstance(((ASN1TaggedObject)obj).getObject());
51         }
52
53         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
54     }
55
56     /**
57      * return an Enumerated 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 DEREnumerated getInstance(
66         ASN1TaggedObject obj,
67         boolean explicit)
68     {
69         return getInstance(obj.getObject());
70     }
71
72     public DEREnumerated(
73         int value)
74     {
75         bytes = BigInteger.valueOf(value).toByteArray();
76     }
77
78     public DEREnumerated(
79         BigInteger JavaDoc value)
80     {
81         bytes = value.toByteArray();
82     }
83
84     public DEREnumerated(
85         byte[] bytes)
86     {
87         this.bytes = bytes;
88     }
89
90     public BigInteger JavaDoc getValue()
91     {
92         return new BigInteger JavaDoc(bytes);
93     }
94
95     void encode(
96         DEROutputStream out)
97         throws IOException JavaDoc
98     {
99         out.writeEncoded(ENUMERATED, bytes);
100     }
101
102     public boolean equals(
103         Object JavaDoc o)
104     {
105         if (o == null || !(o instanceof DEREnumerated))
106         {
107             return false;
108         }
109
110         DEREnumerated other = (DEREnumerated)o;
111
112         if (bytes.length != other.bytes.length)
113         {
114             return false;
115         }
116
117         for (int i = 0; i != bytes.length; i++)
118         {
119             if (bytes[i] != other.bytes[i])
120             {
121                 return false;
122             }
123         }
124
125         return true;
126     }
127 }
128
Popular Tags