View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1;
20  
21  import java.io.IOException;
22  import java.math.BigInteger;
23  
24  public class DEREnumerated
25      extends DERObject
26  {
27      byte[]      bytes;
28  
29      /**
30       * return an integer from the passed in object
31       *
32       * @exception IllegalArgumentException if the object cannot be converted.
33       */
34      public static DEREnumerated getInstance(
35          Object  obj)
36      {
37          if (obj == null || obj instanceof DEREnumerated)
38          {
39              return (DEREnumerated)obj;
40          }
41  
42          if (obj instanceof ASN1OctetString)
43          {
44              return new DEREnumerated(((ASN1OctetString)obj).getOctets());
45          }
46  
47          if (obj instanceof ASN1TaggedObject)
48          {
49              return getInstance(((ASN1TaggedObject)obj).getObject());
50          }
51  
52          throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
53      }
54  
55      /**
56       * return an Enumerated from a tagged object.
57       *
58       * @param obj the tagged object holding the object we want
59       * @param explicit true if the object is meant to be explicitly
60       *              tagged false otherwise.
61       * @exception IllegalArgumentException if the tagged object cannot
62       *               be converted.
63       */
64      public static DEREnumerated getInstance(
65          ASN1TaggedObject obj,
66          boolean          explicit)
67      {
68          return getInstance(obj.getObject());
69      }
70  
71      public DEREnumerated(
72          int         value)
73      {
74          bytes = BigInteger.valueOf(value).toByteArray();
75      }
76  
77      public DEREnumerated(
78          BigInteger   value)
79      {
80          bytes = value.toByteArray();
81      }
82  
83      public DEREnumerated(
84          byte[]   bytes)
85      {
86          this.bytes = bytes;
87      }
88  
89      public BigInteger getValue()
90      {
91          return new BigInteger(bytes);
92      }
93  
94      void encode(
95          DEROutputStream out)
96          throws IOException
97      {
98          out.writeEncoded(ENUMERATED, bytes);
99      }
100 
101     public boolean equals(
102         Object  o)
103     {
104         if (o == null || !(o instanceof DEREnumerated))
105         {
106             return false;
107         }
108 
109         DEREnumerated other = (DEREnumerated)o;
110 
111         if (bytes.length != other.bytes.length)
112         {
113             return false;
114         }
115 
116         for (int i = 0; i != bytes.length; i++)
117         {
118             if (bytes[i] != other.bytes[i])
119             {
120                 return false;
121             }
122         }
123 
124         return true;
125     }
126 
127     public int hashCode()
128     {
129         return this.getValue().hashCode();
130     }
131 }