iaik.cms
Class ContentInfoOutputStream

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

public class ContentInfoOutputStream
extends java.io.OutputStream

This is an output stream version of a CMS ContentInfo structure. It uses indefinite constructed length encoding (so that the content data length must not be known in advance) and may be used in combination with any of the IAIK-CMS output stream implementations:

Before transmission a CMS object usually is packed into a ContentInfo structure to tell the receiving party the content type of the received content:
 ContentInfo ::= SEQUENCE {
    contentType ContentType,
    content
      [0] EXPLICIT ANY DEFINED BY contentType }
 
ContentType ::= OBJECT IDENTIFIER
When using the output stream implementation of a CMS content type, first a ContentInfoOutputStream has to be wrapped around the output stream to which the CMS object shall be written (the ContentInfoOutputStream has to write its headers to the stream at first, thus it must be created at the "lowest" level). The following example uses a SignedDataOutputStream to create a CMS SignedData object, pack it into a ContentInfo and write it encoded to an output stream:
   // the private key of the signer
   PrivateKey signatureKey = ...
   // the certificates of the signer
   X509Certificate[] certificateChain = ...
   // the input stream from which to read the data to be signed
   InputStream dataInputStream = ...
   // the output stream to which to write the signed data
   OutputStream resultStream = ...
   
   // first wrap a ContentInfoOutputStream around the resulting output stream
   ContentInfoOutputStream contentInfoStream = 
     new ContentInfoOutputStream(ObjectID.cms_signedData, resultStream);
   // create SignedDataOutputStream for the ContentInfoStream:
   SignedDataOutputStream signedData = 
     new SignedDataOutputStream(contentInfoStream, SignedDataOutputStream.IMPLICIT);
       
   // the further proceeding is same as above
   
   // add the certificates
   signedData.addCertificates(certificateChain);
   
   // add a SignerInfo
   X509Certificate signatureCert = certificateChain[0];
   SignerInfo signerInfo = new SignerInfo(
     new IssuerAndSerialNumber(signatureCert), AlgorithmID.sha256, signatureKey);
   // define some attributes
   Attribute[] attributes = { 
     new Attribute(new CMSContentType(ObjectID.cms_data)),
     new Attribute(new SigningTime())
   };
   // set the attributes
   signerInfo.setSignedAttributes(attributes);
   // and add the new signer
   signedData.addSignerInfo(signerInfo);
   
   // write in the data to be signed
   byte[] buffer = new byte[2048];
   int bytesRead;
   while ((bytesRead = dataInputStream.read(buffer)) != -1) {
     signedData.write(buffer, 0, bytesRead);
   }
   // closing the stream add the signer infos and closes the underlying stream
   signedData.close();
 

See Also:
SignedDataOutputStream, EnvelopedDataOutputStream, EncryptedDataOutputStream, DigestedDataOutputStream, AuthenticatedDataOutputStream, CompressedDataOutputStream

Constructor Summary
ContentInfoOutputStream(ObjectID type, java.io.OutputStream out)
          Creates a new ContentInfo output stream object.
 
Method Summary
 void close()
          Finishes the ContentInfo encoding and writes the final EOC bytes.
 void flush()
          Flushes any internal data and calls flush of the underlying stream.
 ObjectID getContentType()
          Returns the content type of this CMS ContentInfo.
 boolean isPassThroughClose()
          Checks whether a call to close() will call close of the underlying output stream
 void setPassThroughClose(boolean passThroughClose)
          Setting this to true will cause close() to call close of the underlying output stream.
 java.lang.String toString()
          Returns a string giving some information about this CMS ContentInfo.
 void write(byte[] b)
          Encodes and writes the given content data to the output stream.
 void write(byte[] b, int off, int len)
          Encodes and writes the given content data to the output stream.
 void write(int b)
          Encodes and writes the given content byte to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ContentInfoOutputStream

public ContentInfoOutputStream(ObjectID type,
                               java.io.OutputStream out)
Creates a new ContentInfo output stream object.

Parameters:
type - the content type of the included content (e.g. ObjectID.cms_signedData).
out - the underlying output stream for writing.
Method Detail

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Encodes and writes the given content data to the output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The data to be written
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
Encodes and writes the given content data to the output stream.

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

write

public void write(int b)
           throws java.io.IOException
Encodes and writes the given content byte to the output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
b - The data byte to be written
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 the ContentInfo encoding and writes the final EOC bytes.

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

isPassThroughClose

public boolean isPassThroughClose()
Checks whether a call to close() will call close of the underlying output stream

Returns:
true if a call to close() will call close of the underlying output stream; false if a call to close() will not close the underlying stream.

setPassThroughClose

public void setPassThroughClose(boolean passThroughClose)
Setting this to true will cause close() to call close of the underlying output stream. If false, a call to close() will not close the underlying stream.

Parameters:
passThroughClose - true to pass through close() calls. false to not pass them through.

getContentType

public ObjectID getContentType()
Returns the content type of this CMS ContentInfo.

Returns:
the content type, as ObjectID

toString

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

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

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

IAIK-CMS 6.0, (c) 2002 IAIK, (c) 2003, 2023 SIC