Top Description Fields Constructors Methods
net.sf.ehcache.util

public final Class MemoryEfficientByteArrayOutputStream

extends ByteArrayOutputStream
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .ObjectOutputStream, .Serializable

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.
Author
Greg Luck
Version
$Id$

Field Summary

Modifier and TypeField and Description
private static final int
BEST_GUESS_SIZE

byte[] payloads are not expected to be tiny.

private static int
Inherited from java.io.ByteArrayOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
public
MemoryEfficientByteArrayOutputStream(int
the initial size.
size
)

Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Method Summary

Modifier and TypeMethod and Description
public synchronized byte[]

Returns:

the underlying byte[], or a copy if the byte[] is oversized
getBytes
()

Gets the bytes.

public static MemoryEfficientByteArrayOutputStream

Returns:

a ByteArrayOutputStream with a Serialized object in it
serialize
(Serializable
any Object that implements Serializable
serializable
,
int
how many bytes is expected to be in the Serialized representation
estimatedPayloadSize
)

Factory method

public static MemoryEfficientByteArrayOutputStream

Returns:

a ByteArrayOutputStream with a Serialized object in it
serialize
(Serializable
any Object that implements Serializable
serializable
)

Factory method.

Inherited from java.io.ByteArrayOutputStream:
closeresetsizetoByteArraytoStringtoStringtoStringtoStringwritewritewriteByteswriteTo

Field Detail

BEST_GUESS_SIZEback to summary
private static final int BEST_GUESS_SIZE

byte[] payloads are not expected to be tiny.

lastSizeback to summary
private static int lastSize

Constructor Detail

MemoryEfficientByteArrayOutputStreamback to summary
public MemoryEfficientByteArrayOutputStream(int size)

Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Parameters
size:int

the initial size.

Method Detail

getBytesback to summary
public synchronized byte[] getBytes()

Gets the bytes.

Returns:byte[]

the underlying byte[], or a copy if the byte[] is oversized

serializeback to summary
public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException

Factory method

Parameters
serializable:Serializable

any Object that implements Serializable

estimatedPayloadSize:int

how many bytes is expected to be in the Serialized representation

Returns:MemoryEfficientByteArrayOutputStream

a ByteArrayOutputStream with a Serialized object in it

Exceptions
IOException:
if something goes wrong with the Serialization
serializeback to summary
public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws 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:Serializable

any Object that implements Serializable

Returns:MemoryEfficientByteArrayOutputStream

a ByteArrayOutputStream with a Serialized object in it

Exceptions
IOException:
if something goes wrong with the Serialization