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