Package org.apache.lucene.util
Class UnsafeByteArrayOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.lucene.util.UnsafeByteArrayOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class UnsafeByteArrayOutputStream extends OutputStream
This class is used as a wrapper to a byte array, extendingOutputStream. 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, whentoByteArray()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
Constructors Constructor Description UnsafeByteArrayOutputStream()Constructs a new output stream, with a default allocated buffer which can later be obtained viatoByteArray().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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetStartPos()Returns the start position data was written to.intlength()Returns the number of relevant bytes.voidreInit(byte[] buffer)For reuse-ability, this stream object can be re-initialized with another given buffer, using 0 as default starting position.voidreInit(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.voidwrite(byte[] b, int off, int len)writes a given byte[], with offset and length to the buffer.voidwrite(int value)writes a given byte(at the form of an int) to the buffer.-
Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
-
-
-
Constructor Detail
-
UnsafeByteArrayOutputStream
public UnsafeByteArrayOutputStream()
Constructs a new output stream, with a default allocated buffer which can later be obtained viatoByteArray().
-
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 IOExceptionwrites 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:
writein classOutputStream- Parameters:
value- byte value to be written- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOExceptionwrites 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:
writein classOutputStream- Parameters:
b- byte buffer, containing the source data to be writtenoff- index from which data from the buffer b should be writtenlen- 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 usedreInit(byte[], int)orUnsafeByteArrayOutputStream(byte[], int)and passed a start position which is not 0.
-
-