KickJava   Java API By Example, From Geeks To Geeks.

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


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 DERInteger
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 DERInteger getInstance(
34         Object JavaDoc obj)
35     {
36         if (obj == null || obj instanceof DERInteger)
37         {
38             return (DERInteger)obj;
39         }
40
41         if (obj instanceof ASN1OctetString)
42         {
43             return new DERInteger(((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 Integer 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 DERInteger getInstance(
64         ASN1TaggedObject obj,
65         boolean explicit)
66     {
67         return getInstance(obj.getObject());
68     }
69
70     public DERInteger(
71         int value)
72     {
73         bytes = BigInteger.valueOf(value).toByteArray();
74     }
75
76     public DERInteger(
77         BigInteger JavaDoc value)
78     {
79         bytes = value.toByteArray();
80     }
81
82     public DERInteger(
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     /**
94      * in some cases positive values get crammed into a space,
95      * that's not quite big enough...
96      */

97     public BigInteger JavaDoc getPositiveValue()
98     {
99         return new BigInteger JavaDoc(1, bytes);
100     }
101
102     void encode(
103         DEROutputStream out)
104         throws IOException JavaDoc
105     {
106         out.writeEncoded(INTEGER, bytes);
107     }
108
109     public int hashCode()
110     {
111          int value = 0;
112
113          for (int i = 0; i != bytes.length; i++)
114          {
115              value ^= (bytes[i] & 0xff) << (i % 4);
116          }
117
118          return value;
119     }
120
121     public boolean equals(
122         Object JavaDoc o)
123     {
124         if (o == null || !(o instanceof DERInteger))
125         {
126             return false;
127         }
128
129         DERInteger other = (DERInteger)o;
130
131         if (bytes.length != other.bytes.length)
132         {
133             return false;
134         }
135
136         for (int i = 0; i != bytes.length; i++)
137         {
138             if (bytes[i] != other.bytes[i])
139             {
140                 return false;
141             }
142         }
143
144         return true;
145     }
146 }
147
Popular Tags