KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > ASN1OctetString


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.util.Enumeration;
6 import java.util.Vector;
7
8 public abstract class ASN1OctetString
9     extends DERObject
10 {
11     byte[] string;
12
13     /**
14      * return an Octet String from a tagged object.
15      *
16      * @param obj the tagged object holding the object we want.
17      * @param explicit true if the object is meant to be explicitly
18      * tagged false otherwise.
19      * @exception IllegalArgumentException if the tagged object cannot
20      * be converted.
21      */

22     public static ASN1OctetString getInstance(
23         ASN1TaggedObject obj,
24         boolean explicit)
25     {
26         return getInstance(obj.getObject());
27     }
28     
29     /**
30      * return an Octet String from the given object.
31      *
32      * @param obj the object we want converted.
33      * @exception IllegalArgumentException if the object cannot be converted.
34      */

35     public static ASN1OctetString getInstance(
36         Object obj)
37     {
38         if (obj == null || obj instanceof ASN1OctetString)
39         {
40             return (ASN1OctetString)obj;
41         }
42
43         if (obj instanceof ASN1TaggedObject)
44         {
45             return getInstance(((ASN1TaggedObject)obj).getObject());
46         }
47
48         if (obj instanceof ASN1Sequence)
49         {
50             Vector v = new Vector();
51             Enumeration e = ((ASN1Sequence)obj).getObjects();
52
53             while (e.hasMoreElements())
54             {
55                 v.addElement(e.nextElement());
56             }
57
58             return new BERConstructedOctetString(v);
59         }
60
61         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
62     }
63
64     /**
65      * @param string the octets making up the octet string.
66      */

67     public ASN1OctetString(
68         byte[] string)
69     {
70         this.string = string;
71     }
72
73     public ASN1OctetString(
74         DEREncodable obj)
75     {
76         try
77         {
78             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
79             DEROutputStream dOut = new DEROutputStream(bOut);
80
81             dOut.writeObject(obj);
82             dOut.close();
83
84             this.string = bOut.toByteArray();
85         }
86         catch (IOException e)
87         {
88             throw new IllegalArgumentException("Error processing object : " + e.toString());
89         }
90     }
91
92     public byte[] getOctets()
93     {
94         return string;
95     }
96
97     public int hashCode()
98     {
99         byte[] b = this.getOctets();
100         int value = 0;
101
102         for (int i = 0; i != b.length; i++)
103         {
104             value ^= (b[i] & 0xff) << (i % 4);
105         }
106
107         return value;
108     }
109
110     public boolean equals(
111         Object o)
112     {
113         if (o == null || !(o instanceof DEROctetString))
114         {
115             return false;
116         }
117
118         DEROctetString other = (DEROctetString)o;
119
120         byte[] b1 = other.getOctets();
121         byte[] b2 = this.getOctets();
122
123         if (b1.length != b2.length)
124         {
125             return false;
126         }
127
128         for (int i = 0; i != b1.length; i++)
129         {
130             if (b1[i] != b2[i])
131             {
132                 return false;
133             }
134         }
135
136         return true;
137     }
138
139     abstract void encode(DEROutputStream out)
140         throws IOException;
141 }
142
Popular Tags