public class OCTET_STRING extends ConstructedType
An OCTET STRING object often is BER encoded using the constructed indefinite encoding method by splitting the value into separately definite primitive encoded OCTET STRING components, which may be preferable when dealing with large amounts of data and/or intending to be compatible to the encoding practice of some particular application (for instance some versions of Netscape Navigator).e.g:
 0x24 0x80
           0x04 0x02 0x01 0xAB
           0x04 0x02 0x23 0x7F
           0x04 0x01 0xCA
 0x00 0x00
 
 
 instead of:
 
 0x04 0x05 0x01 0xAB 0x23 0x7F 0xCAfor encoding the five data bytes
0x01 0xAB 0x23 0x7F 0xCA.
 
 Therefore the OCTET STRING class is extended to be a subclass of the
 ConstructedType class. Each component can be added to the
 current OCTET STRING object by using the addComponent method. However, when using the addComponent
 method to build nested octet string structures, the data shall be small
 enough to be handled within the memory. For properly handling large amounts
 of data by splitting the encoding as above, use the
 OCTET_STRING(InputStream is, int blockSize) constructor for
 defining the length of each definite primitive encoded octet string
 component. Note that indefinite constructed encoding only will work when
 performing the encoding with the
 encodeTo method
 of the DerCoder class:
 
 
//the raw data supplying input stream: InputStream data = ...; int blockSize = ...; OCTET_STRING oct = new OCTET_STRING(data, blockSize); OutputStream encoded_stream = ...; DerCoder.encodeTo(oct, encoded_stream);In this way, the general proceeding for using the indefinite constructed method to encode large-data carrying octet strings can be summarized as follows:
0x24 0x80
 0x00 0x00
 This procedure makes it possible to limit the data volumes actually processed within the memory to a reasonable small size!
ASN1Object, 
ASN, 
ConstructedType, 
DerCodercontent_count, content_dataasnType, constructed, encode_listener, indefinite_length, isStringType, stream_mode| Constructor and Description | 
|---|
OCTET_STRING()
Creates an empty OCTET_STRING. 
 | 
OCTET_STRING(byte[] array)
Creates a primitive OCTET_STRING object for the given byte value. 
 | 
OCTET_STRING(byte[] array,
            int blockSize)
Creates a constructed OCTET STRING which reads the content data from a byte
 array and splits the data into blocks of  
blockSize bytes. | 
OCTET_STRING(java.io.InputStream in)
Creates a primitive OCTET STRING which reads the content data from an
 InputStream. 
 | 
OCTET_STRING(java.io.InputStream in,
            int blockSize)
Creates a constructed OCTET STRING which reads the content data from an
 InputStream and splits the data into blocks of  
blockSize
 bytes. | 
| Modifier and Type | Method and Description | 
|---|---|
void | 
addComponent(ASN1Object component,
            int index)
Inserts a new OCTET_STRING component to a given location within this
 OCTET_STRING. 
 | 
java.lang.Object | 
clone()
Returns a clone of this OCTET_STRING. 
 | 
protected void | 
decode(int length,
      java.io.InputStream is)
Decodes an OCTET_STRING value from the given InputStream. 
 | 
protected void | 
encode(java.io.OutputStream os)
DER encodes this OCTET_STRING ASN1Object and writes the result to the given
 output stream. 
 | 
void | 
encodeAsIndefiniteConstructedOctetString(java.io.OutputStream os,
                                        int blocksize)
Writes this OCTET_STRING as indefinite constructed encoded octet string to
 the given output stream. 
 | 
int | 
getBlockSize()
Returns the blockSize defining the encoding scheme - and specifying the
 length of each primitive encoded octet string component, if positive. 
 | 
OCTET_STRING | 
getSimpleOctetString()
Returns this OCTET_STRING as simple OCTET_STRING. 
 | 
java.lang.Object | 
getValue()
Returns the value of this OCTET_STRING as byte array. 
 | 
byte[] | 
getWholeValue()
Returns the whole value of this OCTET_STRING as a byte array. 
 | 
void | 
setIndefiniteLength(boolean indefiniteLength)
Switches indefinite length encoding on or off for this OCTET_STRING. 
 | 
void | 
setValue(java.lang.Object object)
Sets the value (or input stream) of this object to value. 
 | 
java.lang.String | 
toString()
Returns a string that represents the contents of this OCTET STRING
 ASN1Object. 
 | 
java.lang.String | 
toString(boolean allBytes)
Returns a string that represents the contents of this OCTET STRING
 ASN1Object. 
 | 
void | 
writeWholeValueToStream(java.io.OutputStream os)
Writes the value of this OCTET_STRING to the given output stream. 
 | 
addComponent, addEncodeListener, countComponents, getComponentAt, getComponents, removeComponent, removeComponent, setComponentaddEncodeListener, encodeObject, getAsnType, indefiniteLength, isA, isConstructed, isStringTypepublic OCTET_STRING()
setValue method or by using the
 addComponent
 method.
 
 When using the setValue method, the data has to be supplied as
 byte array or input stream. The resulting octet string will be recognized
 as primitive octet string.
 
 When using the addComponent method for building a
 constructed octet string, any supplied component has to be an
 OCTET_STRING instance itself.
public OCTET_STRING(byte[] array)
array - the byte value this OCTET STRING object should be representpublic OCTET_STRING(byte[] array,
                    int blockSize)
blockSize bytes.
 Example: OCTET_STRING(content, 2) with {1,2,3,4,5} as content of the will result in the following BER encoding:
24 constructed 80 indefinite length encoding 04 02 01 02 first OCTET STRING with length 2 04 02 03 04 second OCTET STRING with length 2 04 01 05 third OCTET STRING with length 1 00 00 end of indefinite length encoding
array - the byte array supplying the raw datablockSize - the block size defining the encoding scheme - and specifying the
          length of each primitive encoded octet string component, if
          positivepublic OCTET_STRING(java.io.InputStream in)
DerCoder class. The whole data available from
 the InputStream will be encoded as definite primitive OCTET_STRING:
 
 0x04 <length> <data>
in - the input stream supplying the raw datapublic OCTET_STRING(java.io.InputStream in,
                    int blockSize)
blockSize
 bytes.
 
 The data is read from the InputStream not before actually performing the
 encoding by calling one of the corresponding methods of the
 DerCoder class. This constructor shall be used
 for dealing with big amounts of data.
 Example: OCTET_STRING(in, 2) with {1,2,3,4,5} as content of the InputStream will result in the following BER encoding:
24 constructed 80 indefinite length encoding 04 02 01:02 first OCTET STRING with length 2 04 02 03:04 second OCTET STRING with length 2 04 01 05 third OCTET STRING with length 1 00 00 end of indefinite length encodingThe splitting into blockSize blocks only works if
DerCoder#encodeTo(ASN1Object, OutputStream) is used to encode the octet
 string. If DerCoder.encode(ASN1Object) is used, the whole OCTET_STRING will be
 encoded as definite primitive octet string, which alternatively may be done
 when no blocksize has been specified by using the
 OCTET_STRING(InputStream) constructor.in - the input stream supplying the raw datablockSize - the block size defining the encoding scheme - and specifying the
          length of each primitive encoded octet string component, if
          positivepublic java.lang.Object clone()
clone in class ConstructedTypepublic java.lang.Object getValue()
 If this OCTET_STRING is constructed, this method recursively steps through
 all included OCTET_STRING components and concatenates all participated
 values; use writeWholeValueToStream for expected large objects!
getValue in class ConstructedTypepublic byte[] getWholeValue()
                     throws java.io.IOException
writeWholeValueToStream
 for expected large objects!java.io.IOException - if any of the participated input streams already has been
              read, or an IO Error occurs while stream reading/writingpublic void writeWholeValueToStream(java.io.OutputStream os)
                             throws java.io.IOException
os - the output stream to which to write the value of this OCTET_STRINGjava.io.IOException - if any of the participated input streams already has been
              read, or an IO Error occurs while stream reading/writingpublic void encodeAsIndefiniteConstructedOctetString(java.io.OutputStream os,
                                                     int blocksize)
                                              throws java.io.IOException
If this OCTET_STRING is constructed, this method recursively steps through all included OCTET_STRING components and concatenates all participated values. Any sub octet string may contribute to the final value either by supplying its sub-value immediately or by reading from its sub-inputstream.
This method encodes the data in chunks of definite encoded octet strings of the given blocksize:
 24 80
       04  
       04  
       ...
 00 00
   
 
 If this octet string has been created by means of the
 OCTET_STRING(InputStream in, int
 blockSize) constructor, this method will take the block size specified
 there if no blocksize is specified by this method. If a blocksize
 explicitly is specified, the encoding will be splitted according to this
 blocksize, regardless of the blocksize that has been specified in the
 constructor. If no blocksize is specified and no inherent blocksize is set,
 a default blocksize of 1024 is taken.
 If the number of data bytes represented by this octet sring is shorter than the defined blocksize, the resulting structured encoding only will contain one primitive octet string component containing all data bytes, e.g. (supposing that the octet string holds five data bytes, and the specified blocksize is 1024):
 24 80
       04 05 13 07 01 AB 47
 00 00
 
 
 Note that this method also will encode a simple octet string by splitting
 it according to the specified block size. For that reason it is recommended
 to use this method only for simplifying the encoding of a deeply nested
 octet string structure, or for properly handling big data quantities
 carrying sub-components. Simple octet strings and constructed octet strings
 created with the block size specifying constructor shall be encoded by the
 encoding methods of the DerCoder class. The
 DerCoder class also has to be used when the structure of the actual octet
 string exactly (1:1) has to be rebuilt within the encoding - which
 sometimes unfortunately may exceed the memory capacity.
 
Be careful and do not call this method more than once if the data of this octet string is supplied by an input stream (or if this octet string does contain input stream based components) since a stream can only be rea once.
os - the output stream to which to write the encodingblocksize - the size of the definite encoded data blocks; if value <= 0 is
          specified, the blocksize is set to 1024java.io.IOException - if an I/O Error occurs while stream reading/writingpublic void setValue(java.lang.Object object)
 This method is inherited from the ConstructedType class but
 has a somewhat different meaning for the OCTET_STRING class. Whereas in
 ConstructedType the given object is expected to be an array of
 ASN1Objects for setting the components of the ConstructedType object, now
 the object is expected to be a byte array or an InputStream supplying the
 raw data for a simple OCTET_STRING object. Components for a
 constructed OCTET_STRING only may be supplied by means of the
 addComponent methods.
 
Generally there should be no need for using this method since data immediately can be supplied when creating a new simple OCTET_STRING object by means of a proper constructor. However, when actually using this method, be aware about the following issues:
setValue in class ConstructedTypeobject - the new value for this OCTET_STRING, as byte array or input streampublic void setIndefiniteLength(boolean indefiniteLength)
true and cannot be changed by this
 method.
 
 If this is a primitive octet string, indefinite length encoding always will
 be set to false and cannot be changed by this method.
setIndefiniteLength in class ASN1ObjectindefiniteLength - true, if this OCTET_STRING shall be encoded using
          indefinite length, false if shall be encoded as
          definite lengthpublic void addComponent(ASN1Object component, int index)
addComponent in class ConstructedTypecomponent - the component (OCTET_STRING) to insertindex - where to insert the new componentjava.lang.ArrayIndexOutOfBoundsException - If the index is invalidjava.lang.IllegalArgumentException - if the component to be added is no instance of OCTET_STRING,
              or if this OCTET_STRING contains a not-null value or data
              carrying input_streamprotected 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 ConstructedTypeos - the output stream to which to write the encodingjava.io.IOException - if an error occurs during writing to the streamprotected void decode(int length,
                      java.io.InputStream is)
               throws java.io.IOException,
                      CodingException
length bytes to
 be read represent the value of an ASN.1 object of type OCTET_STRING.
 
 This method only parses the value from the stream, if the supplied DER
 encoding represents a primitive octet string; if it represents a
 constructed octet string, the decoding task is passed to the corresponding
 method of the ConstructedType super class.
 
 This is a protected method and will not be used by an application for
 decoding a DER encoded OCTET_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 (primitive)
 OCTET_STRING object and internally calls this decode method
 for actually reading the value.
decode in class ConstructedTypelength - the already decoded length, i.e. number of the bytes representing
          the value of the (primitive) OCTET_STRING to be decoded;is - the input stream from which the DER encoded data is read injava.io.IOException - if there is a problem with the InputStreamCodingException - if an decoding error occurs; e.g. a simple octet string has
              been indefinite encodedpublic OCTET_STRING getSimpleOctetString() throws java.io.IOException
For instance: The constructed OCTET_STRING below would be transformed by this method as follows (DER encoding, hexadecimal output):
 24 80 -- constructed, indefinite length
       04 03 01 23 AB
       04 05 0C 34 05 06 07
       04 02 45 01
       04 01 12
 00 00 -- end of constructed, indefinite
 
 
 After calling getSimpleOctetString the new OCTET_STRING would
 look like:
 
 04 0B 01 23 AB 0C 34 05 06 07 45 01 12(Note that hexadecimal
0B is decimal 11, the number of octets
 in the content field of the resulting OCTET_STRING.)
 
 Since this method builds up the final simple octet string within the
 memory, use
 encodeAsIndefiniteConstructedOctetString for immediately indefinite
 encoding this octet string to a stream.
java.io.IOException - if any of the participated input streams already has been
              read, or an IO Error occurs while reading from a streampublic int getBlockSize()
public java.lang.String toString()
toString in class ASN1ObjectASN1Object.toString()public java.lang.String toString(boolean allBytes)
allBytes - whether to print all bytes of the octet string, or only the first
          5 bytesASN1Object.toString()