org.mortbay.util

Class ByteBufferOutputStream

Known Direct Subclasses:
BufferedOutputStream

public class ByteBufferOutputStream
extends OutputStream

ByteBuffer OutputStream. This stream is similar to the java.io.ByteArrayOutputStream, except that it maintains a reserve of bytes at the start of the buffer and allows efficient prepending of data.
Version:
$Revision: 1.16 $
Author:
Greg Wilkins (gregw)

Field Summary

protected byte[]
_buf

Constructor Summary

ByteBufferOutputStream()
Constructor.
ByteBufferOutputStream(int capacity)
Constructor.
ByteBufferOutputStream(int capacity, int preReserve)
Constructor.
ByteBufferOutputStream(int bufferSize, int preReserve, int postReserve)
Constructor.

Method Summary

int
bufferSize()
int
capacity()
void
close()
void
destroy()
void
ensureCapacity(int n)
void
ensureReserve(int n)
void
ensureSize(int bufSize)
void
ensureSize(int bufSize, int pre, int post)
void
ensureSpareCapacity(int n)
void
flush()
boolean
isFixed()
int
postReserve()
void
postwrite(byte[] b, int offset, int length)
Write bytes into the postreserve.
int
preReserve()
void
prewrite(byte[] b)
Write byte array to start of the buffer.
void
prewrite(byte[] b, int offset, int length)
Write byte range to start of the buffer.
void
prewrite(int b)
Write byte to start of the buffer.
void
reset(int reserve)
void
resetStream()
void
setFixed(boolean fixed)
int
size()
int
spareCapacity()
void
write(byte[] b)
void
write(byte[] b, int offset, int length)
void
write(int b)
void
writeTo(OutputStream out)

Field Details

_buf

protected byte[] _buf

Constructor Details

ByteBufferOutputStream

public ByteBufferOutputStream()
Constructor.

ByteBufferOutputStream

public ByteBufferOutputStream(int capacity)
Constructor.
Parameters:
capacity - Buffer capacity

ByteBufferOutputStream

public ByteBufferOutputStream(int capacity,
                              int preReserve)
Constructor.
Parameters:
capacity - Buffer capacity.
preReserve - The reserve of byte for prepending

ByteBufferOutputStream

public ByteBufferOutputStream(int bufferSize,
                              int preReserve,
                              int postReserve)
Constructor.
Parameters:
bufferSize - The size of the buffer == capacity+preReserve+postReserve
preReserve - The reserve of byte for prepending
postReserve - The reserve of byte for appending

Method Details

bufferSize

public int bufferSize()
Returns:
The size of the buffer.

capacity

public int capacity()
Returns:
The capacity of the buffer excluding pre and post reserves.

close

public void close()
            throws IOException

destroy

public void destroy()

ensureCapacity

public void ensureCapacity(int n)
            throws IOException

ensureReserve

public void ensureReserve(int n)

ensureSize

public void ensureSize(int bufSize)
            throws IOException

ensureSize

public void ensureSize(int bufSize,
                       int pre,
                       int post)
            throws IOException

ensureSpareCapacity

public void ensureSpareCapacity(int n)
            throws IOException

flush

public void flush()
            throws IOException

isFixed

public boolean isFixed()
Returns:
True if the buffer cannot be expanded

postReserve

public int postReserve()
Returns:
The current post reserve.

postwrite

public void postwrite(byte[] b,
                      int offset,
                      int length)
            throws IOException
Write bytes into the postreserve. The capacity is not checked.
Parameters:
b -
offset -
length -

preReserve

public int preReserve()
Returns:
The current pre reserve.

prewrite

public void prewrite(byte[] b)
Write byte array to start of the buffer.
Parameters:
b -

prewrite

public void prewrite(byte[] b,
                     int offset,
                     int length)
Write byte range to start of the buffer.
Parameters:
b -
offset -
length -

prewrite

public void prewrite(int b)
Write byte to start of the buffer.
Parameters:
b -

reset

public void reset(int reserve)

resetStream

public void resetStream()

setFixed

public void setFixed(boolean fixed)
Parameters:
fixed - True if the buffer cannot be expanded

size

public int size()
Returns:
The size of valid data in the buffer.

spareCapacity

public int spareCapacity()
Returns:
The available capacity of the buffer excluding pre and post reserves and data already written.

write

public void write(byte[] b)
            throws IOException

write

public void write(byte[] b,
                  int offset,
                  int length)
            throws IOException

write

public void write(int b)
            throws IOException

writeTo

public void writeTo(OutputStream out)
            throws IOException

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.