public class ContentInfoOutputStream
extends java.io.OutputStream
SignedDataOutputStream
EnvelopedDataOutputStream
EncryptedDataOutputStream
DigestedDataOutputStream
AuthenticatedDataOutputStream
CompressedDataOutputStream
ContentInfo ::= SEQUENCE { contentType ContentType, content [0] EXPLICIT ANY DEFINED BY contentType }When using the output stream implementation of a CMS content type, first a
ContentType ::= OBJECT IDENTIFIER
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();
Constructor and Description |
---|
ContentInfoOutputStream(iaik.asn1.ObjectID type,
java.io.OutputStream out)
Creates a new ContentInfo output stream object.
|
Modifier and Type | Method and Description |
---|---|
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.
|
iaik.asn1.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)
|
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.
|
public ContentInfoOutputStream(iaik.asn1.ObjectID type, java.io.OutputStream out)
type
- the content type of the included content (e.g. ObjectID.cms_signedData).out
- the underlying output stream for writing.public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data to be writtenoff
- The start offset in the data array b
.len
- The number of bytes to write.java.io.IOException
- If an I/O error occurs.public void write(byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data to be writtenjava.io.IOException
- If an I/O error occurs.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The data byte to be writtenjava.io.IOException
- If an I/O error occurs.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- If flushing the stream fails.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
- if an I/O error occurspublic boolean isPassThroughClose()
close()
will call close
of the
underlying output streampublic void setPassThroughClose(boolean passThroughClose)
true
will cause close()
to call
close
of the underlying output stream. If false
,
a call to close()
will not close the underlying stream.passThroughClose
- true
to pass through close()
calls. false
to not pass them through.public iaik.asn1.ObjectID getContentType()
public java.lang.String toString()
toString
in class java.lang.Object