001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.IOException;
019:
020: /**
021: * DER PrintableString object.
022: */
023: public class DERPrintableString extends DERObject implements DERString {
024: String string;
025:
026: /**
027: * return a printable string from the passed in object.
028: *
029: * @exception IllegalArgumentException if the object cannot be converted.
030: */
031: public static DERPrintableString getInstance(Object obj) {
032: if (obj == null || obj instanceof DERPrintableString) {
033: return (DERPrintableString) obj;
034: }
035:
036: if (obj instanceof ASN1OctetString) {
037: return new DERPrintableString(((ASN1OctetString) obj)
038: .getOctets());
039: }
040:
041: if (obj instanceof ASN1TaggedObject) {
042: return getInstance(((ASN1TaggedObject) obj).getObject());
043: }
044:
045: throw new IllegalArgumentException(
046: "illegal object in getInstance: "
047: + obj.getClass().getName());
048: }
049:
050: /**
051: * return a Printable String from a tagged object.
052: *
053: * @param obj the tagged object holding the object we want
054: * @param explicit true if the object is meant to be explicitly
055: * tagged false otherwise.
056: * @exception IllegalArgumentException if the tagged object cannot
057: * be converted.
058: */
059: public static DERPrintableString getInstance(ASN1TaggedObject obj,
060: boolean explicit) {
061: return getInstance(obj.getObject());
062: }
063:
064: /**
065: * basic constructor - byte encoded string.
066: */
067: public DERPrintableString(byte[] string) {
068: char[] cs = new char[string.length];
069:
070: for (int i = 0; i != cs.length; i++) {
071: cs[i] = (char) (string[i] & 0xff);
072: }
073:
074: this .string = new String(cs);
075: }
076:
077: /**
078: * basic constructor
079: */
080: public DERPrintableString(String string) {
081: this .string = string;
082: }
083:
084: public String getString() {
085: return string;
086: }
087:
088: public byte[] getOctets() {
089: char[] cs = string.toCharArray();
090: byte[] bs = new byte[cs.length];
091:
092: for (int i = 0; i != cs.length; i++) {
093: bs[i] = (byte) cs[i];
094: }
095:
096: return bs;
097: }
098:
099: void encode(DEROutputStream out) throws IOException {
100: out.writeEncoded(PRINTABLE_STRING, this .getOctets());
101: }
102:
103: public int hashCode() {
104: return this .getString().hashCode();
105: }
106:
107: public boolean equals(Object o) {
108: if (!(o instanceof DERPrintableString)) {
109: return false;
110: }
111:
112: DERPrintableString s = (DERPrintableString) o;
113:
114: return this.getString().equals(s.getString());
115: }
116: }
|