1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 import java.math.BigInteger ; 24 25 public class DEREnumerated 26 extends DERObject 27 { 28 byte[] bytes; 29 30 35 public static DEREnumerated getInstance( 36 Object obj) 37 { 38 if (obj == null || obj instanceof DEREnumerated) 39 { 40 return (DEREnumerated)obj; 41 } 42 43 if (obj instanceof ASN1OctetString) 44 { 45 return new DEREnumerated(((ASN1OctetString)obj).getOctets()); 46 } 47 48 if (obj instanceof ASN1TaggedObject) 49 { 50 return getInstance(((ASN1TaggedObject)obj).getObject()); 51 } 52 53 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 54 } 55 56 65 public static DEREnumerated getInstance( 66 ASN1TaggedObject obj, 67 boolean explicit) 68 { 69 return getInstance(obj.getObject()); 70 } 71 72 public DEREnumerated( 73 int value) 74 { 75 bytes = BigInteger.valueOf(value).toByteArray(); 76 } 77 78 public DEREnumerated( 79 BigInteger value) 80 { 81 bytes = value.toByteArray(); 82 } 83 84 public DEREnumerated( 85 byte[] bytes) 86 { 87 this.bytes = bytes; 88 } 89 90 public BigInteger getValue() 91 { 92 return new BigInteger (bytes); 93 } 94 95 void encode( 96 DEROutputStream out) 97 throws IOException 98 { 99 out.writeEncoded(ENUMERATED, bytes); 100 } 101 102 public boolean equals( 103 Object o) 104 { 105 if (o == null || !(o instanceof DEREnumerated)) 106 { 107 return false; 108 } 109 110 DEREnumerated other = (DEREnumerated)o; 111 112 if (bytes.length != other.bytes.length) 113 { 114 return false; 115 } 116 117 for (int i = 0; i != bytes.length; i++) 118 { 119 if (bytes[i] != other.bytes[i]) 120 { 121 return false; 122 } 123 } 124 125 return true; 126 } 127 } 128 | Popular Tags |