KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERBoolean


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4
5 public class DERBoolean
6     extends DERObject
7 {
8     byte value;
9
10     public static final DERBoolean FALSE = new DERBoolean(false);
11     public static final DERBoolean TRUE = new DERBoolean(true);
12
13     /**
14      * return a boolean from the passed in object.
15      *
16      * @exception IllegalArgumentException if the object cannot be converted.
17      */

18     public static DERBoolean getInstance(
19         Object obj)
20     {
21         if (obj == null || obj instanceof DERBoolean)
22         {
23             return (DERBoolean)obj;
24         }
25
26         if (obj instanceof ASN1OctetString)
27         {
28             return new DERBoolean(((ASN1OctetString)obj).getOctets());
29         }
30
31         if (obj instanceof ASN1TaggedObject)
32         {
33             return getInstance(((ASN1TaggedObject)obj).getObject());
34         }
35
36         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
37     }
38
39     /**
40      * return a DERBoolean from the passed in boolean.
41      */

42     public static DERBoolean getInstance(
43         boolean value)
44     {
45         return (value ? TRUE : FALSE);
46     }
47
48     /**
49      * return a Boolean from a tagged object.
50      *
51      * @param obj the tagged object holding the object we want
52      * @param explicit true if the object is meant to be explicitly
53      * tagged false otherwise.
54      * @exception IllegalArgumentException if the tagged object cannot
55      * be converted.
56      */

57     public static DERBoolean getInstance(
58         ASN1TaggedObject obj,
59         boolean explicit)
60     {
61         return getInstance(obj.getObject());
62     }
63     
64     public DERBoolean(
65         byte[] value)
66     {
67         this.value = value[0];
68     }
69
70     public DERBoolean(
71         boolean value)
72     {
73         this.value = (value) ? (byte)0xff : (byte)0;
74     }
75
76     public boolean isTrue()
77     {
78         return (value != 0);
79     }
80
81     void encode(
82         DEROutputStream out)
83         throws IOException
84     {
85         byte[] bytes = new byte[1];
86
87         bytes[0] = value;
88
89         out.writeEncoded(BOOLEAN, bytes);
90     }
91     
92     public boolean equals(
93         Object o)
94     {
95         if ((o == null) || !(o instanceof DERBoolean))
96         {
97             return false;
98         }
99
100         return (value == ((DERBoolean)o).value);
101     }
102
103 }
104
Popular Tags