KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > der > DERBitString


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport capabilities.
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.der;
10
11
12 import org.enhydra.oyster.exception.SMIMEException;
13
14
15 /**
16  * DERBitString is primitive type of DER encoded object which represents bit
17  * string (array of bits) in ASN.1 notation.
18  */

19 public class DERBitString extends DERObject {
20
21 /**
22  * Constructs DERBitString object from string which has only two values of his
23  * elements: 0 and 1
24  * @param bitStr0 String consisting of 0 and 1 elements
25  * @exception SMIMEException thrown in super class constructor or in super class
26  * method addContent.
27  */

28   public DERBitString (String JavaDoc bitStr0) throws SMIMEException
29   {
30     super(3);
31     super.addContent(this.UnusedBitsFromString(bitStr0));
32     super.addContent(this.ConvertStringBitsToByteArray(bitStr0));
33   }
34
35 /**
36  * Constructs DERBitString object from boolean array which has only two values
37  * of his elements: true and false.
38  * @param bitsArray0 array consisting of boolean elements
39  * @exception SMIMEException thrown in super class constructor or in super class
40  * method addContent.
41  */

42   public DERBitString (boolean[] bitsArray0) throws SMIMEException
43   {
44     super(3);
45     String JavaDoc temp = "";
46     for (int i = 0; i != bitsArray0.length; i++) {
47       if (bitsArray0[i])
48         temp = temp.concat("1");
49       else
50         temp = temp.concat("0");
51     }
52     super.addContent(this.UnusedBitsFromString(temp));
53     super.addContent(this.ConvertStringBitsToByteArray(temp));
54   }
55
56 /**
57  * Constructs DERBitString object from bits organised in bytes with information
58  * about last few unused bits. This information is important in generation of
59  * DER object because first byte in Content Octets part of DERBitString
60  * represents number of DERBitString.
61  * @param bitStr0 bit string represented as byte array
62  * @param unused0 number of last few unused bits from last input byte
63  * (must be in range 0-7)
64  * @exception SMIMEException if number of unused bits isn't between 0-7. Also,
65  * it can be thrown in super class constructor or addContent method.
66  */

67   public DERBitString (byte[] bitStr0, int unused0) throws SMIMEException
68   {
69     super(3);
70     if (unused0 > 7 || unused0 < 0)
71       throw new SMIMEException(this, 1006);
72     byte[] temp = new byte[1];
73     temp[0] = (byte)unused0; // Number of a last few unused bits
74
super.addContent(temp);
75     super.addContent(bitStr0); //  bitStr0 is a byte array containing the two's-complement binary representation of a bit String
76
}
77
78 /**
79  * Constructs bit string represented as byte array without information about
80  * unused bits (no bits are unused).
81  * @param bitStr0 bit string represented as byte array
82  * @exception SMIMEException thrown in super class constructor or addContent
83  * method of super class.
84  */

85   public DERBitString (byte[] bitStr0) throws SMIMEException
86   {
87     this(bitStr0, 0);
88   }
89
90 /**
91  * Finds number of unused last bits (0-7) from bit string converted into byte array
92  * @param bitStr0 bit string for verification number of unused bits.
93  * @return Number between 0-7
94  */

95   private byte[] UnusedBitsFromString (String JavaDoc bitStr0) {
96     byte[] temp = new byte[1];
97     int len = bitStr0.length();
98     temp[0] = (byte)(8 - len%8); // Number of unused bits
99
if (temp[0] == 8)
100       temp[0] = 0;
101     return temp;
102   }
103
104 /**
105  * Converts String consisting of 0 and 1 into byte array
106  * @param bitStr0 bit string for transformation in byte array of the Content
107  * Octets
108  * @return Content Octets of DER encoded bit string object
109  * @exception SMIMEException if bit string has values other than 0 and 1
110  */

111   private byte[] ConvertStringBitsToByteArray (String JavaDoc bitStr0) throws SMIMEException {
112     int len = bitStr0.length();
113     if (len%8 == 0)
114       len = len/8;
115     else
116       len = len/8 + 1;
117     int[] temp = new int[len];
118     for (int i = 0; i != bitStr0.length(); i++) {
119       if (!(bitStr0.charAt(i) == '0' || bitStr0.charAt(i) == '1'))
120         throw new SMIMEException(this, 1007);
121       if ((i%8) == 0)
122         temp[i/8] = 0;
123       temp[i/8] = temp[i/8] + (int)((byte)bitStr0.charAt(i) - 48)*(int)Math.pow((double)2, (double)(7 - (i%8)));
124     }
125     byte[] ret = new byte[temp.length];
126     for (int i = 0; i != ret.length; i++)
127       ret[i] = (byte)temp[i];
128     return ret;
129   }
130 }
131
132
133
134
Popular Tags