KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERInteger


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4 import java.math.BigInteger;
5
6 public class DERInteger
7     extends DERObject
8 {
9     byte[] bytes;
10
11     /**
12      * return an integer from the passed in object
13      *
14      * @exception IllegalArgumentException if the object cannot be converted.
15      */

16     public static DERInteger getInstance(
17         Object obj)
18     {
19         if (obj == null || obj instanceof DERInteger)
20         {
21             return (DERInteger)obj;
22         }
23
24         if (obj instanceof ASN1OctetString)
25         {
26             return new DERInteger(((ASN1OctetString)obj).getOctets());
27         }
28
29         if (obj instanceof ASN1TaggedObject)
30         {
31             return getInstance(((ASN1TaggedObject)obj).getObject());
32         }
33
34         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
35     }
36
37     /**
38      * return an Integer from a tagged object.
39      *
40      * @param obj the tagged object holding the object we want
41      * @param explicit true if the object is meant to be explicitly
42      * tagged false otherwise.
43      * @exception IllegalArgumentException if the tagged object cannot
44      * be converted.
45      */

46     public static DERInteger getInstance(
47         ASN1TaggedObject obj,
48         boolean explicit)
49     {
50         return getInstance(obj.getObject());
51     }
52
53     public DERInteger(
54         int value)
55     {
56         bytes = BigInteger.valueOf(value).toByteArray();
57     }
58
59     public DERInteger(
60         BigInteger value)
61     {
62         bytes = value.toByteArray();
63     }
64
65     public DERInteger(
66         byte[] bytes)
67     {
68         this.bytes = bytes;
69     }
70
71     public BigInteger getValue()
72     {
73         return new BigInteger(bytes);
74     }
75
76     /**
77      * in some cases positive values get crammed into a space,
78      * that's not quite big enough...
79      */

80     public BigInteger getPositiveValue()
81     {
82         return new BigInteger(1, bytes);
83     }
84
85     void encode(
86         DEROutputStream out)
87         throws IOException
88     {
89         out.writeEncoded(INTEGER, bytes);
90     }
91     
92     public int hashCode()
93     {
94          int value = 0;
95  
96          for (int i = 0; i != bytes.length; i++)
97          {
98              value ^= (bytes[i] & 0xff) << (i % 4);
99          }
100  
101          return value;
102     }
103
104     public boolean equals(
105         Object o)
106     {
107         if (o == null || !(o instanceof DERInteger))
108         {
109             return false;
110         }
111
112         DERInteger other = (DERInteger)o;
113
114         if (bytes.length != other.bytes.length)
115         {
116             return false;
117         }
118
119         for (int i = 0; i != bytes.length; i++)
120         {
121             if (bytes[i] != other.bytes[i])
122             {
123                 return false;
124             }
125         }
126
127         return true;
128     }
129 }
130
Popular Tags