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