org.openejb.util.io
Class ObjectOutputStream

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

public class ObjectOutputStream
extends OutputStream
implements ObjectOutput, 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(OutputStream out)
           
 
Method Summary
 void flush()
          Flushes this data output stream.
 void reset()
           
 void serializeObject(Object obj)
           
 void serializeObject(Object obj, 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(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(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(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(Object obj)
           
 void writeReset()
           
 void writeShort(int v)
          Writes a short to the underlying output stream as two bytes, high byte first.
 void writeString(String s)
           
 void writeUTF(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(OutputStream out)
                   throws IOException
Throws:
IOException
Method Detail

reset

public void reset()
           throws IOException
Throws:
IOException

serializeObject

public void serializeObject(Object obj,
                            OutputStream out)
                     throws NotSerializableException,
                            IOException
Throws:
NotSerializableException
IOException

serializeObject

public void serializeObject(Object obj)
                     throws NotSerializableException,
                            IOException
Throws:
NotSerializableException
IOException

writeObject

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

writeString

public void writeString(String s)
                 throws IOException
Throws:
IOException

writeException

public void writeException(Throwable th)
                    throws IOException
Throws:
IOException

writeReset

public void writeReset()
                throws IOException
Throws:
IOException

write

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

Specified by:
write in interface DataOutput
Specified by:
write in interface ObjectOutput
Specified by:
write in class 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 DataOutput
Specified by:
write in interface ObjectOutput
Overrides:
write in class OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

flush

public void flush()
           throws 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 Flushable
Specified by:
flush in interface ObjectOutput
Overrides:
flush in class OutputStream
Throws:
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 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 DataOutput
Parameters:
v - a boolean value to be written.
Throws:
IOException - if an I/O error occurs.

writeByte

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

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

writeShort

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

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

writeChar

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

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

writeInt

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

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

writeLong

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

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

writeFloat

public final void writeFloat(float v)
                      throws 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 DataOutput
Parameters:
v - a float value to be written.
Throws:
IOException - if an I/O error occurs.

writeDouble

public final void writeDouble(double v)
                       throws 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 DataOutput
Parameters:
v - a double value to be written.
Throws:
IOException - if an I/O error occurs.
See Also:
Double.doubleToLongBits(double)

writeBytes

public final void writeBytes(String s)
                      throws 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 DataOutput
Parameters:
s - a string of bytes to be written.
Throws:
IOException - if an I/O error occurs.

writeChars

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

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

writeUTF

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


Copyright © 1999-2012 OpenEJB. All Rights Reserved.