View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * DER UTF8String object.
26   */
27  public class DERUTF8String
28      extends DERObject
29      implements DERString
30  {
31      String  string;
32  
33      /**
34       * return an UTF8 string from the passed in object.
35       *
36       * @exception IllegalArgumentException if the object cannot be converted.
37       */
38      public static DERUTF8String getInstance(
39          Object  obj)
40      {
41          if (obj == null || obj instanceof DERUTF8String)
42          {
43              return (DERUTF8String)obj;
44          }
45  
46          if (obj instanceof ASN1OctetString)
47          {
48              return new DERUTF8String(((ASN1OctetString)obj).getOctets());
49          }
50  
51          if (obj instanceof ASN1TaggedObject)
52          {
53              return getInstance(((ASN1TaggedObject)obj).getObject());
54          }
55  
56          throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
57      }
58  
59      /**
60       * return an UTF8 String from a tagged object.
61       *
62       * @param obj the tagged object holding the object we want
63       * @param explicit true if the object is meant to be explicitly
64       *              tagged false otherwise.
65       * @exception IllegalArgumentException if the tagged object cannot
66       *               be converted.
67       */
68      public static DERUTF8String getInstance(
69          ASN1TaggedObject obj,
70          boolean          explicit)
71      {
72          return getInstance(obj.getObject());
73      }
74  
75      /**
76       * basic constructor - byte encoded string.
77       */
78      DERUTF8String(
79          byte[]   string)
80      {
81          int i = 0;
82          int length = 0;
83  
84          while (i < string.length)
85          {
86              length++;
87              if ((string[i] & 0xe0) == 0xe0)
88              {
89                  i += 3;
90              }
91              else if ((string[i] & 0xc0) == 0xc0)
92              {
93                  i += 2;
94              }
95              else
96              {
97                  i += 1;
98              }
99          }
100 
101         char[]  cs = new char[length];
102 
103         i = 0;
104         length = 0;
105 
106         while (i < string.length)
107         {
108             char    ch;
109 
110             if ((string[i] & 0xe0) == 0xe0)
111             {
112                 ch = (char)(((string[i] & 0x1f) << 12)
113                       | ((string[i + 1] & 0x3f) << 6) | (string[i + 2] & 0x3f));
114                 i += 3;
115             }
116             else if ((string[i] & 0xc0) == 0xc0)
117             {
118                 ch = (char)(((string[i] & 0x3f) << 6) | (string[i + 1] & 0x3f));
119                 i += 2;
120             }
121             else
122             {
123                 ch = (char)(string[i] & 0xff);
124                 i += 1;
125             }
126 
127             cs[length++] = ch;
128         }
129 
130         this.string = new String(cs);
131     }
132 
133     /**
134      * basic constructor
135      */
136     public DERUTF8String(
137         String   string)
138     {
139         this.string = string;
140     }
141 
142     public String getString()
143     {
144         return string;
145     }
146 
147     public int hashCode()
148     {
149         return this.getString().hashCode();
150     }
151 
152     public boolean equals(
153         Object  o)
154     {
155         if (!(o instanceof DERUTF8String))
156         {
157             return false;
158         }
159 
160         DERUTF8String  s = (DERUTF8String)o;
161 
162         return this.getString().equals(s.getString());
163     }
164 
165     void encode(
166         DEROutputStream  out)
167         throws IOException
168     {
169         char[]                  c = string.toCharArray();
170         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
171 
172         for (int i = 0; i != c.length; i++)
173         {
174             char    ch = c[i];
175 
176             if (ch < 0x0080)
177             {
178                 bOut.write(ch);
179             }
180             else if (ch < 0x0800)
181             {
182                 bOut.write(0xc0 | (ch >> 6));
183                 bOut.write(0x80 | (ch & 0x3f));
184             }
185             else
186             {
187                 bOut.write(0xe0 | (ch >> 12));
188                 bOut.write(0x80 | ((ch >> 6) & 0x3F));
189                 bOut.write(0x80 | (ch & 0x3F));
190             }
191         }
192 
193         out.writeEncoded(UTF8_STRING, bOut.toByteArray());
194     }
195 }