public class BIT_STRING extends ASN1Object
BIT STRING is a simple ASN.1 string type identified by the UNIVERSAL TAG number 3. An ASN.1 BIT STRING object may represent any arbitrary string of bits.
The value of a BIT STRING object is DER encoded by first specifying the number of bits not used in the last of the following contents octets.
For instance: DER encoding the bit string '011010001'B will need two content
octets: 01101000 10000000
(hexadecimal 68 80); seven bits of the
last octet are not used leading to the following DER encoding (hexadecimal):
03 03 07 68 80where the first octet is the identifier octet: <03> for BIT STRING; the following length octet indicates the number (3) of content octets: <03>; and the first of the final contents octets indicates that seven bits of the last content octet will not be used: <07> <68> <80>
When creating a new ASN.1 BIT_STRING object, the value to be represented may be supplied in one of three formats:
BIT_STRING(byte[] array, int bitsNotValid)
constructor. Otherwise the encoding only will give the expected result when
there are no unused bits in the last content octet.
Consider, for instance, the bit string '011010001'B example from above: when
supplying the value as binary string or boolean array, the number (7) of
unused bits automatically is calculated giving the right encoding of
03 03 07 68 80
:
BIT_STRING bit_string = new BIT_STRING("011010001"); //or BIT_STRING bit_string = new BIT_STRING(new boolean[] {false,true,true,false,true false,false,false,true };As stated above, the content representation of the bit string '011010001'B will give two content octets of values 0x68 0x80 (hexadecimal). Now, when using these two bytes for supplying the value of a new BIT_STRING object, explicitly the number of unused bits (7) of the last content octet has to be specified:
byte[] value = { (byte) 0x68, (byte) 0x80 }; BIT_STRING bit_string = new BIT_STRING(value, 7);Otherwise it would be assumed, that all bits of the last content octet are significant (number of invalid bits = 0), and the encoding will be a different one.
When calling the getValue
method for getting the inherent
value from an ASN.1 BIT_STRING instance, a byte array is returned. For
getting a "binary string" representation of the value, use the
getBinaryString()
method.
DER en/decoding generally is done by means of the several methods of the
DerCoder
class; decoding alternatively may be
performed by using the DerInputStream
utility.
ASN1Object
,
ASN
Modifier and Type | Field and Description |
---|---|
protected int |
bits_not_valid
Number of bits not valid in the byte array of bits.
|
protected byte[] |
value
The bits of the BIT STRING in a byte array, initialized with
null . |
asnType, constructed, encode_listener, indefinite_length, isStringType, stream_mode
Modifier | Constructor and Description |
---|---|
protected |
BIT_STRING()
Creates a new BIT STRING object.
|
|
BIT_STRING(boolean[] array)
Creates a new BIT STRING from a boolean array.
|
|
BIT_STRING(byte[] array)
Creates a new BIT STRING object where all bits of the byte array are valid.
|
|
BIT_STRING(byte[] array,
int bitsNotValid)
Creates a new BIT STRING object from a byte array and an integer.
|
|
BIT_STRING(java.lang.String binaryString)
Creates a new BIT STRING from a binary string (e.g.
|
Modifier and Type | Method and Description |
---|---|
int |
bitsNotValid()
Returns the number of bits which are not valid.
|
java.lang.Object |
clone()
Returns a clone of this ASN1String.
|
protected void |
decode(int length,
java.io.InputStream is)
Decodes a BIT_STRING value from the given InputStream.
|
protected void |
encode(java.io.OutputStream os)
DER encodes this BIT STRING ASN1Object and writes the result to the given
output stream.
|
java.lang.String |
getBinaryString()
Returns the value of this BIT_STRING as a binary string.
|
java.lang.Object |
getValue()
Returns the value of this BIT_STRING as a byte array.
|
void |
setValue(java.lang.Object object)
Sets the value of this object to value.
|
java.lang.String |
toString()
Returns a string that represents the contents of this BIT STRING
ASN1Object.
|
addComponent, addEncodeListener, countComponents, encodeObject, getAsnType, getComponentAt, indefiniteLength, isA, isConstructed, isStringType, setIndefiniteLength
protected int bits_not_valid
protected byte[] value
null
.protected BIT_STRING()
public BIT_STRING(byte[] array, int bitsNotValid)
The, for instance, bit string '011010001'B can be represented by two content octets of values 0x68 0x80 (hexadecimal). Now, when using these two bytes for supplying the value of a new BIT_STRING object, explicitly the number of unused bits (7) of the last content octet has to be specified:
byte[] value = { (byte) 0x68, (byte) 0x80 }; BIT_STRING bit_string = new BIT_STRING(value, 7);If
bitsNotValid
is -1
, the bits that are not
valid are automatically counted from the end of value.array
- the bits in a byte arraybitsNotValid
- number of bits which are not valid in the last bytejava.lang.IllegalArgumentException
- if bitsNotValid
is out of range (%lt; -1 or >
7)public BIT_STRING(byte[] array)
array
- the byte array containing the bitspublic BIT_STRING(java.lang.String binaryString)
BIT_STRING bit_string = new BIT_STRING("011010001");
binaryString
- the String containing the bitspublic BIT_STRING(boolean[] array)
BIT_STRING bit_string = new BIT_STRING(new boolean[] {false,true,true,false,true false,false,false,true };
array
- the boolean array representing the bit string valuepublic java.lang.Object clone()
clone
in class ASN1Object
public java.lang.Object getValue()
getValue
in class ASN1Object
public void setValue(java.lang.Object object)
BIT_STRING(byte[] array, int bitsNotValid)
constructor for supplying a byte array value and explicitly setting the
number of invalid bits.setValue
in class ASN1Object
object
- the byte array value to be set for this BIT STRING objectpublic int bitsNotValid()
public java.lang.String getBinaryString()
protected void encode(java.io.OutputStream os) throws java.io.IOException
encode
methods of the DerCoder
class for performing the encoding, and the DerCoder internally will call
this encode
method.encode
in class ASN1Object
os
- the output stream to which to write the datajava.io.IOException
- if an I/O error occurs while writing to the streamprotected void decode(int length, java.io.InputStream is) throws java.io.IOException
length
bytes to
be read represent the value (content octets including bitsNotValid
specification) of an ASN.1 object of type BIT_STRING.
This is a protected method and will not be used by an application for
decoding a DER encoded BIT_STRING. An application will call one of the
decode
methods of the DerCoder
class for performing the decoding. The DerCoder then determines the number
of bytes (length
) occupied by the value of this BIT_STRING
object and internally calls this decode
method for actually
reading the value.
decode
in class ASN1Object
length
- the already decoded length, i.e. number of the bytes representing
the value of the BIT_STRING to be decodedis
- the input stream from which the der encoded data is read injava.io.IOException
- if there is a problem with the InputStreampublic java.lang.String toString()
toString
in class ASN1Object
ASN1Object.toString()