com.voytechs.jnetstream.io
Class CaptureOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.voytechs.jnetstream.io.ProtocolDataOutputStream
          extended by com.voytechs.jnetstream.io.PacketOutputStream
              extended by com.voytechs.jnetstream.io.CaptureOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable
Direct Known Subclasses:
PcapOutputStream, SnoopOutputStream

public abstract class CaptureOutputStream
extends PacketOutputStream

Base abstract class for all Capture file specific *OutputStream objects. This method is provides generic interfaces and algorithms to help support writting of new capture file.

Main methods are write(), any of the write methods defined in ProtocolDataOutputStream to write native java types or the copy method which is used to copy contents from one capture file to another, possibly in a nother capture format completely.

Important note: Its is very important to call close() when done writting data out to the stream. This finalizes the stream and writes out any remaining padding and headers to the stream before closing it. Without an explicit call to close() last record written may be incomplete.


Field Summary
 
Fields inherited from class com.voytechs.jnetstream.io.ProtocolDataOutputStream
bitsLeft, cachedByte, out
 
Constructor Summary
CaptureOutputStream(java.io.OutputStream out)
          Uses the given output stream to write new capture file.
CaptureOutputStream(java.lang.String file)
          Uses the given file to write new capture file.
 
Method Summary
 void close()
          Finish writting data to the output stream.
 void copy(PacketInputStream in)
          Copy from PacketInputStream to another.
protected abstract  void initPacketStream()
          Write out the main capture file header into the capture stream.
static void main(java.lang.String[] args)
          Test function for CaptureOutputStream
 void nextPacket(long captureMillis, int captureNanos, long snaplen, long length)
          Initiializes the capture file header.
 void nextPacket(PacketInputStream in)
          Initiializes the capture file header.
protected abstract  void writeRecordHeader(long captureMillis, int captureNanos, long snaplen, long length)
          Writes the capture file record header.
 
Methods inherited from class com.voytechs.jnetstream.io.PacketOutputStream
write
 
Methods inherited from class com.voytechs.jnetstream.io.ProtocolDataOutputStream
flush, unloadCachedByte, write, write, write, writeByte, writeInt, writeL, writeLByte, writeLInt, writeLLong, writeLong, writeLShort, writeShort, writeUByte, writeUInt, writeULByte, writeULInt, writeULLong, writeULong, writeULShort, writeUShort
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CaptureOutputStream

public CaptureOutputStream(java.io.OutputStream out)
                    throws java.io.IOException,
                           StreamFormatException
Uses the given output stream to write new capture file. No META data is extracted.

Parameters:
out - Output stream to new capture file.
Throws:
java.io.IOException
StreamFormatException

CaptureOutputStream

public CaptureOutputStream(java.lang.String file)
                    throws java.io.FileNotFoundException,
                           java.io.IOException,
                           StreamFormatException
Uses the given file to write new capture file. No META data is extracted.

Parameters:
file -
Throws:
java.io.FileNotFoundException
java.io.IOException
StreamFormatException
Method Detail

initPacketStream

protected abstract void initPacketStream()
                                  throws java.io.IOException,
                                         StreamFormatException
Write out the main capture file header into the capture stream.

Specified by:
initPacketStream in class PacketOutputStream
Throws:
java.io.IOException - Any I/O errors while writting the header.
StreamFormatException

writeRecordHeader

protected abstract void writeRecordHeader(long captureMillis,
                                          int captureNanos,
                                          long snaplen,
                                          long length)
                                   throws java.io.IOException
Writes the capture file record header. This is very format specific write.

Parameters:
captureMillis -
captureNanos -
snaplen -
length -
Throws:
java.io.IOException

nextPacket

public void nextPacket(PacketInputStream in)
                throws java.io.IOException
Initiializes the capture file header. Extracts the required attributes from he InputStream. If the next packet will not be based on the exact contents of the input stream, such that snpalen and packet length attributes are incorrect, do not use this method, but set the expected packet attributes explicitely with the other variation of nextPacket() method.

Parameters:
in - InputStream to extract certain information from about then next packet.
Throws:
java.io.IOException - Any I/O errors.
See Also:
nextPacket(long captureMillis, int captureNanos, long snaplen, long length)

nextPacket

public void nextPacket(long captureMillis,
                       int captureNanos,
                       long snaplen,
                       long length)
                throws java.io.IOException
Initiializes the capture file header. Extracts the required attributes from he InputStream.

Parameters:
captureMillis - Number of milli seconds from 1970 when packet was captured.
captureNanos - Number nano seconds as additional time fragment in the capture timestamp.
snaplen - Number of bytes captured in the packet.
length - Number of bytes in the original packet, may differ from snaplen.
Throws:
java.io.IOException - Any I/O errors.

close

public void close()
           throws java.io.IOException
Finish writting data to the output stream. This allows the stream to finalize, write out any last padding or closing headers.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - Any I/O errors.

copy

public void copy(PacketInputStream in)
          throws java.io.IOException,
                 StreamFormatException
Copy from PacketInputStream to another. This is a low level copy that copies byte for byte the packet data and re-writes the capture file record headers for potentially different format from the source stream.

Specified by:
copy in class PacketOutputStream
Parameters:
in - PacketInputStream to use as source of the copy operation.
Throws:
java.io.IOException - Any I/O errors.
StreamFormatException

main

public static void main(java.lang.String[] args)
Test function for CaptureOutputStream

Parameters:
args - command line arguments