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