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 T61String (also the teletex string)
022: */
023: public class DERT61String extends DERObject implements DERString {
024: String string;
025:
026: /**
027: * return a T61 string from the passed in object.
028: *
029: * @exception IllegalArgumentException if the object cannot be converted.
030: */
031: public static DERT61String getInstance(Object obj) {
032: if (obj == null || obj instanceof DERT61String) {
033: return (DERT61String) obj;
034: }
035:
036: if (obj instanceof ASN1OctetString) {
037: return new DERT61String(((ASN1OctetString) obj).getOctets());
038: }
039:
040: if (obj instanceof ASN1TaggedObject) {
041: return getInstance(((ASN1TaggedObject) obj).getObject());
042: }
043:
044: throw new IllegalArgumentException(
045: "illegal object in getInstance: "
046: + obj.getClass().getName());
047: }
048:
049: /**
050: * return an T61 String from a tagged object.
051: *
052: * @param obj the tagged object holding the object we want
053: * @param explicit true if the object is meant to be explicitly
054: * tagged false otherwise.
055: * @exception IllegalArgumentException if the tagged object cannot
056: * be converted.
057: */
058: public static DERT61String getInstance(ASN1TaggedObject obj,
059: boolean explicit) {
060: return getInstance(obj.getObject());
061: }
062:
063: /**
064: * basic constructor - with bytes.
065: */
066: public DERT61String(byte[] string) {
067: char[] cs = new char[string.length];
068:
069: for (int i = 0; i != cs.length; i++) {
070: cs[i] = (char) (string[i] & 0xff);
071: }
072:
073: this .string = new String(cs);
074: }
075:
076: /**
077: * basic constructor - with string.
078: */
079: public DERT61String(String string) {
080: this .string = string;
081: }
082:
083: public String getString() {
084: return string;
085: }
086:
087: void encode(DEROutputStream out) throws IOException {
088: out.writeEncoded(T61_STRING, this .getOctets());
089: }
090:
091: public byte[] getOctets() {
092: char[] cs = string.toCharArray();
093: byte[] bs = new byte[cs.length];
094:
095: for (int i = 0; i != cs.length; i++) {
096: bs[i] = (byte) cs[i];
097: }
098:
099: return bs;
100: }
101:
102: public boolean equals(Object o) {
103: if ((o == null) || !(o instanceof DERT61String)) {
104: return false;
105: }
106:
107: return this .getString().equals(((DERT61String) o).getString());
108: }
109:
110: public int hashCode() {
111: return this.getString().hashCode();
112: }
113: }
|