org.jnetstream.capture
Class PacketOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.jnetstream.capture.PacketOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, CapturePacketOutput<Packet>

public class PacketOutputStream
extends java.io.OutputStream
implements CapturePacketOutput<Packet>, java.io.Closeable, java.io.Flushable

A PacketOutputStream writes CapturePacket objects to an OutputStream. The Packet objects can be read (reconstituted) using an PacketInputStream. Persistent storage of CapturePackets can be accomplished by using a file for a stream. If the stream is a network socket stream, the CapturePackets can be reconstitued on a nother host or in another process.

Here is an example:

 FileOutputStream fos = new FileOutputStream("test.stream");
 PacketOutputStream cos = new PacketOutputStream(fos);
 
 CapturePacket packet = // gotten from live network or file 
     cos.writePacket(packet);
 cos.close();
 
 FileInputStream fis = new FileInputStream("test.stream", "r");
 PacketInputStream cis = new PacketInputStream(fis);
 
 packet = cis.readPacket();
 cis.close();
 

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Field Summary
protected  java.io.ObjectOutputStream out
           
 
Constructor Summary
protected PacketOutputStream()
           Provide a way for subclasses that are completely reimplementing CapturePacketOutputStream to not have to allocate private data just used by this implementation of CaptureOutputOutputStream.
  PacketOutputStream(java.io.OutputStream out)
          Creates a new capture output stream bound to the supplied output stream.
 
Method Summary
 void close()
          Closes the output stream.
 void flush()
          Flushes any buffered changes from this output stream to the underlying stream immediately.
 void write(byte[] b)
          Write the byte array into the underlying outputstream.
 void write(byte[] b, int off, int len)
          Writes the byte array into the underlying outputstream
 void write(int b)
          Writes a single byte to the output stream.
 void writePacket(Packet packet)
          Serializes and writes a packet to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

protected java.io.ObjectOutputStream out
Constructor Detail

PacketOutputStream

protected PacketOutputStream()

Provide a way for subclasses that are completely reimplementing CapturePacketOutputStream to not have to allocate private data just used by this implementation of CaptureOutputOutputStream.


PacketOutputStream

public PacketOutputStream(java.io.OutputStream out)
                   throws java.io.IOException
Creates a new capture output stream bound to the supplied output stream. CapturePackets are serialized and sent as a byte stream accorss the output stream.

Parameters:
out - underlying output stream this object is bound to
Throws:
java.io.IOException - any IO errors during transmittion
Method Detail

close

public void close()
           throws java.io.IOException
Closes the output stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Flushes any buffered changes from this output stream to the underlying stream immediately.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes the byte array into the underlying outputstream

Overrides:
write in class java.io.OutputStream
Parameters:
b - byte array to write
off - offset into the byte array to start reading the data
len - number of bytes to write from the byte array to outputstream
Throws:
java.io.IOException - any IO errors

write

public void write(byte[] b)
           throws java.io.IOException
Write the byte array into the underlying outputstream.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the byte array to write to the outputstream in its entirety
Throws:
java.io.IOException - any IO errors

write

public void write(int b)
           throws java.io.IOException
Writes a single byte to the output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte, in form of an integer, to write to the stream
Throws:
java.io.IOException - any IO errors

writePacket

public void writePacket(Packet packet)
                 throws java.io.IOException
Serializes and writes a packet to the output stream. The packet can be read back by a corresponding PacketInputStream connected to this outputstream.

Specified by:
writePacket in interface CapturePacketOutput<Packet>
Parameters:
packet - the packet to serialize and write out to the outputstream
Throws:
java.io.IOException - any of the usual Input/Output related exceptions
See Also:
com.slytechs.capture.stream.CapturePacketOutput#writePacket(com.slytechs.capture.CapturePacket)