1 7 8 9 package org.enhydra.oyster.der; 10 11 12 import org.enhydra.oyster.exception.SMIMEException; 13 14 15 19 public class DERBitString extends DERObject { 20 21 28 public DERBitString (String bitStr0) throws SMIMEException 29 { 30 super(3); 31 super.addContent(this.UnusedBitsFromString(bitStr0)); 32 super.addContent(this.ConvertStringBitsToByteArray(bitStr0)); 33 } 34 35 42 public DERBitString (boolean[] bitsArray0) throws SMIMEException 43 { 44 super(3); 45 String 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 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; super.addContent(temp); 75 super.addContent(bitStr0); } 77 78 85 public DERBitString (byte[] bitStr0) throws SMIMEException 86 { 87 this(bitStr0, 0); 88 } 89 90 95 private byte[] UnusedBitsFromString (String bitStr0) { 96 byte[] temp = new byte[1]; 97 int len = bitStr0.length(); 98 temp[0] = (byte)(8 - len%8); if (temp[0] == 8) 100 temp[0] = 0; 101 return temp; 102 } 103 104 111 private byte[] ConvertStringBitsToByteArray (String 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 |