org.openejb.util.io
Class ObjectOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.openejb.util.io.ObjectOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataOutput, java.io.Flushable, java.io.ObjectOutput, java.io.ObjectStreamConstants

public class ObjectOutputStream
extends java.io.OutputStream
implements java.io.ObjectOutput, java.io.ObjectStreamConstants

This is a faster ObjectOutputStream for high volume object serialization.

This ObjectOutputStream's strength is that it can be reused unlike the Sun ObjectOutputStream which needs to be discarded and re-instantiated. This ObjectOutputStream also has the main algorithm inlined. This of coarse looks terrible but is faster then delegating everything to reusable methods. This is implementation is not finished yet as it does not use the writeObject callback method and does not serialize exceptions to the stream as it should.

We chose not to implement the formula to generate the serialVersionID for classes that do not specify one explicitly as this adds a lot of overhead the first time a new class type is introduced into the stream. This will most likely be added as an optional function.

This ObjectOutputStream is not faster in all situations. When doing only a few dozen serializations during the life of the VM you will want to use the java.io.ObjectOutputStream. You'll notice however that this ObjectOutputStream performs considerably faster with a high number of serializations. This makes this implementation ideal for handling the heavy load of typical a server.

Run the SerializationPerformanceTest to get a better idea on how this OutputPerforms on your machine.

 example:
 
 $java org.openejb.test.SerializationPerformanceTest 20 100 10
 

Running the test with the above parameters will typically give results indicating this ObjectOutputStream running in 64% of the time it take the java.io.ObjectOutputSteam to complete, i.e. about 36% faster.

Since:
OpenEJB 1.0
Version:
0.50, 01/11/2000
Author:
David Blevins

Field Summary
 
Fields inherited from interface java.io.ObjectStreamConstants
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING
 
Constructor Summary
ObjectOutputStream(java.io.OutputStream out)
           
 
Method Summary
 void flush()
          Flushes this data output stream.
 void reset()
           
 void serializeObject(java.lang.Object obj)
           
 void serializeObject(java.lang.Object obj, java.io.OutputStream out)
           
 int size()
          Returns the current size of the buffer.
 byte[] toByteArray()
          Creates a newly allocated byte array.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
 void write(int b)
          Writes the specified byte to this byte array output stream.
 void writeBoolean(boolean v)
          Writes a boolean to the underlying output stream as a 1-byte value.
 void writeByte(int v)
          Writes out a byte to the underlying output stream as a 1-byte value.
 void writeBytes(java.lang.String s)
          Writes out the string to the underlying output stream as a sequence of bytes.
 void writeChar(int v)
          Writes a char to the underlying output stream as a 2-byte value, high byte first.
 void writeChars(java.lang.String s)
          Writes a string to the underlying output stream as a sequence of characters.
 void writeDouble(double v)
          Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
 void writeException(java.lang.Throwable th)
           
 void writeFloat(float v)
          Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
 void writeInt(int v)
          Writes an int to the underlying output stream as four bytes, high byte first.
 void writeLong(long v)
          Writes a long to the underlying output stream as eight bytes, high byte first.
 void writeObject(java.lang.Object obj)
           
 void writeReset()
           
 void writeShort(int v)
          Writes a short to the underlying output stream as two bytes, high byte first.
 void writeString(java.lang.String s)
           
 void writeUTF(java.lang.String str)
           
 
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
 
Methods inherited from interface java.io.ObjectOutput
close, write
 

Constructor Detail

ObjectOutputStream

public ObjectOutputStream(java.io.OutputStream out)
                   throws java.io.IOException
Throws:
java.io.IOException
Method Detail

reset

public void reset()
           throws java.io.IOException
Throws:
java.io.IOException

serializeObject

public void serializeObject(java.lang.Object obj,
                            java.io.OutputStream out)
                     throws java.io.NotSerializableException,
                            java.io.IOException
Throws:
java.io.NotSerializableException
java.io.IOException

serializeObject

public void serializeObject(java.lang.Object obj)
                     throws java.io.NotSerializableException,
                            java.io.IOException
Throws:
java.io.NotSerializableException
java.io.IOException

writeObject

public void writeObject(java.lang.Object obj)
                 throws java.io.IOException
Specified by:
writeObject in interface java.io.ObjectOutput
Throws:
java.io.IOException

writeString

public void writeString(java.lang.String s)
                 throws java.io.IOException
Throws:
java.io.IOException

writeException

public void writeException(java.lang.Throwable th)
                    throws java.io.IOException
Throws:
java.io.IOException

writeReset

public void writeReset()
                throws java.io.IOException
Throws:
java.io.IOException

write

public void write(int b)
Writes the specified byte to this byte array output stream.

Specified by:
write in interface java.io.DataOutput
Specified by:
write in interface java.io.ObjectOutput
Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written.

write

public void write(byte[] b,
                  int off,
                  int len)
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

Specified by:
write in interface java.io.DataOutput
Specified by:
write in interface java.io.ObjectOutput
Overrides:
write in class java.io.OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

flush

public void flush()
           throws java.io.IOException
Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

The flush method of DataOuputStream calls the flush method of its underlying output stream.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in interface java.io.ObjectOutput
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.

toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns:
the current contents of this output stream, as a byte array.
See Also:
ByteArrayOutputStream.size()

size

public int size()
Returns the current size of the buffer.

Returns:
the value of the count field, which is the number of valid bytes in this output stream.

writeBoolean

public final void writeBoolean(boolean v)
                        throws java.io.IOException
Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0.

Specified by:
writeBoolean in interface java.io.DataOutput
Parameters:
v - a boolean value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeByte

public final void writeByte(int v)
                     throws java.io.IOException
Writes out a byte to the underlying output stream as a 1-byte value.

Specified by:
writeByte in interface java.io.DataOutput
Parameters:
v - a byte value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeShort

public final void writeShort(int v)
                      throws java.io.IOException
Writes a short to the underlying output stream as two bytes, high byte first.

Specified by:
writeShort in interface java.io.DataOutput
Parameters:
v - a short to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeChar

public final void writeChar(int v)
                     throws java.io.IOException
Writes a char to the underlying output stream as a 2-byte value, high byte first.

Specified by:
writeChar in interface java.io.DataOutput
Parameters:
v - a char value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeInt

public final void writeInt(int v)
                    throws java.io.IOException
Writes an int to the underlying output stream as four bytes, high byte first.

Specified by:
writeInt in interface java.io.DataOutput
Parameters:
v - an int to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeLong

public final void writeLong(long v)
                     throws java.io.IOException
Writes a long to the underlying output stream as eight bytes, high byte first.

Specified by:
writeLong in interface java.io.DataOutput
Parameters:
v - a long to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeFloat

public final void writeFloat(float v)
                      throws java.io.IOException
Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.

Specified by:
writeFloat in interface java.io.DataOutput
Parameters:
v - a float value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeDouble

public final void writeDouble(double v)
                       throws java.io.IOException
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. *

Specified by:
writeDouble in interface java.io.DataOutput
Parameters:
v - a double value to be written.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
Double.doubleToLongBits(double)

writeBytes

public final void writeBytes(java.lang.String s)
                      throws java.io.IOException
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.

Specified by:
writeBytes in interface java.io.DataOutput
Parameters:
s - a string of bytes to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeChars

public final void writeChars(java.lang.String s)
                      throws java.io.IOException
Writes a string to the underlying output stream as a sequence of characters.

Specified by:
writeChars in interface java.io.DataOutput
Parameters:
s - a String value to be written.
Throws:
java.io.IOException - if an I/O error occurs.

writeUTF

public final void writeUTF(java.lang.String str)
                    throws java.io.IOException
Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException


Copyright © 1999-2011 OpenEJB. All Rights Reserved.