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  /**
24   * DER T61String (also the teletex string)
25   */
26  public class DERT61String
27      extends DERObject
28      implements DERString
29  {
30      String  string;
31  
32      /**
33       * return a T61 string from the passed in object.
34       *
35       * @exception IllegalArgumentException if the object cannot be converted.
36       */
37      public static DERT61String getInstance(
38          Object  obj)
39      {
40          if (obj == null || obj instanceof DERT61String)
41          {
42              return (DERT61String)obj;
43          }
44  
45          if (obj instanceof ASN1OctetString)
46          {
47              return new DERT61String(((ASN1OctetString)obj).getOctets());
48          }
49  
50          if (obj instanceof ASN1TaggedObject)
51          {
52              return getInstance(((ASN1TaggedObject)obj).getObject());
53          }
54  
55          throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
56      }
57  
58      /**
59       * return an T61 String from a tagged object.
60       *
61       * @param obj the tagged object holding the object we want
62       * @param explicit true if the object is meant to be explicitly
63       *              tagged false otherwise.
64       * @exception IllegalArgumentException if the tagged object cannot
65       *               be converted.
66       */
67      public static DERT61String getInstance(
68          ASN1TaggedObject obj,
69          boolean          explicit)
70      {
71          return getInstance(obj.getObject());
72      }
73  
74      /**
75       * basic constructor - with bytes.
76       */
77      public DERT61String(
78          byte[]   string)
79      {
80          char[]  cs = new char[string.length];
81  
82          for (int i = 0; i != cs.length; i++)
83          {
84              cs[i] = (char)(string[i] & 0xff);
85          }
86  
87          this.string = new String(cs);
88      }
89  
90      /**
91       * basic constructor - with string.
92       */
93      public DERT61String(
94          String   string)
95      {
96          this.string = string;
97      }
98  
99      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 }