ehcache

net.sf.ehcache.util
Class MemoryEfficientByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public final class MemoryEfficientByteArrayOutputStream
extends java.io.ByteArrayOutputStream

This class is designed to minimise the number of System.arraycopy(); methods required to complete. ByteArrayOutputStream in the JDK is tuned for a wide variety of purposes. This sub-class starts with an initial size which is a closer match for ehcache usage.

Version:
$Id$
Author:
Greg Luck

Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
MemoryEfficientByteArrayOutputStream(int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 byte[] getBytes()
          Gets the bytes.
static MemoryEfficientByteArrayOutputStream serialize(java.io.Serializable serializable)
          Factory method.
static MemoryEfficientByteArrayOutputStream serialize(java.io.Serializable serializable, int estimatedPayloadSize)
          Factory method
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, write, write, writeTo
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MemoryEfficientByteArrayOutputStream

public MemoryEfficientByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Parameters:
size - the initial size.
Method Detail

getBytes

public byte[] getBytes()
Gets the bytes. Not all may be valid. Use only up to getSize()

Returns:
the underlying byte[]

serialize

public static MemoryEfficientByteArrayOutputStream serialize(java.io.Serializable serializable,
                                                             int estimatedPayloadSize)
                                                      throws java.io.IOException
Factory method

Parameters:
serializable - any Object that implements Serializable
estimatedPayloadSize - how many bytes is expected to be in the Serialized representation
Returns:
a ByteArrayOutputStream with a Serialized object in it
Throws:
java.io.IOException - if something goes wrong with the Serialization

serialize

public static MemoryEfficientByteArrayOutputStream serialize(java.io.Serializable serializable)
                                                      throws java.io.IOException
Factory method. This method optimises memory by trying to make a better guess than the Java default of 32 bytes by assuming the starting point for the serialized size will be what it was last time this method was called.

Parameters:
serializable - any Object that implements Serializable
Returns:
a ByteArrayOutputStream with a Serialized object in it
Throws:
java.io.IOException - if something goes wrong with the Serialization

ehcache