Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

cryptix.openpgp.io
Class PGPLengthDataOutputStream  view PGPLengthDataOutputStream download PGPLengthDataOutputStream.java

java.lang.Object
  extended bycryptix.openpgp.io.PGPDataOutputStream
      extended bycryptix.openpgp.io.PGPLengthDataOutputStream
Direct Known Subclasses:
PGPCipherDataOutputStream, PGPPacketDataOutputStream, PGPSignatureDataOutputStream

public abstract class PGPLengthDataOutputStream
extends PGPDataOutputStream

Generic outputstream to write PGP formatted packet data that contains length information

This class basically has two modes, one where the length of the written data is (partly) known in advance and one when the length is not known. In the first mode all data will be written to the underlying outputstream immediately, which allows for full streaming. In the second mode all output will be buffered until the close method is called, where all data is then written in one big chunk to the outputstream.

If the subclass wants, it can support writing data in chunks. This is particularly useful when in streaming a lot of data, when you do not know in advance what the total length will be. In that case you just buffer the data and write a chunk every time your buffer is full.

A subclass may decide not to support partial chunks.

Version:
$Revision: 1.2 $

Field Summary
private  java.io.ByteArrayOutputStream buffer
          Buffer in case we don't know the length
private  long chunkBytesWritten
          The number of bytes written in the current chunk
private  long chunkLength
          The length of the current chunk if SetLength or setPartialLength has been used.
private  boolean lengthHasBeenWritten
          If true then length info has been written and we can write directly to the outputstream, if false then we have to buffer the info
private  java.io.OutputStream out
          The underlying outputstream
private  boolean partial
          If true then we are currently writing a partial length packet
 
Constructor Summary
PGPLengthDataOutputStream()
          Emtpy constructor for subclasses that handle their own writing
PGPLengthDataOutputStream(java.io.OutputStream out)
          Constructor that takes an outputstream
 
Method Summary
 void close()
          Close this inputstream
 void setLength(long len)
          Set the length of the current packet
 void setPartialLength(long len)
          Set the length of the current packet
protected  void writeDirect(int b)
          Write one byte directly to the underlying outputstream.
protected  void writeInternal(int b)
          Internal method used by all other methods to write one byte.
protected abstract  void writeLength(java.io.OutputStream out, long len)
          Write a length to the outputstream.
protected abstract  void writePartialLength(java.io.OutputStream out, long len)
          Write a partial length to the outputstream.
 
Methods inherited from class cryptix.openpgp.io.PGPDataOutputStream
writeBoolean, writeByte, writeFully, writeFully, writeInt, writeLengthPrependedString, writeLong, writeMPI, writeNullTerminatedString, writeShort, writeString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

private java.io.OutputStream out
The underlying outputstream


buffer

private java.io.ByteArrayOutputStream buffer
Buffer in case we don't know the length


lengthHasBeenWritten

private boolean lengthHasBeenWritten
If true then length info has been written and we can write directly to the outputstream, if false then we have to buffer the info


partial

private boolean partial
If true then we are currently writing a partial length packet


chunkBytesWritten

private long chunkBytesWritten
The number of bytes written in the current chunk


chunkLength

private long chunkLength
The length of the current chunk if SetLength or setPartialLength has been used.

Constructor Detail

PGPLengthDataOutputStream

public PGPLengthDataOutputStream(java.io.OutputStream out)
Constructor that takes an outputstream

Subclasses should call this constructor from their own, after doing their own initialization.


PGPLengthDataOutputStream

public PGPLengthDataOutputStream()
Emtpy constructor for subclasses that handle their own writing

If a subclass decides to use this constructor, then the writeDirect method must be overridden.

Method Detail

writeDirect

protected void writeDirect(int b)
                    throws java.io.IOException
Write one byte directly to the underlying outputstream.


writeInternal

protected void writeInternal(int b)
                      throws java.io.IOException
Internal method used by all other methods to write one byte.

Specified by:
writeInternal in class PGPDataOutputStream

writePartialLength

protected abstract void writePartialLength(java.io.OutputStream out,
                                           long len)
                                    throws java.io.IOException
Write a partial length to the outputstream.

If an implementation does not support partial lengths it should throw a (subclass of) RuntimeException


writeLength

protected abstract void writeLength(java.io.OutputStream out,
                                    long len)
                             throws java.io.IOException
Write a length to the outputstream.

This method will only be called once for every packet. It may be preceded by several writePartialLength calls.


setLength

public void setLength(long len)
               throws java.io.IOException
Set the length of the current packet

This method must only be called once for every packet. It may be preceded by several setPartialLength calls. If this condition is not met an IllegalStateException will be thrown.

If no calls have been made setPartialLength then no bytes must have been written yet, otherwise an IllegalStateException will be thrown.

This method calls writeLength.


setPartialLength

public void setPartialLength(long len)
                      throws java.io.IOException
Set the length of the current packet

If this is the first call to this method then no bytes must have been written yet, otherwise an IllegalStateException will be thrown.

After this method has been called one or more times a setLength must be called for the final chunk. This will be checked in the close method.

This method calls writePartialLength.


close

public void close()
           throws java.io.IOException
Close this inputstream

If setLength and setPartialLength have been used this method checks to see if all bytes of the chunk have been written, otherwise an IllegalStateException will be thrown.

If setLength and setPartialLength have not been used and thus all data has been buffered, the buffered data will be written after a call to writeLength.

This method does not close the underlying outputstream.

This method must always be called.

Specified by:
close in class PGPDataOutputStream