org.jboss.cache.util
Class ExposedByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by org.jboss.cache.util.ExposedByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ExposedByteArrayOutputStream
extends java.io.ByteArrayOutputStream

Extends ByteArrayOutputStream, but exposes the internal buffer. Using this, callers don't need to call toByteArray() which copies the internal buffer.

Also overrides the superclass' behavior of always doubling the size of the internal buffer any time more capacity is needed. This class doubles the size until the internal buffer reaches a configurable max size (default is 4MB), after which it begins growing the buffer in 25% increments. This is intended to help prevent an OutOfMemoryError during a resize of a large buffer.

A version of this class was originally created by Bela Ban as part of the JGroups library.

Version:
$Id$
Author:
Brian Stansberry

Field Summary
static int DEFAULT_DOUBLING_SIZE
          Default buffer size after which if more buffer capacity is needed the buffer will grow by 25% rather than 100%
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
ExposedByteArrayOutputStream()
           
ExposedByteArrayOutputStream(int size)
           
ExposedByteArrayOutputStream(int size, int maxDoublingSize)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 int getMaxDoublingSize()
          Gets the highest internal buffer size after which if more capacity is needed the buffer will grow in 25% increments rather than 100%.
 int getNewBufferSize(int curSize, int minNewSize)
          Gets the number of bytes to which the internal buffer should be resized.
 byte[] getRawBuffer()
          Gets the internal buffer array.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, 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
 

Field Detail

DEFAULT_DOUBLING_SIZE

public static final int DEFAULT_DOUBLING_SIZE
Default buffer size after which if more buffer capacity is needed the buffer will grow by 25% rather than 100%

See Also:
Constant Field Values
Constructor Detail

ExposedByteArrayOutputStream

public ExposedByteArrayOutputStream()

ExposedByteArrayOutputStream

public ExposedByteArrayOutputStream(int size)

ExposedByteArrayOutputStream

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

Parameters:
size - the initial size.
maxDoublingSize - the buffer size, after which if more capacity is needed the buffer will grow by 25% rather than 100%
Throws:
java.lang.IllegalArgumentException - if size is negative.
Method Detail

getRawBuffer

public byte[] getRawBuffer()
Gets the internal buffer array. Note that the length of this array will almost certainly be longer than the data written to it; call size() to get the number of bytes of actual data.


write

public void write(byte[] b,
                  int off,
                  int len)
Overrides:
write in class java.io.ByteArrayOutputStream

write

public void write(int b)
Overrides:
write in class java.io.ByteArrayOutputStream

getMaxDoublingSize

public int getMaxDoublingSize()
Gets the highest internal buffer size after which if more capacity is needed the buffer will grow in 25% increments rather than 100%.


getNewBufferSize

public int getNewBufferSize(int curSize,
                            int minNewSize)
Gets the number of bytes to which the internal buffer should be resized.

Parameters:
curSize - the current number of bytes
minNewSize - the minimum number of bytes required
Returns:
the size to which the internal buffer should be resized


Copyright 1998-2005 JBoss Inc . All Rights Reserved.