KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DEREnumerated


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.IOException JavaDoc;
21 import java.math.BigInteger JavaDoc;
22
23 public class DEREnumerated
24     extends DERObject
25 {
26     byte[] bytes;
27
28     /**
29      * return an integer from the passed in object
30      *
31      * @exception IllegalArgumentException if the object cannot be converted.
32      */

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

63     public static DEREnumerated getInstance(
64         ASN1TaggedObject obj,
65         boolean explicit)
66     {
67         return getInstance(obj.getObject());
68     }
69
70     public DEREnumerated(
71         int value)
72     {
73         bytes = BigInteger.valueOf(value).toByteArray();
74     }
75
76     public DEREnumerated(
77         BigInteger JavaDoc value)
78     {
79         bytes = value.toByteArray();
80     }
81
82     public DEREnumerated(
83         byte[] bytes)
84     {
85         this.bytes = bytes;
86     }
87
88     public BigInteger JavaDoc getValue()
89     {
90         return new BigInteger JavaDoc(bytes);
91     }
92
93     void encode(
94         DEROutputStream out)
95         throws IOException JavaDoc
96     {
97         out.writeEncoded(ENUMERATED, bytes);
98     }
99
100     public boolean equals(
101         Object JavaDoc o)
102     {
103         if (o == null || !(o instanceof DEREnumerated))
104         {
105             return false;
106         }
107
108         DEREnumerated other = (DEREnumerated)o;
109
110         if (bytes.length != other.bytes.length)
111         {
112             return false;
113         }
114
115         for (int i = 0; i != bytes.length; i++)
116         {
117             if (bytes[i] != other.bytes[i])
118             {
119                 return false;
120             }
121         }
122
123         return true;
124     }
125
126     public int hashCode()
127     {
128         return this.getValue().hashCode();
129     }
130 }
131
Popular Tags