01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.crypto.asn1;
17:
18: import java.io.IOException;
19:
20: public class DERGeneralString extends DERObject implements DERString {
21: private String string;
22:
23: public static DERGeneralString getInstance(Object obj) {
24: if (obj == null || obj instanceof DERGeneralString) {
25: return (DERGeneralString) obj;
26: }
27: if (obj instanceof ASN1OctetString) {
28: return new DERGeneralString(((ASN1OctetString) obj)
29: .getOctets());
30: }
31: if (obj instanceof ASN1TaggedObject) {
32: return getInstance(((ASN1TaggedObject) obj).getObject());
33: }
34: throw new IllegalArgumentException(
35: "illegal object in getInstance: "
36: + obj.getClass().getName());
37: }
38:
39: public static DERGeneralString getInstance(ASN1TaggedObject obj,
40: boolean explicit) {
41: return getInstance(obj.getObject());
42: }
43:
44: public DERGeneralString(byte[] string) {
45: char[] cs = new char[string.length];
46: for (int i = 0; i != cs.length; i++) {
47: cs[i] = (char) (string[i] & 0xff);
48: }
49: this .string = new String(cs);
50: }
51:
52: public DERGeneralString(String string) {
53: this .string = string;
54: }
55:
56: public String getString() {
57: return string;
58: }
59:
60: public byte[] getOctets() {
61: char[] cs = string.toCharArray();
62: byte[] bs = new byte[cs.length];
63: for (int i = 0; i != cs.length; i++) {
64: bs[i] = (byte) cs[i];
65: }
66: return bs;
67: }
68:
69: void encode(DEROutputStream out) throws IOException {
70: out.writeEncoded(GENERAL_STRING, this .getOctets());
71: }
72:
73: public int hashCode() {
74: return this .getString().hashCode();
75: }
76:
77: public boolean equals(Object o) {
78: if (!(o instanceof DERGeneralString)) {
79: return false;
80: }
81: DERGeneralString s = (DERGeneralString) o;
82: return this.getString().equals(s.getString());
83: }
84: }
|