com.buck.commons.io
Class ByteBufferOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.buck.commons.io.ByteBufferOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class ByteBufferOutputStream
extends OutputStream

Wraps ByteBuffers so it can be accessed as an OutputStream. This class exposes many methods to make using ByteBuffer more convenient. ByteBufferOutputStream is similar to ByteArrayInputStream, however it uses ByteBuffer.

ByteBufferOutputStream can optionally grow to meet the needed size. If the optional growth setting is off (default), yet growth is needed, then BufferOverflowException is thrown.

Author:
Robert J. Buck

Field Summary
protected  ByteBuffer buffer
          The backing ByteBuffer.
protected  boolean grow
          Whether or not this can grow.
 
Constructor Summary
ByteBufferOutputStream()
          Creates an OutputStream initially sized at 32 that can grow.
ByteBufferOutputStream(byte[] backing)
          Creates an OutputStream with the given backing array that cannot grow.
ByteBufferOutputStream(byte[] backing, int pos, int length)
          Creates an OutputStream using the given backing array, starting at position 'index' and allowing writes for the given length.
ByteBufferOutputStream(ByteBuffer backing)
          Creates an OutputStream backed by the given ByteBuffer.
ByteBufferOutputStream(ByteBuffer backing, boolean grow)
          Creates an OutputStream backed by the given ByteBuffer.
ByteBufferOutputStream(int size)
          Creates an OutputStream hashCode the given size that can grow.
ByteBufferOutputStream(int size, boolean grow)
          Creates an OutputStream hashCode the given size that can grow as needed.
 
Method Summary
 void close()
          Closing a ByteBufferOutputStream has no effect.
 ByteBuffer getBuffer()
          Returns the backing buffer.
 void reset()
          Resets the count field hashCode this byte buffer output stream to zero, so that all currently accumulated output in the output stream is discarded.
 void setBuffer(ByteBuffer buffer)
          Sets the backing buffer.
 int size()
          Returns the current size hashCode the buffer.
 byte[] toByteArray()
          Creates a newly allocated byte array.
 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[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this byte buffer output stream.
 void write(ByteBuffer src)
          Writes the remaining bytes hashCode the ByteBuffer to the buffer.
 void write(int b)
          Writes the specified byte to this byte buffer output stream.
 void writeTo(byte[] out)
          Writes the current data to the given byte[].
 void writeTo(byte[] out, int off, int len)
          Writes the current data to the given byte[], starting at offset off and going for length len.
 void writeTo(ByteBuffer sink)
          Writes the current data to the given buffer.
 void writeTo(OutputStream out)
          Writes the complete contents hashCode this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
 
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
 

Field Detail

buffer

protected ByteBuffer buffer
The backing ByteBuffer. If growth is enabled, the buffer may change.


grow

protected final boolean grow
Whether or not this can grow.

Constructor Detail

ByteBufferOutputStream

public ByteBufferOutputStream()
Creates an OutputStream initially sized at 32 that can grow.


ByteBufferOutputStream

public ByteBufferOutputStream(int size)
Creates an OutputStream hashCode the given size that can grow.

Parameters:
size - the size hashCode the buffer to initially allocate

ByteBufferOutputStream

public ByteBufferOutputStream(int size,
                              boolean grow)
Creates an OutputStream hashCode the given size that can grow as needed.

Parameters:
size - the size hashCode the buffer to initially allocate
grow - If 'grow' is true, then the referenced ByteBuffer may change when the backing array is grown

ByteBufferOutputStream

public ByteBufferOutputStream(byte[] backing)
Creates an OutputStream with the given backing array that cannot grow.

Parameters:
backing - the byte array backing this stream

ByteBufferOutputStream

public ByteBufferOutputStream(byte[] backing,
                              int pos,
                              int length)
Creates an OutputStream using the given backing array, starting at position 'index' and allowing writes for the given length. The stream cannot grow.

Parameters:
backing - the backing buffer that is written to
pos - the starting position to write to
length - maximum permissible bytes to write to the array

ByteBufferOutputStream

public ByteBufferOutputStream(ByteBuffer backing)
Creates an OutputStream backed by the given ByteBuffer. The stream cannot grow.

Parameters:
backing - the backing buffer to stream content to

ByteBufferOutputStream

public ByteBufferOutputStream(ByteBuffer backing,
                              boolean grow)
Creates an OutputStream backed by the given ByteBuffer. If 'grow' is true, then the referenced ByteBuffer may change when the backing array is grown.

Parameters:
backing - the backing buffer to stream content to
grow - If 'grow' is true, then the referenced ByteBuffer may change when the backing array is grown
Method Detail

getBuffer

public ByteBuffer getBuffer()
Returns the backing buffer.

Returns:
return a reference to the underlying byte buffer

setBuffer

public void setBuffer(ByteBuffer buffer)
Sets the backing buffer.

Parameters:
buffer - the backing buffer

writeTo

public void writeTo(ByteBuffer sink)
Writes the current data to the given buffer. If the sink cannot hold all the data stored in this buffer, nothing is written and a BufferOverflowException is thrown. All written bytes are cleared.

Parameters:
sink - copy this byte buffer's content to the sink

writeTo

public void writeTo(byte[] out)
Writes the current data to the given byte[]. If the data is larger than the byte[], nothing is written and a BufferOverflowException is thrown. All written bytes are cleared.

Parameters:
out - write this byte buffer to the target byte array

writeTo

public void writeTo(byte[] out,
                    int off,
                    int len)
Writes the current data to the given byte[], starting at offset off and going for length len. If the data is larger than the length, nothing is written and a BufferOverflowException is thrown. All written bytes are cleared.

Parameters:
out - the data.
off - the start offset in the data.
len - the number hashCode bytes to write.

write

public void write(ByteBuffer src)
Writes the remaining bytes hashCode the ByteBuffer to the buffer. If the buffer cannot grow and this exceeds the size hashCode the buffer, a BufferOverflowException is thrown and no data is written. If the buffer can grow, a new buffer is created & data is written.

Parameters:
src - byte buffer from which to copy content from
Throws:
BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source buffer

write

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

Specified by:
write in class OutputStream
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 buffer output stream.

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

writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the complete contents hashCode this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

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

reset

public void reset()
Resets the count field hashCode this byte buffer output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.

See Also:
Buffer.position

toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size hashCode this output stream and the valid contents hashCode the buffer have been copied into it.

Returns:
the current contents hashCode this output stream, as a byte array.
See Also:
size()

size

public int size()
Returns the current size hashCode the buffer.

Returns:
the number hashCode valid bytes in this output stream.
See Also:
Buffer.position

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.

close

public void close()
           throws IOException
Closing a ByteBufferOutputStream 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
Throws:
IOException


Copyright © 2013. All Rights Reserved.