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 */
017
018 package org.apache.geronimo.util.asn1;
019
020 import java.io.IOException;
021
022 /**
023 * DER IA5String object - this is an ascii string.
024 */
025 public class DERIA5String
026 extends DERObject
027 implements DERString
028 {
029 String string;
030
031 /**
032 * return a IA5 string from the passed in object
033 *
034 * @exception IllegalArgumentException if the object cannot be converted.
035 */
036 public static DERIA5String getInstance(
037 Object obj)
038 {
039 if (obj == null || obj instanceof DERIA5String)
040 {
041 return (DERIA5String)obj;
042 }
043
044 if (obj instanceof ASN1OctetString)
045 {
046 return new DERIA5String(((ASN1OctetString)obj).getOctets());
047 }
048
049 if (obj instanceof ASN1TaggedObject)
050 {
051 return getInstance(((ASN1TaggedObject)obj).getObject());
052 }
053
054 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
055 }
056
057 /**
058 * return an IA5 String from a tagged object.
059 *
060 * @param obj the tagged object holding the object we want
061 * @param explicit true if the object is meant to be explicitly
062 * tagged false otherwise.
063 * @exception IllegalArgumentException if the tagged object cannot
064 * be converted.
065 */
066 public static DERIA5String getInstance(
067 ASN1TaggedObject obj,
068 boolean explicit)
069 {
070 return getInstance(obj.getObject());
071 }
072
073 /**
074 * basic constructor - with bytes.
075 */
076 public DERIA5String(
077 byte[] string)
078 {
079 char[] cs = new char[string.length];
080
081 for (int i = 0; i != cs.length; i++)
082 {
083 cs[i] = (char)(string[i] & 0xff);
084 }
085
086 this.string = new String(cs);
087 }
088
089 /**
090 * basic constructor - with string.
091 */
092 public DERIA5String(
093 String string)
094 {
095 this.string = string;
096 }
097
098 public String getString()
099 {
100 return string;
101 }
102
103 public byte[] getOctets()
104 {
105 char[] cs = string.toCharArray();
106 byte[] bs = new byte[cs.length];
107
108 for (int i = 0; i != cs.length; i++)
109 {
110 bs[i] = (byte)cs[i];
111 }
112
113 return bs;
114 }
115
116 void encode(
117 DEROutputStream out)
118 throws IOException
119 {
120 out.writeEncoded(IA5_STRING, this.getOctets());
121 }
122
123 public int hashCode()
124 {
125 return this.getString().hashCode();
126 }
127
128 public boolean equals(
129 Object o)
130 {
131 if (!(o instanceof DERIA5String))
132 {
133 return false;
134 }
135
136 DERIA5String s = (DERIA5String)o;
137
138 return this.getString().equals(s.getString());
139 }
140 }