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 */ 017 018 package org.apache.geronimo.util.asn1; 019 020 import java.io.IOException; 021 022 public class DERBoolean 023 extends DERObject 024 { 025 byte value; 026 027 public static final DERBoolean FALSE = new DERBoolean(false); 028 public static final DERBoolean TRUE = new DERBoolean(true); 029 030 /** 031 * return a boolean from the passed in object. 032 * 033 * @exception IllegalArgumentException if the object cannot be converted. 034 */ 035 public static DERBoolean getInstance( 036 Object obj) 037 { 038 if (obj == null || obj instanceof DERBoolean) 039 { 040 return (DERBoolean)obj; 041 } 042 043 if (obj instanceof ASN1OctetString) 044 { 045 return new DERBoolean(((ASN1OctetString)obj).getOctets()); 046 } 047 048 if (obj instanceof ASN1TaggedObject) 049 { 050 return getInstance(((ASN1TaggedObject)obj).getObject()); 051 } 052 053 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 054 } 055 056 /** 057 * return a DERBoolean from the passed in boolean. 058 */ 059 public static DERBoolean getInstance( 060 boolean value) 061 { 062 return (value ? TRUE : FALSE); 063 } 064 065 /** 066 * return a Boolean from a tagged object. 067 * 068 * @param obj the tagged object holding the object we want 069 * @param explicit true if the object is meant to be explicitly 070 * tagged false otherwise. 071 * @exception IllegalArgumentException if the tagged object cannot 072 * be converted. 073 */ 074 public static DERBoolean getInstance( 075 ASN1TaggedObject obj, 076 boolean explicit) 077 { 078 return getInstance(obj.getObject()); 079 } 080 081 public DERBoolean( 082 byte[] value) 083 { 084 this.value = value[0]; 085 } 086 087 public DERBoolean( 088 boolean value) 089 { 090 this.value = (value) ? (byte)0xff : (byte)0; 091 } 092 093 public boolean isTrue() 094 { 095 return (value != 0); 096 } 097 098 void encode( 099 DEROutputStream out) 100 throws IOException 101 { 102 byte[] bytes = new byte[1]; 103 104 bytes[0] = value; 105 106 out.writeEncoded(BOOLEAN, bytes); 107 } 108 109 public boolean equals( 110 Object o) 111 { 112 if ((o == null) || !(o instanceof DERBoolean)) 113 { 114 return false; 115 } 116 117 return (value == ((DERBoolean)o).value); 118 } 119 120 public int hashCode() 121 { 122 return value; 123 } 124 125 }