FileDocCategorySizeDatePackage
BitString.javaAPI DocAndroid 1.5 API3510Wed May 06 22:41:06 BST 2009org.apache.harmony.security.asn1

BitString

public final class BitString extends Object
Represents ASN.1 bit string value
see
ASN.1

Fields Summary
private static final byte[]
SET_MASK
private static final byte[]
RESET_MASK
public final byte[]
bytes
Sequence of bits padded with unused bits.
public final int
unusedBits
Number of unused bits in the last byte.
Constructors Summary
public BitString(byte[] bytes, int unusedBits)
Constructs bit string

param
bytes - array of bytes that represents bit string, including unused bits
param
unusedBits - number of unused bits
throws
IllegalArgumentException - if parameters are invalid


                                                       
         

        // constraints are set according X.690
        if (unusedBits < 0 || unusedBits > 7) {
            throw new IllegalArgumentException(
                    Messages.getString("security.13D")); //$NON-NLS-1$
        }

        if (bytes.length == 0 && unusedBits != 0) {
            throw new IllegalArgumentException(
                    Messages.getString("security.13E")); //$NON-NLS-1$
        }

        this.bytes = bytes;
        this.unusedBits = unusedBits;
    
public BitString(boolean[] values)
Constructs bit string from array of booleans

param
values - array of booleans

        unusedBits = values.length % 8;
        int size = values.length / 8;
        if (unusedBits != 0) {
            size++;
        }
        bytes = new byte[size];
        for (int i = 0; i < values.length; i++) {
            setBit(i, values[i]);
        }
    
Methods Summary
public booleangetBit(int bit)

        int offset = bit % 8;
        int index = bit / 8;
        return (bytes[index] & SET_MASK[offset]) != 0;
    
public voidsetBit(int bit, boolean value)

        int offset = bit % 8;
        int index = bit / 8;
        if (value) {
            bytes[index] |= SET_MASK[offset];
        } else {
            bytes[index] &= RESET_MASK[offset];
        }
    
public boolean[]toBooleanArray()

        boolean[] result = new boolean[bytes.length * 8 - unusedBits];
        for (int i = 0; i < result.length; i++) {
            result[i] = getBit(i);
        }
        return result;