org.jasn
Class BitString

java.lang.Object
  extended byorg.jasn.BitString
All Implemented Interfaces:
Cloneable

public class BitString
extends Object
implements Cloneable

A sequence of bits modelling the ASN.1 BIT STRING type. The BIT STRING type denotes an arbitrary string of bits (ones and zeroes). A BIT STRING value can have any length, including zero.

A BitString differs from the BitSet class in that the length method of the former returns the number of bits stored and not the number of bit set.

Each BitString instance has a capacity. The capacity is the size of the array used to store the bits. It is always at least as large as the required number of bytes used to store the bits in the sequence. As bits are added an BitString, its capacity grows automatically.

A BitString is not safe for multithreaded use without external synchronization.

Author:
Nicolas Vraux

Constructor Summary
BitString()
          Creates a new empty bit sequence with an initial capacity of 32 bits.
BitString(boolean[] bits)
          Creates a bit sequence from the specified boolean array.
BitString(int nbits)
          Creates an empty bit sequence whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1 inclusive.
 
Method Summary
 void add(BitString bs)
          Appends all the bits in the specified BitString to the end of this BitString.
 void add(boolean bit)
          Appends the specified bit value to the end of this bit string.
 void clear(int bitIndex)
          Sets the bit specified by the index to false.
 Object clone()
          Creates and returns a (deep) copy this BitString which is equal to it.
 boolean equals(Object obj)
          Compares this object against the specified object.
 boolean get(int bitIndex)
          Returns the value of the bit with the specified index.
 int getByteCount()
          Returns the number of bytes needed to store the bits contained in this BitString.
 int getUnusedBits()
          Returns the number of unused bits in the last byte.
 int hashCode()
          Returns a hash code value for this BitString.
 int length()
          Returns the number of bits contained in this BitString.
 void set(int bitIndex)
          Sets the bit specified by the index to true.
 boolean[] toBooleanArray()
          Returns a boolean array with the same bit values a this BitString.
 byte[] toByteArray()
          Returns a byte array representation of this BitString.
 int toByteArray(byte[] buf, int offset)
          Packs the bits of this BitString into the specified destination byte array, starting at offset offset.
 String toString()
          Returns a string representation of this BitString.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitString

public BitString()
Creates a new empty bit sequence with an initial capacity of 32 bits.


BitString

public BitString(int nbits)
Creates an empty bit sequence whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1 inclusive.

Parameters:
nbits - the initial size of the bit set.
Throws:
NegativeArraySizeException - if the specified initial size is negative.

BitString

public BitString(boolean[] bits)
Creates a bit sequence from the specified boolean array. The value of the first boolean is the value first bit, ...

Method Detail

length

public int length()
Returns the number of bits contained in this BitString.

Returns:
the number of bits contained in this BitString.

getUnusedBits

public int getUnusedBits()
Returns the number of unused bits in the last byte.

Returns:
the number of unused bits in the last byte, i.e 0 if the number of bits stored is a multiple of 8, 8 - length % 8 otherwise.

getByteCount

public int getByteCount()
Returns the number of bytes needed to store the bits contained in this BitString.


get

public boolean get(int bitIndex)
Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this BitSet; otherwise, the result is false.

Parameters:
bitIndex - the bit index.
Returns:
the value of the bit with the specified index.
Throws:
IndexOutOfBoundsException - if the specified index is negative or greater than or equals to the length of this BitString.

set

public void set(int bitIndex)
Sets the bit specified by the index to true.

Parameters:
bitIndex - a bit index.
Throws:
IndexOutOfBoundsException - if the specified index is negative or greater than or equals to the length of this BitString.

clear

public void clear(int bitIndex)
Sets the bit specified by the index to false.

Parameters:
bitIndex - the index of the bit to be cleared.
Throws:
IndexOutOfBoundsException - if the specified index is negative or greater than or equals to the length of this BitString.

add

public void add(boolean bit)
Appends the specified bit value to the end of this bit string. The length of this bit string is then increased by one.

Parameters:
bit - the value of the bit to be appended.

add

public void add(BitString bs)
Appends all the bits in the specified BitString to the end of this BitString.

Parameters:
bs - sequence of bits which are to be added to the BitString.
Throws:
NullPointerException - if the specified bit string is null.

toByteArray

public byte[] toByteArray()
Returns a byte array representation of this BitString.

Returns:
a byte array representation of this BitString.

toByteArray

public int toByteArray(byte[] buf,
                       int offset)
Packs the bits of this BitString into the specified destination byte array, starting at offset offset.

The bits in the bitstring, commencing with first bit and proceeding to the trailing bit, are placed in bits 8 to 1 of the byte buf[offset], followed by bits 8 to 1 of the byte buf[offset+1], etc.

Note:
If the number of bits is not a multiple of 8, the last byte of the destination buffer is partially filled.

Returns:
the number of bytes stored in buf.
Throws:
NullPointerException - if buf is null.
IndexOutOfBoundsException - if offset is negative or greater/equals than/to buf.length.

toBooleanArray

public boolean[] toBooleanArray()
Returns a boolean array with the same bit values a this BitString.


hashCode

public int hashCode()
Returns a hash code value for this BitString.

Returns:
a hash code value for this BitString.

equals

public boolean equals(Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Bitset object that has exactly the same sequence of bits as this bit sequence.

Parameters:
obj - the object to compare with.
Returns:
true if the objects represent the same sequence of bits, false otherwise.

clone

public Object clone()
Creates and returns a (deep) copy this BitString which is equal to it.

Returns:
a copy of this BitString.

toString

public String toString()
Returns a string representation of this BitString.

Returns:
a string representation of this BitString.


Copyright © 2004 Nicolas Vraux. All Rights Reserved.