001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.geronimo.util.asn1; 020 021 import java.io.IOException; 022 import java.math.BigInteger; 023 024 public class DEREnumerated 025 extends DERObject 026 { 027 byte[] bytes; 028 029 /** 030 * return an integer from the passed in object 031 * 032 * @exception IllegalArgumentException if the object cannot be converted. 033 */ 034 public static DEREnumerated getInstance( 035 Object obj) 036 { 037 if (obj == null || obj instanceof DEREnumerated) 038 { 039 return (DEREnumerated)obj; 040 } 041 042 if (obj instanceof ASN1OctetString) 043 { 044 return new DEREnumerated(((ASN1OctetString)obj).getOctets()); 045 } 046 047 if (obj instanceof ASN1TaggedObject) 048 { 049 return getInstance(((ASN1TaggedObject)obj).getObject()); 050 } 051 052 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 053 } 054 055 /** 056 * return an Enumerated from a tagged object. 057 * 058 * @param obj the tagged object holding the object we want 059 * @param explicit true if the object is meant to be explicitly 060 * tagged false otherwise. 061 * @exception IllegalArgumentException if the tagged object cannot 062 * be converted. 063 */ 064 public static DEREnumerated getInstance( 065 ASN1TaggedObject obj, 066 boolean explicit) 067 { 068 return getInstance(obj.getObject()); 069 } 070 071 public DEREnumerated( 072 int value) 073 { 074 bytes = BigInteger.valueOf(value).toByteArray(); 075 } 076 077 public DEREnumerated( 078 BigInteger value) 079 { 080 bytes = value.toByteArray(); 081 } 082 083 public DEREnumerated( 084 byte[] bytes) 085 { 086 this.bytes = bytes; 087 } 088 089 public BigInteger getValue() 090 { 091 return new BigInteger(bytes); 092 } 093 094 void encode( 095 DEROutputStream out) 096 throws IOException 097 { 098 out.writeEncoded(ENUMERATED, bytes); 099 } 100 101 public boolean equals( 102 Object o) 103 { 104 if (o == null || !(o instanceof DEREnumerated)) 105 { 106 return false; 107 } 108 109 DEREnumerated other = (DEREnumerated)o; 110 111 if (bytes.length != other.bytes.length) 112 { 113 return false; 114 } 115 116 for (int i = 0; i != bytes.length; i++) 117 { 118 if (bytes[i] != other.bytes[i]) 119 { 120 return false; 121 } 122 } 123 124 return true; 125 } 126 127 public int hashCode() 128 { 129 return this.getValue().hashCode(); 130 } 131 }