iaik.cms
Class DataOutputStream

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

public class DataOutputStream
extends java.io.OutputStream

This class represents an output stream based implementation of the CMS content type Data.

The Cryptographic Message Syntax (CMS) ( RFC 5652) specifies the Data content type as base type without any cryptographic enhancements. Please note that in difference to PKCS#7, CMS specifies the Data as arbitrary octet strings, such as ASCII text files; the interpretation is left to the application. From this point of view the CMS type Data itself does not have an ASN.1 respresentation in contrast to its PKCS#7 equivalent where Data is defined as ASN.1 OCTET STRING:

Data ::= OCTET STRING

This class may be used for compatibility to PKCS#7 for allowing to wrap Data objects into ContentInfo(Stream)s. The data that is written (e.g. write(byte[])) to this stream is encoded as indefinte constructed OCTET STRING. Each write operation creates an OCTET STRING block. If you want to encode the OCTET STRING in blocks of equal size, you must feed in the data in blocks of this size. For instance, the following program sample

  // the stream to which to write the OCTET_STRING encoded data
  OutputStream resultStream = ...;
  // create a DataOutputStream
  DataOutputStream dataOut = new DataOutputStream(resultStream);
  byte[] data1 =  { (byte)0x02, (byte)0x01, (byte)0xAB };
  byte[] data2 =  { (byte)0x02, (byte)0x23, (byte)0x7F };
  byte[] data3 =  { (byte)0x01, (byte)0xCA };
  dataOut.write(data1);
  dataOut.write(data2);
  dataOut.write(data3);
  // finish indefinte length encoding and close stream 
  dataOut.close();
 
will give the following encoding:
  0x24 0x80
            0x04 0x02 0x01 0xAB
            0x04 0x02 0x23 0x7F
            0x04 0x01 0xCA
  0x00 0x00
 
To, for instance, encode data read from an input stream in OCTET STRING blocks of size 2048 do something like, e.g.:
  // the input stream from which to read the data to be encoded as OCTET STRING
  InputStream is = ...;
  // the stream to which to write the OCTET_STRING encoded data
  OutputStream resultStream = ...;
  // create a DataOutputStream
  DataOutputStream dataOut = new DataOutputStream(resultStream);
  // the size of each OCTET STRING block
  int blockSize = 2048;
  // copy data
  byte[] buffer = new byte[blockSize];
  int bytesRead;
  while ((bytesRead = is.read(buffer)) != -1) {
    dataOut.write(buffer, 0, bytesRead);
  }
  // finish indefinte length encoding and close stream 
  dataOut.close();
 
This class is not thread-safe. Use a separate DataOutputStream for each data you want to encode as OCTET STRING.

See Also:
ContentInfoOutputStream

Constructor Summary
DataOutputStream(java.io.OutputStream out)
          Creates a new DataOutputStream for writing data encoded as OCTET STRING to the specified output stream.
DataOutputStream(java.io.OutputStream out, boolean passThroughClose)
          Creates a new DataOutputStream for writing data encoded as OCTET STRING to the specified output stream.
 
Method Summary
 void close()
          Closes the OCTET STRING, i.e. write the trailing 0x00,0x00 end-mark of the constructed OCTET STRING.
 void flush()
          Flushes any internal data and calls flush of the underlying stream.
 void write(byte[] b)
          Writes one OCTET STRING block of the constructed OCTET STRING.
 void write(byte[] b, int off, int len)
          Writes one OCTET STRING block of the constructed OCTET STRING.
 void write(int b)
          Writes one OCTET STRING block of the constructed OCTET STRING.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataOutputStream

public DataOutputStream(java.io.OutputStream out)
Creates a new DataOutputStream for writing data encoded as OCTET STRING to the specified output stream. The data that is written (e.g. write(byte[])) to this stream is encoded as indefinte constructed OCTET STRING. Each write operation creates an OCTET STRING block. If you want to encode the OCTET STRING in blocks of equal size, you must feed in the data in blocks of this size.

This stream will pass through close calls.

Parameters:
out - The stream for writing the encoded data.

DataOutputStream

public DataOutputStream(java.io.OutputStream out,
                        boolean passThroughClose)
Creates a new DataOutputStream for writing data encoded as OCTET STRING to the specified output stream. The data that is written (e.g. write(byte[])) to this stream is encoded as indefinte constructed OCTET STRING. Each write operation creates an OCTET STRING block. If you want to encode the OCTET STRING in blocks of equal size, you must feed in the data in blocks of this size.

Parameters:
out - The stream for writing the encoded data.
passThroughClose - if true, calls to close() will be passed through to out.
Method Detail

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes one OCTET STRING block of the constructed OCTET STRING. The block size will be that of the given data.

Overrides:
write in class java.io.OutputStream
Parameters:
b - The data.
off - The start offset in the data.
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
Writes one OCTET STRING block of the constructed OCTET STRING. The block size will be that of the given data.

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

write

public void write(int b)
           throws java.io.IOException
Writes one OCTET STRING block of the constructed OCTET STRING. The block size will be 1.

Specified by:
write in class java.io.OutputStream
Parameters:
b - The data.
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
Closes the OCTET STRING, i.e. write the trailing 0x00,0x00 end-mark of the constructed OCTET STRING.

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

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