iaik.cms
Class CompressedDataOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by iaik.cms.CompressedDataOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class CompressedDataOutputStream
extends java.io.OutputStream

This is an OutputStream implementation of the CMS CompressedData structure (see RFC 3274). It allows creating a compressed object by writing the content to be compressed to this stream.

It only supports implicit (where the content is included in the CompressedData object) CompressedData objects.

This stream version will encode the content of the CompressedData as a constructed OCTET STRING. Each write operation to this stream will result in a OCTET STRING block within this constructed OCTET STRING.

The final call to close() will finish the encoding.

The typical usage of this class looks like this example for creating a CMS CompressedData structure with the compressed content included.

   // the input stream from which to read the data to be compressed
   InputStream dataInputStream = ...
   // the output stream to which to write the compressed data 
   OutputStream resultStream = ...
   
   // the compression algorithm to be used  
   AlgorithmID compressionAlg = (AlgorithmID)CMSAlgorithmID.zlib_compress.clone();
   
   // create CompressedDataOutputStream
   CompressedDataOutputStream compressedData = 
     new CompressedDataOutputStream(resultStream, compressionAlg);
   
   // write in the data to be compressed
   byte[] buffer = new byte[2048];
   int bytesRead;
   while ((bytesRead = dataInputStream.read(buffer)) != -1) {
     compressedData.write(buffer, 0, bytesRead);
   }
   // closing the stream finishes compression and encoding and closes the underlying stream
   compressedData.close();
 

If you want to encapsulate the CompressedData into a ContentInfo you first must wrap a ContentInfoOutputStream around the final output stream (the ContentInfoOutputStream has to write its headers to the stream at first, thus it must be created at the "lowest" level):

   ContentInfoOutputStream contentInfoStream = 
     new ContentInfoOutputStream(ObjectID.cms_compressedData, resultStream);
   // now create CompressedDataOutputStream for the ContentInfoStream:
   CompressedDataOutputStream compressedData = 
     new CompressedDataOutputStream(contentInfoStream, compressionAlg);
       
   // the further proceeding is same as above
   
   // write in the data to be compressed
   byte[] buffer = new byte[2048];
   int bytesRead;
   while ((bytesRead = dataInputStream.read(buffer)) != -1) {
     compressedData.write(buffer, 0, bytesRead);
   }
   // closing the stream finishes compression and encoding and closes the underlying stream
   compressedData.close();
 
Use class CompressedDataStream to read in and parse the encoded CompressedData and decompress the content.

See Also:
ContentInfoOutputStream, CompressedDataStream

Constructor Summary
CompressedDataOutputStream(ObjectID contentType, java.io.OutputStream out, AlgorithmID compressionAlgorithm)
          Creates a new CompressedDataOutputStream object which later writes the complete encoded CompressedData structure to the given output stream (e.g.
CompressedDataOutputStream(java.io.OutputStream out, AlgorithmID compressionAlgorithm)
          Creates a new CompressedDataOutputStream object which later writes the complete encoded CompressedData structure to the given output stream (e.g.
 
Method Summary
 void close()
          Finishes compression and encoding and closes the underlying stream.
 void flush()
          Flushes any internal data and calls flush of the underlying stream.
 SecurityProvider getSecurityProvider()
          Gets the SecurityProvider installed for this CompressedDataOutputStream.
 int getVersion()
          Returns the version syntax number (0).
 void setSecurityProvider(SecurityProvider securityProvider)
          Sets the SecurityProvider for this CompressedDataOutputStream.
 java.lang.String toString()
          Returns a string giving some information about this CompressedDataOutputStream object.
 java.lang.String toString(boolean detailed)
          Returns a string giving some - if requested - detailed information about this CompressedDataOutputStream object.
 void write(byte[] b)
          Processes the given content data to be compressed.
 void write(byte[] b, int off, int len)
          Processes the given content data to be compressed.
 void write(int b)
          Processes the given content byte to be compressed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CompressedDataOutputStream

public CompressedDataOutputStream(java.io.OutputStream out,
                                  AlgorithmID compressionAlgorithm)
Creates a new CompressedDataOutputStream object which later writes the complete encoded CompressedData structure to the given output stream (e.g. write(byte[])). The content type of the inherent content data is set to ObjectID.cms_data.

Parameters:
out - The output stream to which to write the encoded CompressedData structure.
compressionAlgorithm - the compression algorithm to be used for content compression

CompressedDataOutputStream

public CompressedDataOutputStream(ObjectID contentType,
                                  java.io.OutputStream out,
                                  AlgorithmID compressionAlgorithm)
Creates a new CompressedDataOutputStream object which later writes the complete encoded CompressedData structure to the given output stream (e.g. write(byte[])).

Parameters:
contentType - the content type (e.g. ObjectID.cms_data) of the data to be compressed
out - The output stream to which to write the encoded CompressedData structure.
compressionAlgorithm - the compression algorithm to be used for content compression
Method Detail

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Processes the given content data to be compressed. The data is compressed with the compression algorithm used by this CompressedDataOutputStream.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The data to be compressed as byte array.
off - The start offset in the data array b.
len - The number of bytes to write.
Throws:
java.io.IOException - If an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Processes the given content data to be compressed. The data is compressed with the compression algorithm used by this CompressedDataOutputStream.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The data to be compressed as byte array.
Throws:
java.io.IOException - If an I/O error occurs.

write

public void write(int b)
           throws java.io.IOException
Processes the given content byte to be compressed. The data is compressed with the compression algorithm used by this CompressedDataOutputStream.

Note that when repeatedly calling this method to write single data bytes the encoding may consist of many single-byte OCTET STRINGs. Thus it may be more appropriate to use a byte array expcting write method.

Specified by:
write in class java.io.OutputStream
Parameters:
b - The content data byte to be compressed
Throws:
java.io.IOException - If an I/O error occurs.

flush

public void flush()
           throws java.io.IOException
Flushes any internal data and calls flush of the underlying stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - If flushing the stream fails.

close

public void close()
           throws java.io.IOException
Finishes compression and encoding and closes the underlying stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs while writing to the stream

setSecurityProvider

public void setSecurityProvider(SecurityProvider securityProvider)
Sets the SecurityProvider for this CompressedDataOutputStream.

This method allows to explicitly set a SecurityProvider for this CompressedDataOutputStream. If no explicit SecurityProvider is set, the default system wide installed SecurityProvider will be used for the required cryptographic operations.

This class may use the following method of the SecurityProvider, which may be overriden by an application, if required:

Parameters:
securityProvider - the SecurityProvider to be set

getSecurityProvider

public SecurityProvider getSecurityProvider()
Gets the SecurityProvider installed for this CompressedDataOutputStream.

This class uses the following method of the SecurityProvider, which may be overriden by an application, if required:

If no explicit SecurityProvider has been set for this object, the default system wide installed SecurityProvider will be used for the required cryptographic operations. However, this method will return null if it does not have its own SecurityProvider.

Returns:
the SecurityProvider explicitly installed for this object, or null if this object does not have its own SecurityProvider

getVersion

public int getVersion()
Returns the version syntax number (0).

Returns:
the version syntax number

toString

public java.lang.String toString()
Returns a string giving some information about this CompressedDataOutputStream object.

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

toString

public java.lang.String toString(boolean detailed)
Returns a string giving some - if requested - detailed information about this CompressedDataOutputStream object.

Parameters:
detailed - - whether or not to give detailed information
Returns:
the string representation

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