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