net.wimpi.modbus.io
Class FastByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by net.wimpi.modbus.io.FastByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
BytesOutputStream

public class FastByteArrayOutputStream
extends OutputStream

This class is a clean room implementation of the ByteArrayOutputStream, with enhancements for speed (no synchronization for example).

The idea for such an implementation was originally obtained from Berkeley DB JE, however, this represents a clean-room implementation that is NOT derived from their implementation for license reasons and differs in implementation considerably. For compatibility reasons we have tried to conserve the interface as much as possible.

Version:
@version@ (@date@)
Author:
Dieter Wimberger

Field Summary
static int DEFAULT_SIZE
           
 
Constructor Summary
FastByteArrayOutputStream()
          Create a new FastByteArrayOutputStream.
FastByteArrayOutputStream(byte[] buf)
          Create a new FastByteArrayOutputStream with a given initial buffer.
FastByteArrayOutputStream(int bufferSize)
          Create a new FastByteArrayOutputStream with a given initial buffer size.
 
Method Summary
 void close()
          Closing a FastByteArrayOutputStream has no effect.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this FastByteArrayOutputStreams buffer, if necessary, to ensure that it can hold at least the number of bytes specified by the minimum capacity argument.
 byte[] getBuffer()
          Returns a reference to the buffer of this FastByteArrayOutputStream.
 void reset()
          Resets the count of this FastByteArrayOutputStream to zero, so that all currently accumulated output in the ouput stream is discarded when overwritten.
 int size()
          Returns the current number of bytes in the buffer of this FastByteArrayOutputStream.
 byte[] toByteArray()
          Returns a newly allocated byte[] with the actual content of the buffer of this FastByteArrayOutputStream.
 void toByteArray(byte[] b, int offset)
          Copies the content of this FastByteArrayOutputStream into the given byte array, starting from the given offset.
 String toString()
          Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 String toString(String enc)
          Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
 void write(byte[] buf)
          Convenience method that writes all bytes from the specified byte array to this FastByteArrayOutputStream.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this FastByteArrayOutputStream.
 void write(int b)
          Writes the specified byte to this byte array output stream.
 void writeTo(OutputStream out)
          Writes the complete contents of this FastByteArrayOutputStream to the specified output stream argument.
 
Methods inherited from class java.io.OutputStream
flush
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
See Also:
Constant Field Values
Constructor Detail

FastByteArrayOutputStream

public FastByteArrayOutputStream()
Create a new FastByteArrayOutputStream. The default value DEFAULT_SIZE will be used.


FastByteArrayOutputStream

public FastByteArrayOutputStream(int bufferSize)
Create a new FastByteArrayOutputStream with a given initial buffer size.

Parameters:
bufferSize - the initial size of the buffer as int.

FastByteArrayOutputStream

public FastByteArrayOutputStream(byte[] buf)
Create a new FastByteArrayOutputStream with a given initial buffer.

Parameters:
buf - the buffer as byte[].
Method Detail

close

public void close()
Closing a FastByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream

reset

public void reset()
Resets the count of this FastByteArrayOutputStream to zero, so that all currently accumulated output in the ouput stream is discarded when overwritten.


size

public int size()
Returns the current number of bytes in the buffer of this FastByteArrayOutputStream.

Returns:
the amount of bytes in the buffer.

toByteArray

public byte[] toByteArray()
Returns a newly allocated byte[] with the actual content of the buffer of this FastByteArrayOutputStream.

Returns:
the current contents of this output stream, as a byte[].
See Also:
size()

toString

public 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 Object
Returns:
String translated from the buffer's contents.

toString

public String toString(String enc)
                throws 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:
String translated from the buffer's contents.
Throws:
UnsupportedEncodingException - If the named encoding is not supported.

write

public void write(byte[] b,
                  int off,
                  int len)
Writes len bytes from the specified byte array starting at offset off to this FastByteArrayOutputStream.

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

write

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

Specified by:
write in class OutputStream
Parameters:
b - the byte to be written.

writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the complete contents of this FastByteArrayOutputStream to the specified output stream argument.

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

write

public void write(byte[] buf)
           throws IOException
Convenience method that writes all bytes from the specified byte array to this FastByteArrayOutputStream.

Overrides:
write in class OutputStream
Parameters:
buf -
Throws:
IOException

ensureCapacity

public final void ensureCapacity(int minCapacity)
Increases the capacity of this FastByteArrayOutputStreams buffer, if necessary, to ensure that it can hold at least the number of bytes specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.

toByteArray

public void toByteArray(byte[] b,
                        int offset)
Copies the content of this FastByteArrayOutputStream into the given byte array, starting from the given offset.

Parameters:
b - the buffer to hold a copy of the data.
offset - the offset at which to start copying.

getBuffer

public byte[] getBuffer()
Returns a reference to the buffer of this FastByteArrayOutputStream.

Returns:
the buffer.


Copyright © 2010. All Rights Reserved.