pulse - the web application framework Get pulse at SourceForge.net. Fast, secure and Free Open Source software downloads Ohloh project report for pulse-java

org.torweg.pulse.util.io
Class FastByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.torweg.pulse.util.io.FastByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public final class FastByteArrayOutputStream
extends java.io.OutputStream

A not thread-safe replacement for ByteArrayOutputStream.

Version:
$Revision$
Author:
Thomas Weber
See Also:
FastStringWriter

Constructor Summary
FastByteArrayOutputStream()
          creates a new byte array output stream.
FastByteArrayOutputStream(int size)
          creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
FastByteArrayOutputStream(int size, java.io.OutputStream target)
          creates a new byte array output stream, with a buffer capacity of the specified size, in bytes writing through to the given OutputStream.
FastByteArrayOutputStream(java.io.OutputStream target)
          creates a new byte array output stream writing through to the given OutputStream.
 
Method Summary
 void close()
          closes the optional write-through OutputStream.
 void flush()
          flushes the optional write-through OutputStream.
 byte[] getByteArray()
          returns a reference of the underlying byte array buffer, thus avoiding a duplication of the buffer in memory.
 int size()
          returns the current size of the bytes buffered.
 java.lang.String toString()
          converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 java.lang.String toString(java.lang.String enc)
          converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
 void write(byte[] b, int off, int len)
          
 void write(int b)
          writes the specified byte to the byte buffer.
 void writeTo(java.io.OutputStream out)
          writes the currently buffered bytes to the given OutputStream.
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FastByteArrayOutputStream

public FastByteArrayOutputStream()
creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.


FastByteArrayOutputStream

public FastByteArrayOutputStream(java.io.OutputStream target)
creates a new byte array output stream writing through to the given OutputStream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.

Parameters:
target - the output stream to write through

FastByteArrayOutputStream

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

Parameters:
size - the initial size
Throws:
java.lang.IllegalArgumentException - if size is negative

FastByteArrayOutputStream

public FastByteArrayOutputStream(int size,
                                 java.io.OutputStream target)
creates a new byte array output stream, with a buffer capacity of the specified size, in bytes writing through to the given OutputStream.

Parameters:
size - the initial size
target - the output stream to write through
Throws:
java.lang.IllegalArgumentException - if size is negative
Method Detail

write

public void write(int b)
writes the specified byte to the byte buffer.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written
Throws:
WriteThroughException - on I/O errors in the optional write-through OutputStream

write

public void write(byte[] b,
                  int off,
                  int len)

Overrides:
write in class java.io.OutputStream
Throws:
WriteThroughException - on I/O errors in the optional write-through OutputStream

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
writes the currently buffered bytes to the given OutputStream.

Parameters:
out - the output stream to which to write the data
Throws:
java.io.IOException - if an I/O error occurs.

getByteArray

public byte[] getByteArray()
returns a reference of the underlying byte array buffer, thus avoiding a duplication of the buffer in memory.

Returns:
a reference of the underlying byte array buffer

size

public int size()
returns the current size of the bytes buffered.

Returns:
the current size of the bytes buffered

toString

public java.lang.String toString()
converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Overrides:
toString in class java.lang.Object
Returns:
a string translated from the buffer's contents.

toString

public java.lang.String toString(java.lang.String enc)
                          throws java.io.UnsupportedEncodingException
converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.

Parameters:
enc - a character-encoding name.
Returns:
a string translated from the buffer's contents.
Throws:
java.io.UnsupportedEncodingException - If the named encoding is not supported.

flush

public void flush()
flushes the optional write-through OutputStream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
WriteThroughException - on I/O errors in the optional write-through OutputStream
See Also:
FastByteArrayOutputStream(OutputStream), FastByteArrayOutputStream(int,OutputStream)

close

public void close()
closes the optional write-through OutputStream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
WriteThroughException - on I/O errors in the optional write-through OutputStream
See Also:
FastByteArrayOutputStream(OutputStream), FastByteArrayOutputStream(int,OutputStream)