com.voytechs.jnetstream.io
Class SnoopOutputStream

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
                  extended by com.voytechs.jnetstream.io.SnoopOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class SnoopOutputStream
extends CaptureOutputStream

Output stream which writes out a capture file in PCAP format. If the InputStream is also a PCAP stream, then all original PCAP header information is copied into the new OutputStream. Otherwise all parameters of the new SnoopOutputStream are created from scratch (i.e. the latest support file format, instead of the original version of PCAP found in InputStream.)

These examples demonstrates how InputStream and OutputStream can be used to copy between files.

First example is a simple copy. No format or file version conversion is done:

 PacketInputStream in = new SnoopInputStream("oldfile.pcap");
 PacketOutputStream out = new SnoopOutputStream("newfile.pcap", in); // get META from 'in'


Field Summary
static int DEFAULT_PAD_VALUE
           
static int DL_CHANNEL
          IBM Channel-to-channel
static int DL_CHARACTER
          Character Synchronous
static int DL_ETHERNET
          Ethernet II.
static int DL_FDDI
          FDDI
static int DL_HDCL
          HDLC
static int DL_IEEE_802dot3
          IEEE 802.3 Ethernet
static int DL_IEEE_802dot4
          IEEE 802.4 Token bus.
static int DL_IEEE_802dot5
          IEEE 802.5 Token ring.
static int DL_IEEE_802dot6
          IEEE 802.6 Metro net.
static int DL_OTHER
          other
protected static java.lang.String FORMAT_NAME
           
static long SNOOP_MAGIC_NUMBER
           
static int SNOOP_RECORD_HEADER_LENGTH
           
 
Fields inherited from class com.voytechs.jnetstream.io.ProtocolDataOutputStream
bitsLeft, cachedByte, out
 
Constructor Summary
SnoopOutputStream(java.io.OutputStream out)
          Uses the given output stream to write new capture file.
SnoopOutputStream(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 finalize()
          Catch the stream closing so we can finalize writting of the remainder padding.
 void initPacketStream()
          Write out the main capture file header into the capture stream.
static void main(java.lang.String[] args)
          Test function for SnoopOutputStream
protected  void writeRecordHeader(long captureSecs, int captureNanos, long snaplen, long originalLength)
          Write out the record header into the capture stream.
 
Methods inherited from class com.voytechs.jnetstream.io.CaptureOutputStream
copy, nextPacket, nextPacket
 
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, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FORMAT_NAME

protected static final java.lang.String FORMAT_NAME
See Also:
Constant Field Values

DEFAULT_PAD_VALUE

public static final int DEFAULT_PAD_VALUE
See Also:
Constant Field Values

SNOOP_RECORD_HEADER_LENGTH

public static final int SNOOP_RECORD_HEADER_LENGTH
See Also:
Constant Field Values

SNOOP_MAGIC_NUMBER

public static final long SNOOP_MAGIC_NUMBER
See Also:
Constant Field Values

DL_IEEE_802dot3

public static final int DL_IEEE_802dot3
IEEE 802.3 Ethernet

See Also:
Constant Field Values

DL_IEEE_802dot4

public static final int DL_IEEE_802dot4
IEEE 802.4 Token bus.

See Also:
Constant Field Values

DL_IEEE_802dot5

public static final int DL_IEEE_802dot5
IEEE 802.5 Token ring.

See Also:
Constant Field Values

DL_IEEE_802dot6

public static final int DL_IEEE_802dot6
IEEE 802.6 Metro net.

See Also:
Constant Field Values

DL_ETHERNET

public static final int DL_ETHERNET
Ethernet II.

See Also:
Constant Field Values

DL_HDCL

public static final int DL_HDCL
HDLC

See Also:
Constant Field Values

DL_CHARACTER

public static final int DL_CHARACTER
Character Synchronous

See Also:
Constant Field Values

DL_CHANNEL

public static final int DL_CHANNEL
IBM Channel-to-channel

See Also:
Constant Field Values

DL_FDDI

public static final int DL_FDDI
FDDI

See Also:
Constant Field Values

DL_OTHER

public static final int DL_OTHER
other

See Also:
Constant Field Values
Constructor Detail

SnoopOutputStream

public SnoopOutputStream(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

SnoopOutputStream

public SnoopOutputStream(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 - Filename of the output capture file.
Throws:
FileNotFound - If file can not be opened.
java.io.FileNotFoundException
java.io.IOException
StreamFormatException
Method Detail

initPacketStream

public void initPacketStream()
                      throws java.io.IOException
Write out the main capture file header into the capture stream.

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

writeRecordHeader

protected void writeRecordHeader(long captureSecs,
                                 int captureNanos,
                                 long snaplen,
                                 long originalLength)
                          throws java.io.IOException
Write out the record header into the capture stream.

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

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 CaptureOutputStream
Throws:
java.io.IOException - Any I/O errors.

finalize

public void finalize()
Catch the stream closing so we can finalize writting of the remainder padding.

One side effect is that since we can not pass IOException from finalize we catch it and throw it away. We silently ignore the IOException. Inorder to make sure and do a clean exit, best that you call finish() on the stream yourself. This way all the data is written out explicently.

Overrides:
finalize in class java.lang.Object
See Also:
#finish()

main

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

Parameters:
args - First argument is a file name of the input capture file. Second argument (optinal)is the output file name.