2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERBitString.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5 public class DERBitString
6     extends DERObject
7 {
8     protected byte[]      data;
9     protected int         padBits;
10
11     protected DERBitString(
12         byte    data,
13         int     padBits)
14     {
15         this.data = new byte[1];
16         this.data[0] = data;
17         this.padBits = padBits;
18     }
19
20     /**
21      * @param data the octets making up the bit string.
22      * @param padBits the number of extra bits at the end of the string.
23      */
24     public DERBitString(
25         byte[]  data,
26         int     padBits)
27     {
28         this.data = data;
29         this.padBits = padBits;
30     }
31
32     public DERBitString(
33         byte[]  data)
34     {
35         this(data, 0);
36     }
37
38     public DERBitString(
39         DERObject  obj)
40     {
41         try
42         {
43             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
44             DEROutputStream         dOut = new DEROutputStream(bOut);
45
46             dOut.writeObject(obj);
47             dOut.close();
48
49             this.data = bOut.toByteArray();
50             this.padBits = 0;
51         }
52         catch (IOException e)
53         {
54             throw new IllegalArgumentException("Error processing object : " + e.toString());
55         }
56     }
57
58     public DERBitString(
59         DEREncodable  obj)
60     {
61         this(obj.getDERObject());
62     }
63
64     public byte[] getBytes()
65     {
66         return data;
67     }
68
69     public int getPadBits()
70     {
71         return padBits;
72     }
73
74     void encode(
75         DEROutputStream  out)
76         throws IOException
77     {
78         byte[]  bytes = new byte[getBytes().length + 1];
79
80         bytes[0] = (byte)getPadBits();
81         System.arraycopy(getBytes(), 0, bytes, 1, bytes.length - 1);
82
83         out.writeEncoded(BIT_STRING, bytes);
84     }
85 }