iaik.cms
Class CompressedData

java.lang.Object
  extended by iaik.cms.CompressedDataStream
      extended by iaik.cms.CompressedData
All Implemented Interfaces:
Content, ContentStream

public class CompressedData
extends CompressedDataStream
implements Content

This class represents the stream-supporting implementation of the CMS content type CompressedData.

The compressed-data content type is identified by the following object identifier:

 id-ct-compressedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
       us(840) rsadsi(113549) pkcs(1) pkcs-9(9) smime(16) ct(1) 9 }
 

which corresponds to the OID string "1.2.840.113549.1.9.16.1.9".

The CompressedData type provides a syntax for compressing/decompressing the content of a CMS message. An algorithm identifier identifies the compression algorithm to be used ( RFC 3274 refers the ZLIB compression algorithm [RFC1950], [RFC1951]) for compressing the encapsulated content:

 CompressedData ::= SEQUENCE {
   version CMSVersion,
   compressionAlgorithm CompressionAlgorithmIdentifier,
   encapContentInfo EncapsulatedContentInfo
 }
 


When creating a CompressedData object an application has to decide whether the compressed content shall be incldued (IMPLICIT mode) into the CompressedData message or shall be transmitted by other means (EXPLICIT mode):

 int mode = CompressedData.IMPLICIT; // include content
 
or
 int mode = CompressedData.EXPLICIT; // do not include content
 
However, in both cases the content data has to be supplied when creating the CompressedData object to perform the compression with some particular compression algorithm, e.g.:
 // transmission mode (EXPLICIT or IMPLICIT)
 int mode = ...; 
 // the content data 
 byte[] content = ...; 
 // compression algorithm
 AlgorithmID compressionAlg = CMSAlgorithmID.zlib_compress;
 // create the CompressedData object:
 CompressedData compressedData = new CompressedData(content, compressionAlg, mode);
 
If the content shall not be included in the CompressedData object (EXPLICIT mode) you may get the compressed content byte calling method getContent) to transmit it by other means, e.g.:
 // in explicit mode get the compressed content to transmit it by other means
 byte[] compressedContent = null;
 if (mode == CompressedData.EXPLICIT) {
   compressedContent = compressedData.getcontent();
 }
 
Finally the CompressedData object has to be prepared for transmission by transforming it into an ASN1Object or immediately DER encoding it. The former is done by calling method toASN1Object, the latter by using method getEncoded method:
 ASN1Object asn1CompressedData = compressedData.toASN1Object();
 
or
 byte[] encoding = compressedData.getEncoded();
 
You alternatively may use a proper writeTo method of the parent CompressedDataStream class for immediately encoding this CompressedData object to an output stream. When using writeTo in implicit mode, you additionally have the possibility of specifying a particular blockSize for forcing an indefinite constructed encoding of the inherent compressed content data bytes, instead of of the default definite primitive encoding, 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 0xCA
 
for encoding the five bytes 0x01 0xAB 0x23 0x7F 0xCA.


When receiving an CompressedData message use the CompressedData(InputStream) constructor for parsing the CompressedData from its DER encoding:

 // the input stream supplying the DER encoded CompressedData
 InputStream encodedStream = ...;
 // parse the CompressedData
 CompressedData compressedData = new CompressedData(encodedStream);
 
If the compressed content has been transmitted by other means (EXPLICIT mode) it now has to be supplied by calling method setContent since it is required for being decompressed:
 if (compressedData.getMode() == AuthenticatedData.EXPLICIT) {
   // in explicit mode explicitly supply the compressed content for being decompressed 
   authenticatedData.setContent(compressedContent);
 }
 
Finally get the (decompressed) content by calling method getContent:
 byte[] content = compressedData.getContent();
 


Field Summary
 
Fields inherited from class iaik.cms.CompressedDataStream
blockSize_, compressionAlgorithm_, contentType, encapContentType_, EXPLICIT, IMPLICIT, inputStream_, version_
 
Constructor Summary
protected CompressedData()
          Default constructor for dynamic object creation.
  CompressedData(byte[] content, AlgorithmID compressionAlgorithm, int mode)
          Creates a new CompressedData object for compressing the given content with the given compression algorithm.
  CompressedData(java.io.InputStream is)
          Creates a CompressedData object from a DER encoded CompressedData object which is read from the given input stream.
  CompressedData(ObjectID contentType, byte[] content, AlgorithmID compressionAlgorithm, int mode)
          Creates a new CompressedData object for compressing the given content with the given compression algorithm.
  CompressedData(ObjectID contentType, byte[] content, AlgorithmID compressionAlgorithm, int mode, SecurityProvider securityProvider)
          Creates a new CompressedData object for compressing the given content with the given compression algorithm.
 
Method Summary
 void decode(ASN1Object obj)
          Decodes the given CompressedData ASN1 object.
 byte[] getContent()
          Gets the compressed (or decompressed) content.
 byte[] getEncoded()
          Returns this CompressedData as DER encoded byte array.
 java.io.InputStream getInputStream()
          Returns an input stream from which the compressed (or decompressed) content can be read.
 void setContent(byte[] content)
          Sets the content to be compressed/decompressed.
 void setInputStream(java.io.InputStream is)
          Sets an input stream suppliyng the content to be compressed/decompressed.
protected  ASN1Object toASN1Object(int blockSize)
          Returns this CompressedData as ASN1Object where a constructed OCTET STRING is used for encoding the content.
 
Methods inherited from class iaik.cms.CompressedDataStream
decode, getBlockSize, getCompressionAlgorithm, getContentType, getEncapsulatedContentType, getMode, getSecurityProvider, getVersion, setBlockSize, setSecurityProvider, toASN1Object, toString, toString, writeTo, writeTo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface iaik.cms.ContentStream
decode, getBlockSize, getContentType, setBlockSize, toASN1Object, toString
 

Constructor Detail

CompressedData

protected CompressedData()
Default constructor for dynamic object creation. Shall be not used by an application. The block size is set to -1 to enforce definite primitive encoding.


CompressedData

public CompressedData(byte[] content,
                      AlgorithmID compressionAlgorithm,
                      int mode)
               throws java.security.NoSuchAlgorithmException,
                      java.io.IOException
Creates a new CompressedData object for compressing the given content with the given compression algorithm. The mode parameter specifies whether to include the compressed data (mode = CompressedData.IMPLICIT) or not include it (mode = CompressedData.EXPLICIT). This constrcutor already performs the content compression.

Parameters:
content - the content data to be compressed
compressionAlgorithm - the compression algorithm to be used
mode - either CompressedData.IMPLICIT for including the data, or CompressedData.EXPLICIT for not including it
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

CompressedData

public CompressedData(ObjectID contentType,
                      byte[] content,
                      AlgorithmID compressionAlgorithm,
                      int mode)
               throws java.security.NoSuchAlgorithmException,
                      java.io.IOException
Creates a new CompressedData object for compressing the given content with the given compression algorithm. The mode parameter specifies whether to include the compressed data (mode = CompressedData.IMPLICIT) or not include it (mode = CompressedData.EXPLICIT). This constrcutor already performs the content compression.

Parameters:
contentType - the content type of the data to be compressed
content - the content data to be compressed
compressionAlgorithm - the compression algorithm to be used
mode - either CompressedData.IMPLICIT for including the data, or CompressedData.EXPLICIT for not including it
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

CompressedData

public CompressedData(ObjectID contentType,
                      byte[] content,
                      AlgorithmID compressionAlgorithm,
                      int mode,
                      SecurityProvider securityProvider)
               throws java.security.NoSuchAlgorithmException,
                      java.io.IOException
Creates a new CompressedData object for compressing the given content with the given compression algorithm. The mode parameter specifies whether to include the compressed data (mode = CompressedData.IMPLICIT) or not include it (mode = CompressedData.EXPLICIT). This constrcutor already performs the content compression.

Parameters:
contentType - the content type of the data to be compressed
content - the content data to be compressed
compressionAlgorithm - the compression algorithm to be used
mode - either CompressedData.IMPLICIT for including the data, or CompressedData.EXPLICIT for not including it
securityProvider - the SecurityProvider to be used for data compression
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

CompressedData

public CompressedData(java.io.InputStream is)
               throws java.io.IOException,
                      CMSParsingException
Creates a CompressedData object from a DER encoded CompressedData object which is read from the given input stream. The encoded CompressedData may (or may not) be wrapped into a ContentInfo.

The given input stream supplies the DER encoding of an already existing CMS CompressedData object.

Parameters:
is - the InputStream holding the DER encoded CMS CompressedData object
Throws:
java.io.IOException - if an error occurs when reading from the stream
CMSParsingException - if the object can not be parsed
Method Detail

decode

public void decode(ASN1Object obj)
            throws CMSParsingException
Decodes the given CompressedData ASN1 object. The ASN.1 CompressedData may (or may not) be wrapped into a ContentInfo.

Specified by:
decode in interface Content
Parameters:
obj - the ASN1Object the ASN.1 CompressedData object
Throws:
CMSParsingException - if an error occurs when parsing the given ASN1Object

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.security.NoSuchAlgorithmException,
                                          java.io.IOException
Returns an input stream from which the compressed (or decompressed) content can be read.

When having created a new CompressedData object this method returns the compressed content.
When parsing an existing CompressedData from its ASN.1 or DER representation, this method returns the decompressed content.

Overrides:
getInputStream in class CompressedDataStream
Returns:
an input stream with the compressed/decompressed content
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

getContent

public byte[] getContent()
                  throws java.security.NoSuchAlgorithmException,
                         java.io.IOException
Gets the compressed (or decompressed) content.

When having created a new CompressedData object this method returns the compressed content.
When parsing an existing CompressedData from its ASN.1 or DER representation, this method returns the decompressed content.

Returns:
the compressed/decompressed content
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

setContent

public void setContent(byte[] content)
                throws java.security.NoSuchAlgorithmException,
                       java.io.IOException
Sets the content to be compressed/decompressed.

When having created a new CompressedData object and setting the content now this method will the compress the content.
When parsing an existing CompressedData from its ASN.1 or DER representation and setting the compressed content now, this method will decompress the compressed content.

Parameters:
content - the raw/compressed content
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

setInputStream

public void setInputStream(java.io.InputStream is)
                    throws java.security.NoSuchAlgorithmException,
                           java.io.IOException
Sets an input stream suppliyng the content to be compressed/decompressed.

When having created a new CompressedData object and setting the content now this method will the compress the content.
When parsing an existing CompressedData from its ASN.1 or DER representation and setting the compressed content now, this method will decompress the compressed content.

Overrides:
setInputStream in class CompressedDataStream
Parameters:
is - the raw/compressed content supplied from an input stream
Throws:
java.security.NoSuchAlgorithmException - if the requested compression algorithm is not supported
java.io.IOException - if an I/O error occurs

toASN1Object

protected ASN1Object toASN1Object(int blockSize)
                           throws CMSException
Returns this CompressedData as ASN1Object where a constructed OCTET STRING is used for encoding the content.

Overrides:
toASN1Object in class CompressedDataStream
Parameters:
blockSize - the block size defining the encoding scheme - and specifying the length of each primitive encoded octet string component, if positive
Throws:
CMSException - if the ASN1Object cannot be created

getEncoded

public byte[] getEncoded()
                  throws CMSException
Returns this CompressedData as DER encoded byte array. If for the inherent content structure a positive blocksize has been specified, the content is encoded as indefinite constructed octet string.

Returns:
a byte array holding the DER encoding of this CompressedData
Throws:
CMSException - if an error occurs during the encoding procedure

This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note).

IAIK-CMS 5.1, (c) 2002 IAIK, (c) 2003 - 2010 SIC