01: package org.bouncycastle.asn1;
02:
03: import java.io.IOException;
04:
05: public class DERBoolean extends ASN1Object {
06: byte value;
07:
08: public static final DERBoolean FALSE = new DERBoolean(false);
09: public static final DERBoolean TRUE = new DERBoolean(true);
10:
11: /**
12: * return a boolean from the passed in object.
13: *
14: * @exception IllegalArgumentException if the object cannot be converted.
15: */
16: public static DERBoolean getInstance(Object obj) {
17: if (obj == null || obj instanceof DERBoolean) {
18: return (DERBoolean) obj;
19: }
20:
21: if (obj instanceof ASN1OctetString) {
22: return new DERBoolean(((ASN1OctetString) obj).getOctets());
23: }
24:
25: if (obj instanceof ASN1TaggedObject) {
26: return getInstance(((ASN1TaggedObject) obj).getObject());
27: }
28:
29: throw new IllegalArgumentException(
30: "illegal object in getInstance: "
31: + obj.getClass().getName());
32: }
33:
34: /**
35: * return a DERBoolean from the passed in boolean.
36: */
37: public static DERBoolean getInstance(boolean value) {
38: return (value ? TRUE : FALSE);
39: }
40:
41: /**
42: * return a Boolean from a tagged object.
43: *
44: * @param obj the tagged object holding the object we want
45: * @param explicit true if the object is meant to be explicitly
46: * tagged false otherwise.
47: * @exception IllegalArgumentException if the tagged object cannot
48: * be converted.
49: */
50: public static DERBoolean getInstance(ASN1TaggedObject obj,
51: boolean explicit) {
52: return getInstance(obj.getObject());
53: }
54:
55: public DERBoolean(byte[] value) {
56: this .value = value[0];
57: }
58:
59: public DERBoolean(boolean value) {
60: this .value = (value) ? (byte) 0xff : (byte) 0;
61: }
62:
63: public boolean isTrue() {
64: return (value != 0);
65: }
66:
67: void encode(DEROutputStream out) throws IOException {
68: byte[] bytes = new byte[1];
69:
70: bytes[0] = value;
71:
72: out.writeEncoded(BOOLEAN, bytes);
73: }
74:
75: protected boolean asn1Equals(DERObject o) {
76: if ((o == null) || !(o instanceof DERBoolean)) {
77: return false;
78: }
79:
80: return (value == ((DERBoolean) o).value);
81: }
82:
83: public int hashCode() {
84: return value;
85: }
86:
87: public String toString() {
88: return (value != 0) ? "TRUE" : "FALSE";
89: }
90: }
|