org.apache.lucene.util
Class UnsafeByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.apache.lucene.util.UnsafeByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class UnsafeByteArrayOutputStream
extends OutputStream

This class is used as a wrapper to a byte array, extending OutputStream. Data is written in the given byte[] buffer, until its length is insufficient. Than the buffer size is doubled and the data is written. This class is Unsafe as it is using a buffer which potentially can be changed from the outside. Moreover, when toByteArray() is called, the buffer itself is returned, and not a copy.

WARNING: This API is experimental and might change in incompatible ways in the next release.

Constructor Summary
UnsafeByteArrayOutputStream()
          Constructs a new output stream, with a default allocated buffer which can later be obtained via toByteArray().
UnsafeByteArrayOutputStream(byte[] buffer)
          Constructs a new output stream, with a given buffer.
UnsafeByteArrayOutputStream(byte[] buffer, int startPos)
          Constructs a new output stream, with a given buffer.
 
Method Summary
 int getStartPos()
          Returns the start position data was written to.
 int length()
          Returns the number of relevant bytes.
 void reInit(byte[] buffer)
          For reuse-ability, this stream object can be re-initialized with another given buffer, using 0 as default starting position.
 void reInit(byte[] buffer, int startPos)
          For reuse-ability, this stream object can be re-initialized with another given buffer and starting position.
 byte[] toByteArray()
          Returns the byte array saved within the buffer AS IS.
 void write(byte[] b, int off, int len)
          writes a given byte[], with offset and length to the buffer.
 void write(int value)
          writes a given byte(at the form of an int) to the buffer.
 
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
 

Constructor Detail

UnsafeByteArrayOutputStream

public UnsafeByteArrayOutputStream()
Constructs a new output stream, with a default allocated buffer which can later be obtained via toByteArray().


UnsafeByteArrayOutputStream

public UnsafeByteArrayOutputStream(byte[] buffer)
Constructs a new output stream, with a given buffer. Writing will start at index 0 as a default.

Parameters:
buffer - some space to which writing will be made

UnsafeByteArrayOutputStream

public UnsafeByteArrayOutputStream(byte[] buffer,
                                   int startPos)
Constructs a new output stream, with a given buffer. Writing will start at a given index.

Parameters:
buffer - some space to which writing will be made.
startPos - an index (inclusive) from white data will be written.
Method Detail

reInit

public void reInit(byte[] buffer,
                   int startPos)
For reuse-ability, this stream object can be re-initialized with another given buffer and starting position.

Parameters:
buffer - some space to which writing will be made.
startPos - an index (inclusive) from white data will be written.

reInit

public void reInit(byte[] buffer)
For reuse-ability, this stream object can be re-initialized with another given buffer, using 0 as default starting position.

Parameters:
buffer - some space to which writing will be made.

write

public void write(int value)
           throws IOException
writes a given byte(at the form of an int) to the buffer. If the buffer's empty space is insufficient, the buffer is doubled.

Specified by:
write in class OutputStream
Parameters:
value - byte value to be written
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
writes a given byte[], with offset and length to the buffer. If the buffer's empty space is insufficient, the buffer is doubled until it could contain all the data.

Overrides:
write in class OutputStream
Parameters:
b - byte buffer, containing the source data to be written
off - index from which data from the buffer b should be written
len - number of bytes that should be written
Throws:
IOException

toByteArray

public byte[] toByteArray()
Returns the byte array saved within the buffer AS IS.

Returns:
the actual inner buffer - not a copy of it.

length

public int length()
Returns the number of relevant bytes. This objects makes sure the buffer is at least the size of it's data. But it can also be twice as big. The user would want to process the relevant bytes only. For that he would need the count.

Returns:
number of relevant bytes

getStartPos

public int getStartPos()
Returns the start position data was written to. This is useful in case you used reInit(byte[], int) or UnsafeByteArrayOutputStream(byte[], int) and passed a start position which is not 0.



Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.