org.jboss.remoting.transport.multiplex.utility
Class ShrinkableByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by org.jboss.remoting.transport.multiplex.utility.ShrinkableByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ShrinkableByteArrayOutputStream
extends java.io.ByteArrayOutputStream

ShrinkableByteArrayOutputStream extends java.io.ByteArrayOutputStream and adds the following features:

  1. Rather than creating a new byte array with each call to toByteArray(), it returns a reference to its internal byte array. start() returns the position of the next available byte and available() returns the number of bytes of content, starting at start(), are available.
  2. It reuses its capacity, treating its byte array as a circular queue. When write() is called, if there is too little space at the end of the buffer, and less than half of the capacity is currently in use, it will shift the current contents of its buffer to position 0.
  3. When toByteArray() is called, if less than a quarter of the current capacity is in use and the current capacity is greater than MIN_LENGTH (currently defined as 1024), it will copy the current contents to a byte array one half the size of the current byte array.

Version:
$Revision: 1248 $

Copyright (c) 2005

Author:
Ron Sigal

Field Summary
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
ShrinkableByteArrayOutputStream()
          Create a new ShrinkableByteArrayOutputStream.
ShrinkableByteArrayOutputStream(int size)
          Create a new ShrinkableByteArrayOutputStream.
 
Method Summary
 int available()
          Returns number of bytes of content which can be retrieved.
 int bytesReturned()
          Returns number of bytes of content returned by last call to toByteArray().
protected  int getCount()
           
protected  int getLength()
           
protected  int getNextUsed()
           
protected  int getUsed()
           
 int start()
          Returns position of next available byte of content in byte array returned by toByteArray().
 byte[] toByteArray(int length)
          Returns reference to internal 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.
 
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
 

Constructor Detail

ShrinkableByteArrayOutputStream

public ShrinkableByteArrayOutputStream()
Create a new ShrinkableByteArrayOutputStream.


ShrinkableByteArrayOutputStream

public ShrinkableByteArrayOutputStream(int size)
Create a new ShrinkableByteArrayOutputStream.

Method Detail

available

public int available()
Returns number of bytes of content which can be retrieved.

Returns:
number of bytes of content which can be retrieved

bytesReturned

public int bytesReturned()
Returns number of bytes of content returned by last call to toByteArray().

Returns:
number of bytes of content returned by last call to toByteArray()

start

public int start()
Returns position of next available byte of content in byte array returned by toByteArray().

Returns:
position of next available byte of content in byte array returned by toByteArray()

toByteArray

public byte[] toByteArray(int length)
Returns reference to internal byte array.

Parameters:
length - number of bytes desired
Returns:
reference to internal byte array

write

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

Overrides:
write in class java.io.ByteArrayOutputStream
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.

Overrides:
write in class java.io.ByteArrayOutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

getCount

protected int getCount()

getLength

protected int getLength()

getNextUsed

protected int getNextUsed()

getUsed

protected int getUsed()


Copyright © 1998-2005 JBoss Inc . All Rights Reserved.