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 ASN1Object
      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         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     29     }
     30 
     31     /**
     32      * return a DERBoolean from the passed in boolean.
     33      */
     34     public static DERBoolean getInstance(
     35         boolean  value)
     36     {
     37         return (value ? TRUE : FALSE);
     38     }
     39 
     40     // BEGIN android-added
     41     /**
     42      * return a DERBoolean from the passed in array.
     43      */
     44     public static DERBoolean getInstance(
     45         byte[] octets)
     46     {
     47         return (octets[0] != 0) ? TRUE : FALSE;
     48     }
     49     // END android-added
     50 
     51     /**
     52      * return a Boolean from a tagged object.
     53      *
     54      * @param obj the tagged object holding the object we want
     55      * @param explicit true if the object is meant to be explicitly
     56      *              tagged false otherwise.
     57      * @exception IllegalArgumentException if the tagged object cannot
     58      *               be converted.
     59      */
     60     public static DERBoolean getInstance(
     61         ASN1TaggedObject obj,
     62         boolean          explicit)
     63     {
     64         DERObject o = obj.getObject();
     65 
     66         if (explicit || o instanceof DERBoolean)
     67         {
     68             return getInstance(o);
     69         }
     70         else
     71         {
     72             // BEGIN android-changed
     73             return getInstance(((ASN1OctetString)o).getOctets());
     74             // END android-changed
     75         }
     76     }
     77 
     78     // BEGIN android-removed
     79     // public DERBoolean(
     80     //     byte[]       value)
     81     // {
     82     //     if (value.length != 1)
     83     //     {
     84     //         throw new IllegalArgumentException("byte value should have 1 byte in it");
     85     //     }
     86     //
     87     //     this.value = value[0];
     88     // }
     89     // END android-removed
     90 
     91     // BEGIN android-changed
     92     protected DERBoolean(
     93         boolean     value)
     94     // END android-changed
     95     {
     96         this.value = (value) ? (byte)0xff : (byte)0;
     97     }
     98 
     99     public boolean isTrue()
    100     {
    101         return (value != 0);
    102     }
    103 
    104     void encode(
    105         DEROutputStream out)
    106         throws IOException
    107     {
    108         byte[]  bytes = new byte[1];
    109 
    110         bytes[0] = value;
    111 
    112         out.writeEncoded(BOOLEAN, bytes);
    113     }
    114 
    115     protected boolean asn1Equals(
    116         DERObject  o)
    117     {
    118         if ((o == null) || !(o instanceof DERBoolean))
    119         {
    120             return false;
    121         }
    122 
    123         return (value == ((DERBoolean)o).value);
    124     }
    125 
    126     public int hashCode()
    127     {
    128         return value;
    129     }
    130 
    131 
    132     public String toString()
    133     {
    134       return (value != 0) ? "TRUE" : "FALSE";
    135     }
    136 }
    137