org.fusesource.hawtbuf
Class DataByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.fusesource.hawtbuf.DataByteArrayOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataOutput, java.io.Flushable

public class DataByteArrayOutputStream
extends java.io.OutputStream
implements java.io.DataOutput

Optimized ByteArrayOutputStream

Author:
Hiram Chirino

Field Summary
protected  byte[] buf
           
protected  int pos
           
 
Constructor Summary
DataByteArrayOutputStream()
          Creates a new byte array output stream.
DataByteArrayOutputStream(byte[] buf)
           
DataByteArrayOutputStream(int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
Method Summary
 byte[] getData()
           
protected  void onWrite()
          This method is called after each write to the buffer.
 int position()
           
 void position(int offset)
          Set the current position for writing
 void reset()
          reset the output stream
protected  void resize(int newcount)
           
 void restart()
          start using a fresh byte array
 void restart(int size)
          start using a fresh byte array
 int size()
           
 void skip(int size)
           
 Buffer toBuffer()
          Get a Buffer from the stream
 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.
 void writeBoolean(boolean v)
           
 void writeByte(int v)
           
 void writeBytes(java.lang.String s)
           
 void writeChar(int v)
           
 void writeChars(java.lang.String s)
           
 void writeDouble(double v)
           
 void writeFloat(float v)
           
 void writeInt(int v)
           
 void writeLong(long v)
           
 void writeShort(int v)
           
 void writeUTF(java.lang.String str)
           
 
Methods inherited from class java.io.OutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.io.DataOutput
write
 

Field Detail

buf

protected byte[] buf

pos

protected int pos
Constructor Detail

DataByteArrayOutputStream

public DataByteArrayOutputStream(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.

DataByteArrayOutputStream

public DataByteArrayOutputStream(byte[] buf)

DataByteArrayOutputStream

public DataByteArrayOutputStream()
Creates a new byte array output stream.

Method Detail

restart

public void restart(int size)
start using a fresh byte array

Parameters:
size -

restart

public void restart()
start using a fresh byte array


toBuffer

public Buffer toBuffer()
Get a Buffer from the stream

Returns:
the byte sequence

write

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

Specified by:
write in interface java.io.DataOutput
Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written.
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

Specified by:
write in interface java.io.DataOutput
Overrides:
write in class java.io.OutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException

getData

public byte[] getData()
Returns:
the underlying byte[] buffer

reset

public void reset()
reset the output stream


position

public void position(int offset)
              throws java.io.IOException
Set the current position for writing

Parameters:
offset -
Throws:
java.io.IOException

position

public int position()

size

public int size()

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Specified by:
writeBoolean in interface java.io.DataOutput
Throws:
java.io.IOException

writeByte

public void writeByte(int v)
               throws java.io.IOException
Specified by:
writeByte in interface java.io.DataOutput
Throws:
java.io.IOException

writeShort

public void writeShort(int v)
                throws java.io.IOException
Specified by:
writeShort in interface java.io.DataOutput
Throws:
java.io.IOException

writeChar

public void writeChar(int v)
               throws java.io.IOException
Specified by:
writeChar in interface java.io.DataOutput
Throws:
java.io.IOException

writeInt

public void writeInt(int v)
              throws java.io.IOException
Specified by:
writeInt in interface java.io.DataOutput
Throws:
java.io.IOException

writeLong

public void writeLong(long v)
               throws java.io.IOException
Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

writeFloat

public void writeFloat(float v)
                throws java.io.IOException
Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

writeDouble

public void writeDouble(double v)
                 throws java.io.IOException
Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Specified by:
writeBytes in interface java.io.DataOutput
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Specified by:
writeChars in interface java.io.DataOutput
Throws:
java.io.IOException

writeUTF

public void writeUTF(java.lang.String str)
              throws java.io.IOException
Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException

resize

protected void resize(int newcount)

onWrite

protected void onWrite()
                throws java.io.IOException
This method is called after each write to the buffer. This should allow subclasses to take some action based on the writes, for example flushing data to an external system based on size.

Throws:
java.io.IOException

skip

public void skip(int size)
          throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2010-2011 FuseSource, Corp.. All Rights Reserved.