1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.IOException ; 21 import java.math.BigInteger ; 22 23 public class DEREnumerated 24 extends DERObject 25 { 26 byte[] bytes; 27 28 33 public static DEREnumerated getInstance( 34 Object 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 ("illegal object in getInstance: " + obj.getClass().getName()); 52 } 53 54 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 value) 78 { 79 bytes = value.toByteArray(); 80 } 81 82 public DEREnumerated( 83 byte[] bytes) 84 { 85 this.bytes = bytes; 86 } 87 88 public BigInteger getValue() 89 { 90 return new BigInteger (bytes); 91 } 92 93 void encode( 94 DEROutputStream out) 95 throws IOException 96 { 97 out.writeEncoded(ENUMERATED, bytes); 98 } 99 100 public boolean equals( 101 Object 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 |