01: package org.bouncycastle.asn1;
02:
03: import java.io.IOException;
04:
05: public class DERGeneralString extends ASN1Object implements DERString {
06: private String string;
07:
08: public static DERGeneralString getInstance(Object obj) {
09: if (obj == null || obj instanceof DERGeneralString) {
10: return (DERGeneralString) obj;
11: }
12: if (obj instanceof ASN1OctetString) {
13: return new DERGeneralString(((ASN1OctetString) obj)
14: .getOctets());
15: }
16: if (obj instanceof ASN1TaggedObject) {
17: return getInstance(((ASN1TaggedObject) obj).getObject());
18: }
19: throw new IllegalArgumentException(
20: "illegal object in getInstance: "
21: + obj.getClass().getName());
22: }
23:
24: public static DERGeneralString getInstance(ASN1TaggedObject obj,
25: boolean explicit) {
26: return getInstance(obj.getObject());
27: }
28:
29: public DERGeneralString(byte[] string) {
30: char[] cs = new char[string.length];
31: for (int i = 0; i != cs.length; i++) {
32: cs[i] = (char) (string[i] & 0xff);
33: }
34: this .string = new String(cs);
35: }
36:
37: public DERGeneralString(String string) {
38: this .string = string;
39: }
40:
41: public String getString() {
42: return string;
43: }
44:
45: public String toString() {
46: return string;
47: }
48:
49: public byte[] getOctets() {
50: char[] cs = string.toCharArray();
51: byte[] bs = new byte[cs.length];
52: for (int i = 0; i != cs.length; i++) {
53: bs[i] = (byte) cs[i];
54: }
55: return bs;
56: }
57:
58: void encode(DEROutputStream out) throws IOException {
59: out.writeEncoded(GENERAL_STRING, this .getOctets());
60: }
61:
62: public int hashCode() {
63: return this .getString().hashCode();
64: }
65:
66: boolean asn1Equals(DERObject o) {
67: if (!(o instanceof DERGeneralString)) {
68: return false;
69: }
70: DERGeneralString s = (DERGeneralString) o;
71: return this.getString().equals(s.getString());
72: }
73: }
|