DERBoolean.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.asn1;
2
3import java.io.IOException;
4
5import org.bouncycastle.util.Arrays;
6
7public class DERBoolean
8    extends ASN1Primitive
9{
10    private static final byte[] TRUE_VALUE = new byte[] { (byte)0xff };
11    private static final byte[] FALSE_VALUE = new byte[] { 0 };
12
13    // BEGIN android-changed
14    final private byte[]         value;
15    // END android-changed
16
17    public static final ASN1Boolean FALSE = new ASN1Boolean(false);
18    public static final ASN1Boolean TRUE  = new ASN1Boolean(true);
19
20
21    /**
22     * return a boolean from the passed in object.
23     *
24     * @exception IllegalArgumentException if the object cannot be converted.
25     */
26    public static ASN1Boolean getInstance(
27        Object  obj)
28    {
29        if (obj == null || obj instanceof ASN1Boolean)
30        {
31            return (ASN1Boolean)obj;
32        }
33
34        if (obj instanceof DERBoolean)
35        {
36            return ((DERBoolean)obj).isTrue() ? DERBoolean.TRUE : DERBoolean.FALSE;
37        }
38
39        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
40    }
41
42    /**
43     * return a ASN1Boolean from the passed in boolean.
44     */
45    public static ASN1Boolean getInstance(
46        boolean  value)
47    {
48        return (value ? TRUE : FALSE);
49    }
50
51    /**
52     * return a ASN1Boolean from the passed in boolean.
53     */
54    public static ASN1Boolean getInstance(
55        int value)
56    {
57        return (value != 0 ? TRUE : FALSE);
58    }
59
60    // BEGIN android-added
61    /**
62     * return a DERBoolean from the passed in array.
63     */
64    public static DERBoolean getInstance(
65        byte[] octets)
66    {
67        return (octets[0] != 0) ? TRUE : FALSE;
68    }
69
70    // END android-added
71    /**
72     * return a Boolean from a tagged object.
73     *
74     * @param obj the tagged object holding the object we want
75     * @param explicit true if the object is meant to be explicitly
76     *              tagged false otherwise.
77     * @exception IllegalArgumentException if the tagged object cannot
78     *               be converted.
79     */
80    public static DERBoolean getInstance(
81        ASN1TaggedObject obj,
82        boolean          explicit)
83    {
84        ASN1Primitive o = obj.getObject();
85
86        if (explicit || o instanceof DERBoolean)
87        {
88            return getInstance(o);
89        }
90        else
91        {
92            return ASN1Boolean.fromOctetString(((ASN1OctetString)o).getOctets());
93        }
94    }
95
96    // BEGIN android-changed
97    protected DERBoolean(
98    // END android-changed
99        byte[]       value)
100    {
101        if (value.length != 1)
102        {
103            throw new IllegalArgumentException("byte value should have 1 byte in it");
104        }
105
106        if (value[0] == 0)
107        {
108            this.value = FALSE_VALUE;
109        }
110        else if (value[0] == 0xff)
111        {
112            this.value = TRUE_VALUE;
113        }
114        else
115        {
116            this.value = Arrays.clone(value);
117        }
118    }
119
120    /**
121     * @deprecated use getInstance(boolean) method.
122     * @param value
123     */
124    // BEGIN android-changed
125    protected DERBoolean(
126        boolean     value)
127    // END android-changed
128    {
129        this.value = (value) ? TRUE_VALUE : FALSE_VALUE;
130    }
131
132    public boolean isTrue()
133    {
134        return (value[0] != 0);
135    }
136
137    boolean isConstructed()
138    {
139        return false;
140    }
141
142    int encodedLength()
143    {
144        return 3;
145    }
146
147    void encode(
148        ASN1OutputStream out)
149        throws IOException
150    {
151        out.writeEncoded(BERTags.BOOLEAN, value);
152    }
153
154    protected boolean asn1Equals(
155        ASN1Primitive  o)
156    {
157        if ((o == null) || !(o instanceof DERBoolean))
158        {
159            return false;
160        }
161
162        return (value[0] == ((DERBoolean)o).value[0]);
163    }
164
165    public int hashCode()
166    {
167        return value[0];
168    }
169
170
171    public String toString()
172    {
173      return (value[0] != 0) ? "TRUE" : "FALSE";
174    }
175
176    static ASN1Boolean fromOctetString(byte[] value)
177    {
178        if (value.length != 1)
179        {
180            throw new IllegalArgumentException("byte value should have 1 byte in it");
181        }
182
183        if (value[0] == 0)
184        {
185            return FALSE;
186        }
187        else if (value[0] == 0xff)
188        {
189            return TRUE;
190        }
191        else
192        {
193            return new ASN1Boolean(value);
194        }
195    }
196}
197