KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > ASN1OctetString


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 public abstract class ASN1OctetString
26     extends DERObject
27 {
28     byte[] string;
29
30     /**
31      * return an Octet String from a tagged object.
32      *
33      * @param obj the tagged object holding the object we want.
34      * @param explicit true if the object is meant to be explicitly
35      * tagged false otherwise.
36      * @exception IllegalArgumentException if the tagged object cannot
37      * be converted.
38      */

39     public static ASN1OctetString getInstance(
40         ASN1TaggedObject obj,
41         boolean explicit)
42     {
43         return getInstance(obj.getObject());
44     }
45
46     /**
47      * return an Octet String from the given object.
48      *
49      * @param obj the object we want converted.
50      * @exception IllegalArgumentException if the object cannot be converted.
51      */

52     public static ASN1OctetString getInstance(
53         Object JavaDoc obj)
54     {
55         if (obj == null || obj instanceof ASN1OctetString)
56         {
57             return (ASN1OctetString)obj;
58         }
59
60         if (obj instanceof ASN1TaggedObject)
61         {
62             return getInstance(((ASN1TaggedObject)obj).getObject());
63         }
64
65         if (obj instanceof ASN1Sequence)
66         {
67             Vector JavaDoc v = new Vector JavaDoc();
68             Enumeration JavaDoc e = ((ASN1Sequence)obj).getObjects();
69
70             while (e.hasMoreElements())
71             {
72                 v.addElement(e.nextElement());
73             }
74
75             return new BERConstructedOctetString(v);
76         }
77
78         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
79     }
80
81     /**
82      * @param string the octets making up the octet string.
83      */

84     public ASN1OctetString(
85         byte[] string)
86     {
87         this.string = string;
88     }
89
90     public ASN1OctetString(
91         DEREncodable obj)
92     {
93         try
94         {
95             ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
96             DEROutputStream dOut = new DEROutputStream(bOut);
97
98             dOut.writeObject(obj);
99             dOut.close();
100
101             this.string = bOut.toByteArray();
102         }
103         catch (IOException JavaDoc e)
104         {
105             throw new IllegalArgumentException JavaDoc("Error processing object : " + e.toString());
106         }
107     }
108
109     public byte[] getOctets()
110     {
111         return string;
112     }
113
114     public int hashCode()
115     {
116         byte[] b = this.getOctets();
117         int value = 0;
118
119         for (int i = 0; i != b.length; i++)
120         {
121             value ^= (b[i] & 0xff) << (i % 4);
122         }
123
124         return value;
125     }
126
127     public boolean equals(
128         Object JavaDoc o)
129     {
130         if (o == null || !(o instanceof DEROctetString))
131         {
132             return false;
133         }
134
135         DEROctetString other = (DEROctetString)o;
136
137         byte[] b1 = other.getOctets();
138         byte[] b2 = this.getOctets();
139
140         if (b1.length != b2.length)
141         {
142             return false;
143         }
144
145         for (int i = 0; i != b1.length; i++)
146         {
147             if (b1[i] != b2[i])
148             {
149                 return false;
150             }
151         }
152
153         return true;
154     }
155
156     abstract void encode(DEROutputStream out)
157         throws IOException JavaDoc;
158 }
159
Popular Tags