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