KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DERBoolean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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

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

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

74     public static DERBoolean getInstance(
75         ASN1TaggedObject obj,
76         boolean explicit)
77     {
78         return getInstance(obj.getObject());
79     }
80
81     public DERBoolean(
82         byte[] value)
83     {
84         this.value = value[0];
85     }
86
87     public DERBoolean(
88         boolean value)
89     {
90         this.value = (value) ? (byte)0xff : (byte)0;
91     }
92
93     public boolean isTrue()
94     {
95         return (value != 0);
96     }
97
98     void encode(
99         DEROutputStream out)
100         throws IOException JavaDoc
101     {
102         byte[] bytes = new byte[1];
103
104         bytes[0] = value;
105
106         out.writeEncoded(BOOLEAN, bytes);
107     }
108
109     public boolean equals(
110         Object JavaDoc o)
111     {
112         if ((o == null) || !(o instanceof DERBoolean))
113         {
114             return false;
115         }
116
117         return (value == ((DERBoolean)o).value);
118     }
119
120     public int hashCode()
121     {
122         return value;
123     }
124
125 }
126
Popular Tags