Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.IOException;
      4 
      5 public class DERBoolean
      6     extends DERObject
      7 {
      8     // BEGIN android-changed
      9     private final byte  value;
     10     // END android-changed
     11 
     12     public static final DERBoolean FALSE = new DERBoolean(false);
     13     public static final DERBoolean TRUE  = new DERBoolean(true);
     14 
     15     /**
     16      * return a boolean from the passed in object.
     17      *
     18      * @exception IllegalArgumentException if the object cannot be converted.
     19      */
     20     public static DERBoolean getInstance(
     21         Object  obj)
     22     {
     23         if (obj == null || obj instanceof DERBoolean)
     24         {
     25             return (DERBoolean)obj;
     26         }
     27 
     28         if (obj instanceof ASN1OctetString)
     29         {
     30             // BEGIN android-changed
     31             return getInstance(((ASN1OctetString)obj).getOctets());
     32             // END android-changed
     33         }
     34 
     35         if (obj instanceof ASN1TaggedObject)
     36         {
     37             return getInstance(((ASN1TaggedObject)obj).getObject());
     38         }
     39 
     40         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     41     }
     42 
     43     /**
     44      * return a DERBoolean from the passed in boolean.
     45      */
     46     public static DERBoolean getInstance(
     47         boolean  value)
     48     {
     49         return (value ? TRUE : FALSE);
     50     }
     51 
     52     // BEGIN android-added
     53     /**
     54      * return a DERBoolean from the passed in array.
     55      */
     56     public static DERBoolean getInstance(
     57         byte[] octets)
     58     {
     59         return (octets[0] != 0) ? TRUE : FALSE;
     60     }
     61     // END android-added
     62 
     63     /**
     64      * return a Boolean from a tagged object.
     65      *
     66      * @param obj the tagged object holding the object we want
     67      * @param explicit true if the object is meant to be explicitly
     68      *              tagged false otherwise.
     69      * @exception IllegalArgumentException if the tagged object cannot
     70      *               be converted.
     71      */
     72     public static DERBoolean getInstance(
     73         ASN1TaggedObject obj,
     74         boolean          explicit)
     75     {
     76         return getInstance(obj.getObject());
     77     }
     78 
     79     // BEGIN android-removed
     80     //private DERBoolean(
     81     //    byte[]       value)
     82     //{
     83     //    this.value = value[0];
     84     //}
     85     // END android-removed
     86 
     87     // BEGIN android-changed
     88     private DERBoolean(
     89         boolean     value)
     90     {
     91         this.value = (value) ? (byte)0xff : (byte)0;
     92     }
     93     // END android-changed
     94 
     95     public boolean isTrue()
     96     {
     97         return (value != 0);
     98     }
     99 
    100     void encode(
    101         DEROutputStream out)
    102         throws IOException
    103     {
    104         byte[]  bytes = new byte[1];
    105 
    106         bytes[0] = value;
    107 
    108         out.writeEncoded(BOOLEAN, bytes);
    109     }
    110 
    111     public boolean equals(
    112         Object  o)
    113     {
    114         if ((o == null) || !(o instanceof DERBoolean))
    115         {
    116             return false;
    117         }
    118 
    119         return (value == ((DERBoolean)o).value);
    120     }
    121 
    122     public int hashCode()
    123     {
    124         return value;
    125     }
    126 
    127 
    128     public String toString()
    129     {
    130       return (value != 0) ? "TRUE" : "FALSE";
    131     }
    132 }
    133