@Test(expected = IllegalArgumentException.class) public void testEncodeBitString32As8() { ByteBuffer buf = ByteBuffer.wrap(new byte[7]); buf.position(buf.position() + 7); BitSet bits = new BitSet(32); bits.set(8); Asn1Utils.encodeBitString(bits, 8, buf); }
public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) { if (value == null || nbits < value.length()) { throw new IllegalArgumentException(); } int pos = buf.position(); int contentLength = (int) Math.ceil(nbits / 8.0d); for (int i = contentLength; i > 0; i--) { byte octet = 0; for (int j = (i - 1) * 8; j < i * 8; j++) { if (value.get(j)) { octet |= BIT_STRING_MASK[j % 8]; } } pos--; buf.put(pos, octet); } pos--; buf.put(pos, (byte) 0); contentLength++; buf.position(buf.position() - contentLength); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM, contentLength, buf); return headerLength + contentLength; }
Asn1Utils { public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) { if (value == null || nbits < value.length()) { throw new IllegalArgumentException(); } int pos = buf.position(); int contentLength = (int) Math.ceil(nbits / 8.0d); for (int i = contentLength; i > 0; i--) { byte octet = 0; for (int j = (i - 1) * 8; j < i * 8; j++) { if (value.get(j)) { octet |= BIT_STRING_MASK[j % 8]; } } pos--; buf.put(pos, octet); } pos--; buf.put(pos, (byte) 0); contentLength++; buf.position(buf.position() - contentLength); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM, contentLength, buf); return headerLength + contentLength; } }
Asn1Utils { public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) { if (value == null || nbits < value.length()) { throw new IllegalArgumentException(); } int pos = buf.position(); int contentLength = (int) Math.ceil(nbits / 8.0d); for (int i = contentLength; i > 0; i--) { byte octet = 0; for (int j = (i - 1) * 8; j < i * 8; j++) { if (value.get(j)) { octet |= BIT_STRING_MASK[j % 8]; } } pos--; buf.put(pos, octet); } pos--; buf.put(pos, (byte) 0); contentLength++; buf.position(buf.position() - contentLength); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM, contentLength, buf); return headerLength + contentLength; } private  Asn1Utils(); }
Asn1Utils { public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) { if (value == null || nbits < value.length()) { throw new IllegalArgumentException(); } int pos = buf.position(); int contentLength = (int) Math.ceil(nbits / 8.0d); for (int i = contentLength; i > 0; i--) { byte octet = 0; for (int j = (i - 1) * 8; j < i * 8; j++) { if (value.get(j)) { octet |= BIT_STRING_MASK[j % 8]; } } pos--; buf.put(pos, octet); } pos--; buf.put(pos, (byte) 0); contentLength++; buf.position(buf.position() - contentLength); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM, contentLength, buf); return headerLength + contentLength; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length); }
Asn1Utils { public static int encodeBitString(BitSet value, int nbits, ByteBuffer buf) { if (value == null || nbits < value.length()) { throw new IllegalArgumentException(); } int pos = buf.position(); int contentLength = (int) Math.ceil(nbits / 8.0d); for (int i = contentLength; i > 0; i--) { byte octet = 0; for (int j = (i - 1) * 8; j < i * 8; j++) { if (value.get(j)) { octet |= BIT_STRING_MASK[j % 8]; } } pos--; buf.put(pos, octet); } pos--; buf.put(pos, (byte) 0); contentLength++; buf.position(buf.position() - contentLength); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM, contentLength, buf); return headerLength + contentLength; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length);  }