KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > ASN1OctetString


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

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

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

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

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