KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 public class DERBitString
24     extends DERObject
25     implements DERString
26 {
27     private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
28
29     protected byte[] data;
30     protected int padBits;
31
32     /**
33      * return the correct number of pad bits for a bit string defined in
34      * a 32 bit constant
35      */

36     static protected int getPadBits(
37         int bitString)
38     {
39         int val = 0;
40         for (int i = 3; i >= 0; i--)
41         {
42             //
43
// this may look a little odd, but if it isn't done like this pre jdk1.2
44
// JVM's break!
45
//
46
if (i != 0)
47             {
48                 if ((bitString >> (i * 8)) != 0)
49                 {
50                     val = (bitString >> (i * 8)) & 0xFF;
51                     break;
52                 }
53             }
54             else
55             {
56                 if (bitString != 0)
57                 {
58                     val = bitString & 0xFF;
59                     break;
60                 }
61             }
62         }
63
64         if (val == 0)
65         {
66             return 7;
67         }
68
69
70         int bits = 1;
71
72         while (((val <<= 1) & 0xFF) != 0)
73         {
74             bits++;
75         }
76
77         return 8 - bits;
78     }
79
80     /**
81      * return the correct number of bytes for a bit string defined in
82      * a 32 bit constant
83      */

84     static protected byte[] getBytes(int bitString)
85     {
86         int bytes = 4;
87         for (int i = 3; i >= 1; i--)
88         {
89             if ((bitString & (0xFF << (i * 8))) != 0)
90             {
91                 break;
92             }
93             bytes--;
94         }
95
96         byte[] result = new byte[bytes];
97         for (int i = 0; i < bytes; i++)
98         {
99             result[i] = (byte) ((bitString >> (i * 8)) & 0xFF);
100         }
101
102         return result;
103     }
104
105     /**
106      * return a Bit String from the passed in object
107      *
108      * @exception IllegalArgumentException if the object cannot be converted.
109      */

110     public static DERBitString getInstance(
111         Object JavaDoc obj)
112     {
113         if (obj == null || obj instanceof DERBitString)
114         {
115             return (DERBitString)obj;
116         }
117
118         if (obj instanceof ASN1OctetString)
119         {
120             byte[] bytes = ((ASN1OctetString)obj).getOctets();
121             int padBits = bytes[0];
122             byte[] data = new byte[bytes.length - 1];
123
124             System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
125
126             return new DERBitString(data, padBits);
127         }
128
129         if (obj instanceof ASN1TaggedObject)
130         {
131             return getInstance(((ASN1TaggedObject)obj).getObject());
132         }
133
134         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
135     }
136
137     /**
138      * return a Bit String from a tagged object.
139      *
140      * @param obj the tagged object holding the object we want
141      * @param explicit true if the object is meant to be explicitly
142      * tagged false otherwise.
143      * @exception IllegalArgumentException if the tagged object cannot
144      * be converted.
145      */

146     public static DERBitString getInstance(
147         ASN1TaggedObject obj,
148         boolean explicit)
149     {
150         return getInstance(obj.getObject());
151     }
152
153     protected DERBitString(
154         byte data,
155         int padBits)
156     {
157         this.data = new byte[1];
158         this.data[0] = data;
159         this.padBits = padBits;
160     }
161
162     /**
163      * @param data the octets making up the bit string.
164      * @param padBits the number of extra bits at the end of the string.
165      */

166     public DERBitString(
167         byte[] data,
168         int padBits)
169     {
170         this.data = data;
171         this.padBits = padBits;
172     }
173
174     public DERBitString(
175         byte[] data)
176     {
177         this(data, 0);
178     }
179
180     public DERBitString(
181         DEREncodable obj)
182     {
183         try
184         {
185             ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
186             DEROutputStream dOut = new DEROutputStream(bOut);
187
188             dOut.writeObject(obj);
189             dOut.close();
190
191             this.data = bOut.toByteArray();
192             this.padBits = 0;
193         }
194         catch (IOException JavaDoc e)
195         {
196             throw new IllegalArgumentException JavaDoc("Error processing object : " + e.toString());
197         }
198     }
199
200     public byte[] getBytes()
201     {
202         return data;
203     }
204
205     public int getPadBits()
206     {
207         return padBits;
208     }
209
210
211     /**
212      * @return the value of the bit string as an int (truncating if necessary)
213      */

214     public int intValue()
215     {
216         int value = 0;
217
218         for (int i = 0; i != data.length && i != 4; i++)
219         {
220             value |= (data[i] & 0xff) << (8 * i);
221         }
222
223         return value;
224     }
225
226     void encode(
227         DEROutputStream out)
228         throws IOException JavaDoc
229     {
230         byte[] bytes = new byte[getBytes().length + 1];
231
232         bytes[0] = (byte)getPadBits();
233         System.arraycopy(getBytes(), 0, bytes, 1, bytes.length - 1);
234
235         out.writeEncoded(BIT_STRING, bytes);
236     }
237
238     public int hashCode()
239     {
240         int value = 0;
241
242         for (int i = 0; i != data.length; i++)
243         {
244             value ^= (data[i] & 0xff) << (i % 4);
245         }
246
247         return value;
248     }
249
250     public boolean equals(
251         Object JavaDoc o)
252     {
253         if (o == null || !(o instanceof DERBitString))
254         {
255             return false;
256         }
257
258         DERBitString other = (DERBitString)o;
259
260         if (data.length != other.data.length)
261         {
262             return false;
263         }
264
265         for (int i = 0; i != data.length; i++)
266         {
267             if (data[i] != other.data[i])
268             {
269                 return false;
270             }
271         }
272
273         return (padBits == other.padBits);
274     }
275
276     public String JavaDoc getString()
277     {
278         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("#");
279         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
280         ASN1OutputStream aOut = new ASN1OutputStream(bOut);
281
282         try
283         {
284             aOut.writeObject(this);
285         }
286         catch (IOException JavaDoc e)
287         {
288            throw new RuntimeException JavaDoc("internal error encoding BitString");
289         }
290
291         byte[] string = bOut.toByteArray();
292
293         for (int i = 0; i != string.length; i++)
294         {
295             buf.append(table[(string[i] >>> 4) % 0xf]);
296             buf.append(table[string[i] & 0xf]);
297         }
298
299         return buf.toString();
300     }
301 }
302
Popular Tags