org.demac.impl.j2me.process.base.service.webservices.asn1
Class ASN1BitString

java.lang.Object
  extended by org.demac.impl.j2me.process.base.service.webservices.asn1.ASN1AbstractType
      extended by org.demac.impl.j2me.process.base.service.webservices.asn1.ASN1BitString
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, ASN1Type

public class ASN1BitString
extends ASN1AbstractType

Represents an ASN.1 BIT STRING type. The corresponding Java type is boolean[].

This class has to modes of initialization which play a crucial role in standards compliant DER encoding. One mode initializes instances of this class as a representation of "named bits". The second mode initializes it as a plain bitstring.

An ASN.1 structure with named bits looks e.g., as follows:

 Rights ::= BIT STRING { read(0), write(1), execute(2)}
 
Such bitstrings have a special canonical encoding. The mode (and defaults) are specified in the documentation of the constructors of this class. Basically, in named bits mode, trailing zeroes are truncated from the internal representation of the bitstring.

See Also:
Serialized Form

Constructor Summary
ASN1BitString()
          Initializes an instance for decoding.
ASN1BitString(boolean[] b)
          Initializes the instance for encoding with the given bits.
ASN1BitString(byte[] b, int pad)
          Creates an instance with the given contents.
 
Method Summary
 int bitCount()
          Returns the number of bits of the bitstring representation.
 int byteCount()
           
 void decode(Decoder dec)
          Decodes this instance.
 void encode(Encoder enc)
          DOCUMENT ME!
 boolean[] getBits()
          Returns the contents bits of this instance.
 byte[] getBytes()
          Returns the contents octets of this instance.
 int getPadCount()
           
 int getTag()
          DOCUMENT ME!
 java.lang.Object getValue()
          This method calls getBits().
 boolean isNamedBits()
           
 boolean isZero()
          Returns true if the bit string contains no bits that are 1.
 void setBits(boolean[] bits)
          Sets the contents bits of this instance.
 void setBits(byte[] b, int pad)
          Sets the bit string from the given byte aray and pad count.
 java.lang.String toString()
          Returns the string representation of this ASN1BitString.
 
Methods inherited from class org.demac.impl.j2me.process.base.service.webservices.asn1.ASN1AbstractType
checkConstraints, getConstraint, getTagClass, isExplicit, isOptional, isType, readExternal, setConstraint, setExplicit, setOptional, writeExternal
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ASN1BitString

public ASN1BitString()
Initializes an instance for decoding. This initializes this instance to be decoded as a plain bitstring (no named bits). Use this for bitstrings with named bits as well. It does not make a difference for the application, but it ensures that bitstrings that have been decoded are encoded in the same way again.


ASN1BitString

public ASN1BitString(boolean[] b)
Initializes the instance for encoding with the given bits. The index of each bit corresponds to its number in the ASN.1 definition. This constructor initializes the instance as a bitstring with named bits. The array is not copied into this instance and can be modified subsequently without causing side effects.

Parameters:
b - The string of bits to be encoded.

ASN1BitString

public ASN1BitString(byte[] b,
                     int pad)
Creates an instance with the given contents. Use of this constructor copies the given byte array by reference and may cause side effects. The mode of this bitstring is plain (no named bits are assumed).

Parameters:
b - The left aligned contents bits.
pad - The number of pad bits.
Method Detail

getValue

public java.lang.Object getValue()
This method calls getBits().

Specified by:
getValue in interface ASN1Type
Specified by:
getValue in class ASN1AbstractType
Returns:
The contents bits as a boolean array.

getBits

public boolean[] getBits()
Returns the contents bits of this instance. No side effects occur when the returned array is modified.

Returns:
The contents bits.

setBits

public void setBits(boolean[] bits)
             throws ConstraintException
Sets the contents bits of this instance. This method does not cause side effects. It switches to the named bits mode.

Parameters:
bits - The contents bits that are set.
Throws:
ConstraintException - if the given bits violates the specified constraint.

setBits

public void setBits(byte[] b,
                    int pad)
             throws ConstraintException
Sets the bit string from the given byte aray and pad count. Bit 0 is the most significant bit in the first byte of the array and bit n is bit 7-(n&0x07) in byte floor(n/8). The length of the bit string is b.length*8-pad. The pad value be in the range of [0..7]. In other words the bits in the byte array are left aligned.

The given byte array may be copied by reference. Subsequent modification of it can cause side effects. The mode is set not to represent named bits.

Parameters:
b - The bits encoded into a byte array.
pad - The number of pad bits after the actual bits in the array.
Throws:
java.lang.IllegalArgumentException - if the pad value is out of range.
ConstraintException - if the given bits violates the

getBytes

public byte[] getBytes()
Returns the contents octets of this instance. The bits are left aligned and the most significant bit is a one (or else the bitstring is zero and an empty array is returned). The returned byte array is the one used internally. Modifying it causes side effects which may result in erroneous encoding. So, don't modify it!

Please also note that the bits in the bytes are left aligned. In other words, the bits are shifted to the left by the amount of pad bits. Bit X in the byte array corresponds to the logical bit with the number X minus pad count.

Returns:
The bits left aligned in a byte array with no trailing zeroes.

getPadCount

public int getPadCount()
Returns:
The number of unused bits in the last byte of the bitstring's byte array representation. This number is always in the range zero to seven.

byteCount

public int byteCount()
Returns:
The number of bytes of the bitstring's byte array representation.

bitCount

public int bitCount()
Returns the number of bits of the bitstring representation. Bits are counted only to the most significant bit that is a one. Trailing zeroes are neither present in the internal representation nor are they counted.

Returns:
The number of bits of the bitstring representation.

isNamedBits

public boolean isNamedBits()
Returns:
true iff this instance has named bits.

isZero

public boolean isZero()
Returns true if the bit string contains no bits that are 1. Otherwise, false is returned. This method is used by the DEREncoder in order to determine cases in which special encoding is to be used. If no bits of a BIT STRING are 1 then it is encoded as 0x03 0x01 0x00 even if the BIT STRING has hundreds of bits in length.

Returns:
true if all bits are zero.

getTag

public int getTag()
DOCUMENT ME!

Specified by:
getTag in interface ASN1Type
Specified by:
getTag in class ASN1AbstractType
Returns:
DOCUMENT ME!

encode

public void encode(Encoder enc)
            throws ASN1Exception,
                   java.io.IOException
DOCUMENT ME!

Specified by:
encode in interface ASN1Type
Specified by:
encode in class ASN1AbstractType
Parameters:
enc - The encoder which is used to encode
Throws:
ASN1Exception - DOCUMENT ME!
java.io.IOException - if an I/O error occures

decode

public void decode(Decoder dec)
            throws ASN1Exception,
                   java.io.IOException
Decodes this instance. After decoding, this method restores the mode (named bits vs no named bits) of this instance to the one it had before decoding. This is to maintain the original mode while assuring that encoded values are identical to decoded ones even if the encoding.

Specified by:
decode in interface ASN1Type
Specified by:
decode in class ASN1AbstractType
Parameters:
dec - The decoder which is used to decode
Throws:
java.io.IOException - if an I/O error occures
ASN1Exception

toString

public java.lang.String toString()
Returns the string representation of this ASN1BitString.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this ASN1BitString