1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1;
20
21 import java.io.IOException;
22
23 public class DERBoolean
24 extends DERObject
25 {
26 byte value;
27
28 public static final DERBoolean FALSE = new DERBoolean(false);
29 public static final DERBoolean TRUE = new DERBoolean(true);
30
31 /**
32 * return a boolean from the passed in object.
33 *
34 * @exception IllegalArgumentException if the object cannot be converted.
35 */
36 public static DERBoolean getInstance(
37 Object obj)
38 {
39 if (obj == null || obj instanceof DERBoolean)
40 {
41 return (DERBoolean)obj;
42 }
43
44 if (obj instanceof ASN1OctetString)
45 {
46 return new DERBoolean(((ASN1OctetString)obj).getOctets());
47 }
48
49 if (obj instanceof ASN1TaggedObject)
50 {
51 return getInstance(((ASN1TaggedObject)obj).getObject());
52 }
53
54 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
55 }
56
57 /**
58 * return a DERBoolean from the passed in boolean.
59 */
60 public static DERBoolean getInstance(
61 boolean value)
62 {
63 return (value ? TRUE : FALSE);
64 }
65
66 /**
67 * return a Boolean from a tagged object.
68 *
69 * @param obj the tagged object holding the object we want
70 * @param explicit true if the object is meant to be explicitly
71 * tagged false otherwise.
72 * @exception IllegalArgumentException if the tagged object cannot
73 * be converted.
74 */
75 public static DERBoolean getInstance(
76 ASN1TaggedObject obj,
77 boolean explicit)
78 {
79 return getInstance(obj.getObject());
80 }
81
82 public DERBoolean(
83 byte[] value)
84 {
85 this.value = value[0];
86 }
87
88 public DERBoolean(
89 boolean value)
90 {
91 this.value = (value) ? (byte)0xff : (byte)0;
92 }
93
94 public boolean isTrue()
95 {
96 return (value != 0);
97 }
98
99 void encode(
100 DEROutputStream out)
101 throws IOException
102 {
103 byte[] bytes = new byte[1];
104
105 bytes[0] = value;
106
107 out.writeEncoded(BOOLEAN, bytes);
108 }
109
110 public boolean equals(
111 Object o)
112 {
113 if ((o == null) || !(o instanceof DERBoolean))
114 {
115 return false;
116 }
117
118 return (value == ((DERBoolean)o).value);
119 }
120
121 public int hashCode()
122 {
123 return value;
124 }
125
126 }