com.ccg.io
Class ObjectOutputStreamReset

java.lang.Object
  extended by com.ccg.io.ObjectOutputStreamReset

public class ObjectOutputStreamReset
extends Object

Enable a ObjectOutputStream for writing a lot of data.

The current design of the ObjectOutputStream does not make it very usable when one needs to write out a large number of different objects. The problem is that ObjectOutputStream maintains a reference to each object it writes. This results in a huge "reference leak" affect (consumes a lot of memory). This affect was discovered when implementing a lightning data server - the server kept running out of memory because references to each lightning event were being contained (the server was unknowning keeping a copy of every lightning event it sent to a client).

To avoid this, this class makes use of the reset method to periodically clear the internal reference table maintained by the ObjectOutputStream to prevent massive consumption of memory.

This class is basically a convience wrapper around the ObjectOutputStream class. To read data back, you can use the standard ObjectInputStream class.

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $
See Also:
writeObject(java.lang.Object)

Constructor Summary
ObjectOutputStreamReset(OutputStream os, int resetCnt)
          Create a object with a specific "reset" count.
 
Method Summary
 void close()
          Closes the stream.
 void flush()
          Flush the stream.
 void writeObject(Object o)
          Write a Serializable object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectOutputStreamReset

public ObjectOutputStreamReset(OutputStream os,
                               int resetCnt)
                        throws IOException
Create a object with a specific "reset" count. This constructor is similar in nature to the default constructor for the ObjectOutputStream class. It does the following:

Parameters:
os - A OutputStream to write objects to.
cnt - A "reset" count. This count indiciates how often the underlying ObjectOutputStream should be reset as objects are written. If you plan on writing large objects, this number should be relatively low, if you plan on writing small objects, set this number higher. As a general rule of thumb, I like to set this number such that the object will be "reset" after 100K of data has been written which is calculated as (100K / "estimated size of average object"). Note, if you set this value to a value less than or equal to 0, you will disable the automatic "reset" capability of this class.
Throws:
{@link - java.io.IOException} If there is a problem constructing the underlying ObjectOutputStream object used by this class.
IOException
Since:
1.0
See Also:
ObjectOutputStreamReset(java.io.OutputStream, int)
Method Detail

writeObject

public void writeObject(Object o)
                 throws IOException
Write a Serializable object. This method first determines if its time to reset the underlying reference table. If so, this table will be cleared (which reduces the efficiency of the ObjectOutputStream class, but prevents it from consuming vast amounts of memory). After making the "reset" check, the object is then written to the underlying ObjectOutputStream object.

Parameters:
o - A Serializable object to write out.
Throws:
{@link - java.io.IOException} If there is a problem writing the object to the underlying stream.
IOException
Since:
1.0

flush

public void flush()
           throws IOException
Flush the stream. Forces any of the written data still in buffers to be written to the output device. You will still be able to write additional objects after invoking this method.

Throws:
{@link - java.io.IOException} If an I/O error has occurred.
IOException
Since:
1.0
See Also:
writeObject(java.lang.Object), close()

close

public void close()
           throws IOException
Closes the stream. Forces any of the written data still in buffers to be written to the output device and then closes the device. You will be unable to write more objects after invoking this method.

Throws:
{@link - java.io.IOException} If an I/O error has occurred.
IOException
Since:
1.0
See Also:
writeObject(java.lang.Object), flush()


Copyright 1998-1998-2006 null. All Rights Reserved.