DEREnumerated.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.asn1; 2 3import java.io.IOException; 4import java.math.BigInteger; 5 6import org.bouncycastle.util.Arrays; 7 8/** 9 * Use ASN1Enumerated instead of this. 10 */ 11public class DEREnumerated 12 extends ASN1Primitive 13{ 14 byte[] bytes; 15 16 /** 17 * return an integer from the passed in object 18 * 19 * @exception IllegalArgumentException if the object cannot be converted. 20 */ 21 public static ASN1Enumerated getInstance( 22 Object obj) 23 { 24 if (obj == null || obj instanceof ASN1Enumerated) 25 { 26 return (ASN1Enumerated)obj; 27 } 28 29 if (obj instanceof DEREnumerated) 30 { 31 return new ASN1Enumerated(((DEREnumerated)obj).getValue()); 32 } 33 34 if (obj instanceof byte[]) 35 { 36 try 37 { 38 return (ASN1Enumerated)fromByteArray((byte[])obj); 39 } 40 catch (Exception e) 41 { 42 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString()); 43 } 44 } 45 46 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 47 } 48 49 /** 50 * return an Enumerated from a tagged object. 51 * 52 * @param obj the tagged object holding the object we want 53 * @param explicit true if the object is meant to be explicitly 54 * tagged false otherwise. 55 * @exception IllegalArgumentException if the tagged object cannot 56 * be converted. 57 */ 58 public static ASN1Enumerated getInstance( 59 ASN1TaggedObject obj, 60 boolean explicit) 61 { 62 ASN1Primitive o = obj.getObject(); 63 64 if (explicit || o instanceof DEREnumerated) 65 { 66 return getInstance(o); 67 } 68 else 69 { 70 return fromOctetString(((ASN1OctetString)o).getOctets()); 71 } 72 } 73 74 /** 75 * @deprecated use ASN1Enumerated 76 */ 77 public DEREnumerated( 78 int value) 79 { 80 bytes = BigInteger.valueOf(value).toByteArray(); 81 } 82 83 /** 84 * @deprecated use ASN1Enumerated 85 */ 86 public DEREnumerated( 87 BigInteger value) 88 { 89 bytes = value.toByteArray(); 90 } 91 92 /** 93 * @deprecated use ASN1Enumerated 94 */ 95 public DEREnumerated( 96 byte[] bytes) 97 { 98 this.bytes = bytes; 99 } 100 101 public BigInteger getValue() 102 { 103 return new BigInteger(bytes); 104 } 105 106 boolean isConstructed() 107 { 108 return false; 109 } 110 111 int encodedLength() 112 { 113 return 1 + StreamUtil.calculateBodyLength(bytes.length) + bytes.length; 114 } 115 116 void encode( 117 ASN1OutputStream out) 118 throws IOException 119 { 120 out.writeEncoded(BERTags.ENUMERATED, bytes); 121 } 122 123 boolean asn1Equals( 124 ASN1Primitive o) 125 { 126 if (!(o instanceof DEREnumerated)) 127 { 128 return false; 129 } 130 131 DEREnumerated other = (DEREnumerated)o; 132 133 return Arrays.areEqual(this.bytes, other.bytes); 134 } 135 136 public int hashCode() 137 { 138 return Arrays.hashCode(bytes); 139 } 140 141 private static ASN1Enumerated[] cache = new ASN1Enumerated[12]; 142 143 static ASN1Enumerated fromOctetString(byte[] enc) 144 { 145 if (enc.length > 1) 146 { 147 return new ASN1Enumerated(Arrays.clone(enc)); 148 } 149 150 if (enc.length == 0) 151 { 152 throw new IllegalArgumentException("ENUMERATED has zero length"); 153 } 154 int value = enc[0] & 0xff; 155 156 if (value >= cache.length) 157 { 158 return new ASN1Enumerated(Arrays.clone(enc)); 159 } 160 161 ASN1Enumerated possibleMatch = cache[value]; 162 163 if (possibleMatch == null) 164 { 165 possibleMatch = cache[value] = new ASN1Enumerated(Arrays.clone(enc)); 166 } 167 168 return possibleMatch; 169 } 170} 171