01: package org.bouncycastle.asn1;
02:
03: import java.io.IOException;
04: import java.math.BigInteger;
05:
06: public class DEREnumerated extends ASN1Object {
07: byte[] bytes;
08:
09: /**
10: * return an integer from the passed in object
11: *
12: * @exception IllegalArgumentException if the object cannot be converted.
13: */
14: public static DEREnumerated getInstance(Object obj) {
15: if (obj == null || obj instanceof DEREnumerated) {
16: return (DEREnumerated) obj;
17: }
18:
19: if (obj instanceof ASN1OctetString) {
20: return new DEREnumerated(((ASN1OctetString) obj)
21: .getOctets());
22: }
23:
24: if (obj instanceof ASN1TaggedObject) {
25: return getInstance(((ASN1TaggedObject) obj).getObject());
26: }
27:
28: throw new IllegalArgumentException(
29: "illegal object in getInstance: "
30: + obj.getClass().getName());
31: }
32:
33: /**
34: * return an Enumerated from a tagged object.
35: *
36: * @param obj the tagged object holding the object we want
37: * @param explicit true if the object is meant to be explicitly
38: * tagged false otherwise.
39: * @exception IllegalArgumentException if the tagged object cannot
40: * be converted.
41: */
42: public static DEREnumerated getInstance(ASN1TaggedObject obj,
43: boolean explicit) {
44: return getInstance(obj.getObject());
45: }
46:
47: public DEREnumerated(int value) {
48: bytes = BigInteger.valueOf(value).toByteArray();
49: }
50:
51: public DEREnumerated(BigInteger value) {
52: bytes = value.toByteArray();
53: }
54:
55: public DEREnumerated(byte[] bytes) {
56: this .bytes = bytes;
57: }
58:
59: public BigInteger getValue() {
60: return new BigInteger(bytes);
61: }
62:
63: void encode(DEROutputStream out) throws IOException {
64: out.writeEncoded(ENUMERATED, bytes);
65: }
66:
67: boolean asn1Equals(DERObject o) {
68: if (!(o instanceof DEREnumerated)) {
69: return false;
70: }
71:
72: DEREnumerated other = (DEREnumerated) o;
73:
74: if (bytes.length != other.bytes.length) {
75: return false;
76: }
77:
78: for (int i = 0; i != bytes.length; i++) {
79: if (bytes[i] != other.bytes[i]) {
80: return false;
81: }
82: }
83:
84: return true;
85: }
86:
87: public int hashCode() {
88: return this.getValue().hashCode();
89: }
90: }
|