com.devexperts.io
Class ByteArrayOutput

java.lang.Object
  extended by java.io.OutputStream
      extended by com.devexperts.io.BufferedOutput
          extended by com.devexperts.io.ByteArrayOutput
All Implemented Interfaces:
Closeable, DataOutput, Flushable, ObjectOutput

public class ByteArrayOutput
extends BufferedOutput

An implementation of BufferedOutput using single byte array buffer.


Field Summary
 
Fields inherited from class com.devexperts.io.BufferedOutput
buffer, EMPTY_BYTE_ARRAY, limit, position
 
Constructor Summary
ByteArrayOutput()
          Creates a new byte array output without pre-allocated buffer.
ByteArrayOutput(int size)
          Creates a new byte array output with specified buffer capacity.
 
Method Summary
 void clear()
          Clears this buffer.
 void ensureCapacity(int requiredCapacity)
          Ensures that the byte array used for buffering has at least the specified capacity.
 byte[] getBuffer()
          Returns byte array used for buffering.
 int getLimit()
          Returns limit.
 int getPosition()
          Returns position.
protected  void needSpace()
          This method is invoked when output methods need more space to write data.
 void setBuffer(byte[] newBuffer)
          Sets specified byte array to be used for buffering.
 void setLimit(int newLimit)
          Sets limit as specified.
 void setPosition(int newPosition)
          Sets position as specified.
 byte[] toByteArray()
          Returns a full copy of byte array buffer from zero to the position.
 String toString()
          Converts the buffer's content from zero to the position into a string, translating bytes into characters according to the platform's default character encoding.
 
Methods inherited from class com.devexperts.io.BufferedOutput
write, write, write, writeBoolean, writeByte, writeByteArray, writeBytes, writeChar, writeChars, writeCompactInt, writeCompactLong, writeDouble, writeFloat, writeInt, writeLong, writeObject, writeShort, writeUTF, writeUTFChar, writeUTFString
 
Methods inherited from class java.io.OutputStream
close, flush
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.io.ObjectOutput
close, flush
 

Constructor Detail

ByteArrayOutput

public ByteArrayOutput()
Creates a new byte array output without pre-allocated buffer.


ByteArrayOutput

public ByteArrayOutput(int size)
Creates a new byte array output with specified buffer capacity.

Parameters:
size - the initial buffer size
Throws:
IllegalArgumentException - if size is negative
Method Detail

needSpace

protected void needSpace()
                  throws IOException
Description copied from class: BufferedOutput
This method is invoked when output methods need more space to write data. This method must ensure that expression (position < limit) is true or throw an exception. This method may block if needed. This method is allowed to update buffer, position and limit fields as needed.

Specified by:
needSpace in class BufferedOutput
Throws:
IOException - if an I/O error occurs

getBuffer

public byte[] getBuffer()
Returns byte array used for buffering. Never returns null.


setBuffer

public void setBuffer(byte[] newBuffer)
Sets specified byte array to be used for buffering. Null is treated as empty array. The position is set to zero, the limit is set to the capacity of new buffer.


getPosition

public int getPosition()
Returns position.


setPosition

public void setPosition(int newPosition)
Sets position as specified.

Throws:
IllegalArgumentException - if the new position is negative or is larger than the limit

getLimit

public int getLimit()
Returns limit.


setLimit

public void setLimit(int newLimit)
Sets limit as specified. If the position is larger than the new limit then it is set to the new limit.

Throws:
IllegalArgumentException - if the new limit is negative or is larger than the capacity

clear

public void clear()
Clears this buffer. The position is set to zero, the limit is set to the capacity.


ensureCapacity

public void ensureCapacity(int requiredCapacity)
Ensures that the byte array used for buffering has at least the specified capacity. This method reallocates buffer if needed and copies content of old buffer into new one. This method also sets the limit to the capacity.


toByteArray

public byte[] toByteArray()
Returns a full copy of byte array buffer from zero to the position.


toString

public String toString()
Converts the buffer's content from zero to the position into a string, translating bytes into characters according to the platform's default character encoding.

Overrides:
toString in class Object


Copyright © 2013 Devexperts. All Rights Reserved.