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  import java.util.Enumeration;
24  import java.util.Vector;
25  
26  public abstract class ASN1OctetString
27      extends DERObject
28  {
29      byte[]  string;
30  
31      /**
32       * return an Octet String from a tagged object.
33       *
34       * @param obj the tagged object holding the object we want.
35       * @param explicit true if the object is meant to be explicitly
36       *              tagged false otherwise.
37       * @exception IllegalArgumentException if the tagged object cannot
38       *              be converted.
39       */
40      public static ASN1OctetString getInstance(
41          ASN1TaggedObject    obj,
42          boolean             explicit)
43      {
44          return getInstance(obj.getObject());
45      }
46  
47      /**
48       * return an Octet String from the given object.
49       *
50       * @param obj the object we want converted.
51       * @exception IllegalArgumentException if the object cannot be converted.
52       */
53      public static ASN1OctetString getInstance(
54          Object  obj)
55      {
56          if (obj == null || obj instanceof ASN1OctetString)
57          {
58              return (ASN1OctetString)obj;
59          }
60  
61          if (obj instanceof ASN1TaggedObject)
62          {
63              return getInstance(((ASN1TaggedObject)obj).getObject());
64          }
65  
66          if (obj instanceof ASN1Sequence)
67          {
68              Vector      v = new Vector();
69              Enumeration e = ((ASN1Sequence)obj).getObjects();
70  
71              while (e.hasMoreElements())
72              {
73                  v.addElement(e.nextElement());
74              }
75  
76              return new BERConstructedOctetString(v);
77          }
78  
79          throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
80      }
81  
82      /**
83       * @param string the octets making up the octet string.
84       */
85      public ASN1OctetString(
86          byte[]  string)
87      {
88          this.string = string;
89      }
90  
91      public ASN1OctetString(
92          DEREncodable obj)
93      {
94          try
95          {
96              ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
97              DEROutputStream         dOut = new DEROutputStream(bOut);
98  
99              dOut.writeObject(obj);
100             dOut.close();
101 
102             this.string = bOut.toByteArray();
103         }
104         catch (IOException e)
105         {
106             throw new IllegalArgumentException("Error processing object : " + e.toString());
107         }
108     }
109 
110     public byte[] getOctets()
111     {
112         return string;
113     }
114 
115     public int hashCode()
116     {
117         byte[]  b = this.getOctets();
118         int     value = 0;
119 
120         for (int i = 0; i != b.length; i++)
121         {
122             value ^= (b[i] & 0xff) << (i % 4);
123         }
124 
125         return value;
126     }
127 
128     public boolean equals(
129         Object  o)
130     {
131         if (o == null || !(o instanceof DEROctetString))
132         {
133             return false;
134         }
135 
136         DEROctetString  other = (DEROctetString)o;
137 
138         byte[] b1 = other.getOctets();
139         byte[] b2 = this.getOctets();
140 
141         if (b1.length != b2.length)
142         {
143             return false;
144         }
145 
146         for (int i = 0; i != b1.length; i++)
147         {
148             if (b1[i] != b2[i])
149             {
150                 return false;
151             }
152         }
153 
154         return true;
155     }
156 
157     abstract void encode(DEROutputStream out)
158         throws IOException;
159 }