KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERBoolean


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.IOException JavaDoc;
23
24 public class DERBoolean
25     extends DERObject
26 {
27     byte value;
28
29     public static final DERBoolean FALSE = new DERBoolean(false);
30     public static final DERBoolean TRUE = new DERBoolean(true);
31
32     /**
33      * return a boolean from the passed in object.
34      *
35      * @exception IllegalArgumentException if the object cannot be converted.
36      */

37     public static DERBoolean getInstance(
38         Object JavaDoc obj)
39     {
40         if (obj == null || obj instanceof DERBoolean)
41         {
42             return (DERBoolean)obj;
43         }
44
45         if (obj instanceof ASN1OctetString)
46         {
47             return new DERBoolean(((ASN1OctetString)obj).getOctets());
48         }
49
50         if (obj instanceof ASN1TaggedObject)
51         {
52             return getInstance(((ASN1TaggedObject)obj).getObject());
53         }
54
55         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
56     }
57
58     /**
59      * return a DERBoolean from the passed in boolean.
60      */

61     public static DERBoolean getInstance(
62         boolean value)
63     {
64         return (value ? TRUE : FALSE);
65     }
66
67     /**
68      * return a Boolean from a tagged object.
69      *
70      * @param obj the tagged object holding the object we want
71      * @param explicit true if the object is meant to be explicitly
72      * tagged false otherwise.
73      * @exception IllegalArgumentException if the tagged object cannot
74      * be converted.
75      */

76     public static DERBoolean getInstance(
77         ASN1TaggedObject obj,
78         boolean explicit)
79     {
80         return getInstance(obj.getObject());
81     }
82
83     public DERBoolean(
84         byte[] value)
85     {
86         this.value = value[0];
87     }
88
89     public DERBoolean(
90         boolean value)
91     {
92         this.value = (value) ? (byte)0xff : (byte)0;
93     }
94
95     public boolean isTrue()
96     {
97         return (value != 0);
98     }
99
100     void encode(
101         DEROutputStream out)
102         throws IOException JavaDoc
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 JavaDoc o)
113     {
114         if ((o == null) || !(o instanceof DERBoolean))
115         {
116             return false;
117         }
118
119         return (value == ((DERBoolean)o).value);
120     }
121
122 }
123
Popular Tags