001    package org.apache.commons.ssl.asn1;
002    
003    import java.io.IOException;
004    
005    public class DERBoolean
006        extends ASN1Object {
007        byte value;
008    
009        public static final DERBoolean FALSE = new DERBoolean(false);
010        public static final DERBoolean TRUE = new DERBoolean(true);
011    
012        /**
013         * return a boolean from the passed in object.
014         *
015         * @throws IllegalArgumentException if the object cannot be converted.
016         */
017        public static DERBoolean getInstance(
018            Object obj) {
019            if (obj == null || obj instanceof DERBoolean) {
020                return (DERBoolean) obj;
021            }
022    
023            if (obj instanceof ASN1OctetString) {
024                return new DERBoolean(((ASN1OctetString) obj).getOctets());
025            }
026    
027            if (obj instanceof ASN1TaggedObject) {
028                return getInstance(((ASN1TaggedObject) obj).getObject());
029            }
030    
031            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
032        }
033    
034        /** return a DERBoolean from the passed in boolean. */
035        public static DERBoolean getInstance(
036            boolean value) {
037            return (value ? TRUE : FALSE);
038        }
039    
040        /**
041         * return a Boolean from a tagged object.
042         *
043         * @param obj      the tagged object holding the object we want
044         * @param explicit true if the object is meant to be explicitly
045         *                 tagged false otherwise.
046         * @throws IllegalArgumentException if the tagged object cannot
047         *                                  be converted.
048         */
049        public static DERBoolean getInstance(
050            ASN1TaggedObject obj,
051            boolean explicit) {
052            return getInstance(obj.getObject());
053        }
054    
055        public DERBoolean(
056            byte[] value) {
057            this.value = value[0];
058        }
059    
060        public DERBoolean(
061            boolean value) {
062            this.value = (value) ? (byte) 0xff : (byte) 0;
063        }
064    
065        public boolean isTrue() {
066            return (value != 0);
067        }
068    
069        void encode(
070            DEROutputStream out)
071            throws IOException {
072            byte[] bytes = new byte[1];
073    
074            bytes[0] = value;
075    
076            out.writeEncoded(BOOLEAN, bytes);
077        }
078    
079        protected boolean asn1Equals(
080            DERObject o) {
081            if ((o == null) || !(o instanceof DERBoolean)) {
082                return false;
083            }
084    
085            return (value == ((DERBoolean) o).value);
086        }
087    
088        public int hashCode() {
089            return value;
090        }
091    
092    
093        public String toString() {
094            return (value != 0) ? "TRUE" : "FALSE";
095        }
096    }