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.IOException;
22  
23  public class DERGeneralString
24      extends DERObject implements DERString
25  {
26      private String string;
27  
28      public static DERGeneralString getInstance(
29          Object obj)
30      {
31          if (obj == null || obj instanceof DERGeneralString)
32          {
33              return (DERGeneralString) obj;
34          }
35          if (obj instanceof ASN1OctetString)
36          {
37              return new DERGeneralString(((ASN1OctetString) obj).getOctets());
38          }
39          if (obj instanceof ASN1TaggedObject)
40          {
41              return getInstance(((ASN1TaggedObject) obj).getObject());
42          }
43          throw new IllegalArgumentException("illegal object in getInstance: "
44                  + obj.getClass().getName());
45      }
46  
47      public static DERGeneralString getInstance(
48          ASN1TaggedObject obj,
49          boolean explicit)
50      {
51          return getInstance(obj.getObject());
52      }
53  
54      public DERGeneralString(byte[] string)
55      {
56          char[] cs = new char[string.length];
57          for (int i = 0; i != cs.length; i++)
58          {
59              cs[i] = (char)(string[i] & 0xff);
60          }
61          this.string = new String(cs);
62      }
63  
64      public DERGeneralString(String string)
65      {
66          this.string = string;
67      }
68  
69      public String getString()
70      {
71          return string;
72      }
73  
74      public byte[] getOctets()
75      {
76          char[] cs = string.toCharArray();
77          byte[] bs = new byte[cs.length];
78          for (int i = 0; i != cs.length; i++)
79          {
80              bs[i] = (byte) cs[i];
81          }
82          return bs;
83      }
84  
85      void encode(DEROutputStream out)
86          throws IOException
87      {
88          out.writeEncoded(GENERAL_STRING, this.getOctets());
89      }
90  
91      public int hashCode()
92      {
93          return this.getString().hashCode();
94      }
95  
96      public boolean equals(Object o)
97      {
98          if (!(o instanceof DERGeneralString))
99          {
100             return false;
101         }
102         DERGeneralString s = (DERGeneralString) o;
103         return this.getString().equals(s.getString());
104     }
105 }