com.voytechs.jnetstream.io
Class DupPacketOutputStream

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.DupPacketOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class DupPacketOutputStream
extends PacketOutputStream

Output stream which duplicates capture file format and contents.

InputStream's original capture file headers are duplicated using the META data mechanism to the output stream. Any META headers extracted from source, are copied as is into the output stream. This class is best used for exact copies between the source and destination streams.

If data content change in any way or form, it is upto the user to make sure the capture record headers are appropriately updated to reflect the change. (.i.e. trunkated packet length or CRC needs to be recalculated.)

If you need to modify the contents between a source and a destination stream, the best class to use is specific capture format class. (i.e. PcapOutputStream)

Here is an example how to use the class. Example ommits try{} and catch{} statements:

 RawformatInputStream in = new RawformatInputStream("capture.pcap", "config/captureformats.npl");
 CaptureMetaFormat meta = (CaptureMetaFormat)in; // For explicit demonstration only
 DupPacketOutputStream out = new DupPacketOutputStream("dupfile.pcap", meta);
 

out.copy(in);

To see complete example try running the static main method DupPacketOutputStream.main();


Field Summary
protected  CaptureMetaFormat metaSource
           
 
Fields inherited from class com.voytechs.jnetstream.io.ProtocolDataOutputStream
bitsLeft, cachedByte, out
 
Constructor Summary
DupPacketOutputStream(java.io.OutputStream out, CaptureMetaFormat metaSource)
          Uses the given output stream to write new capture file, using META headers.
DupPacketOutputStream(java.lang.String file, CaptureMetaFormat metaSource)
          Uses the given file to write new capture file.
 
Method Summary
 void copy(PacketInputStream in)
          Copy from PacketInputStream to another.
 void initPacketStream()
          Initialize the PCAP capture file header using META header from META source.
static void main(java.lang.String[] args)
          Test function for DupPacketOutputStream

Defines two (2) methods of running the class.
 void nextPacket()
          Initialize the packet header using META data found.
 
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
close, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

metaSource

protected CaptureMetaFormat metaSource
Constructor Detail

DupPacketOutputStream

public DupPacketOutputStream(java.io.OutputStream out,
                             CaptureMetaFormat metaSource)
                      throws java.io.IOException,
                             StreamFormatException
Uses the given output stream to write new capture file, using META headers. META data is used to extract capture file parameters from the source stream and used to write them out to the outputstream.

Parameters:
out -
metaSource -
Throws:
java.io.IOException
StreamFormatException

DupPacketOutputStream

public DupPacketOutputStream(java.lang.String file,
                             CaptureMetaFormat metaSource)
                      throws java.io.FileNotFoundException,
                             java.io.IOException,
                             StreamFormatException
Uses the given file to write new capture file. With META data. META data is used to extract capture file parameters from the source stream.

Parameters:
file - Filename of the output capture file.
metaSource - Source input stream to extract META data from.
Throws:
java.io.FileNotFoundException - If file can not be opened.
java.io.IOException
StreamFormatException
Method Detail

initPacketStream

public void initPacketStream()
                      throws java.io.IOException,
                             StreamFormatException
Initialize the PCAP capture file header using META header from META source.

Specified by:
initPacketStream in class PacketOutputStream
Throws:
java.io.IOException - Any I/O errors.
StreamFormatException - All problems with META data are reported using this exception.

nextPacket

public void nextPacket()
                throws java.io.IOException,
                       StreamFormatException
Initialize the packet header using META data found.

Throws:
java.io.IOException - Any I/O errors.
StreamFormatException - if META is not available of compatible.

copy

public void copy(PacketInputStream in)
          throws java.io.IOException,
                 StreamFormatException
Copy from PacketInputStream to another.

Specified by:
copy in class PacketOutputStream
Throws:
java.io.IOException - Any I/O errors.
StreamFormatException - Unexpected end of stream occures.

main

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

Defines two (2) methods of running the class.
1) Automatic using the copy method
2) Manual by calling nextPacket() and write() methods.

Parameters:
args - command line arguments