Package org.apache.lucene.util
Class UnsafeByteArrayInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.lucene.util.UnsafeByteArrayInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class UnsafeByteArrayInputStream extends InputStream
This class, much likeByteArrayInputStreamuses a given buffer as a source of an InputStream. Unlike ByteArrayInputStream, this class does not "waste" memory by creating a local copy of the given buffer, but rather uses the given buffer as is. Hence the name Unsafe. While using this class one should remember that the byte[] buffer memory is shared and might be changed from outside. For reuse-ability, a call forreInit(byte[])can be called, and initialize the stream with a new buffer.- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Constructor Summary
Constructors Constructor Description UnsafeByteArrayInputStream()Creates a new instance by not using any byte[] up front.UnsafeByteArrayInputStream(byte[] buffer)Creates an UnsafeByteArrayInputStream which uses a given byte array as the source of the stream.UnsafeByteArrayInputStream(byte[] buffer, int startPos, int endPos)Creates an UnsafeByteArrayInputStream which uses a given byte array as the source of the stream, at the specific range: [startPos, endPos)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidmark(int readlimit)booleanmarkSupported()intread()Read a byte.voidreInit(byte[] buffer)Initialize the stream with a given buffer, using the default limits of [0, buffer.length)voidreInit(byte[] buffer, int startPos, int endPos)Initialize the stream with a given byte array as the source of the stream, at the specific range: [startPos, endPos)voidreset()Resets the stream back to its original state.-
Methods inherited from class java.io.InputStream
close, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, skip, transferTo
-
-
-
-
Constructor Detail
-
UnsafeByteArrayInputStream
public UnsafeByteArrayInputStream()
Creates a new instance by not using any byte[] up front. If you use this constructor, you MUST call either of thereInitmethods before you consume any byte from this instance.
This constructor is for convenience purposes only, so that if one does not have the byte[] at the moment of creation, one is not forced to pass anew byte[0]or something. Obviously in that case, one will call eitherreInitmethods before using the class.
-
UnsafeByteArrayInputStream
public UnsafeByteArrayInputStream(byte[] buffer)
Creates an UnsafeByteArrayInputStream which uses a given byte array as the source of the stream. Default range is [0 , buffer.length)- Parameters:
buffer- byte array used as the source of this stream
-
UnsafeByteArrayInputStream
public UnsafeByteArrayInputStream(byte[] buffer, int startPos, int endPos)Creates an UnsafeByteArrayInputStream which uses a given byte array as the source of the stream, at the specific range: [startPos, endPos)- Parameters:
buffer- byte array used as the source of this streamstartPos- first index (inclusive) to the data lying in the given bufferendPos- an index (exclusive) where the data ends. data @ buffer[endPos] will never be read
-
-
Method Detail
-
mark
public void mark(int readlimit)
- Overrides:
markin classInputStream
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classInputStream
-
reInit
public void reInit(byte[] buffer)
Initialize the stream with a given buffer, using the default limits of [0, buffer.length)- Parameters:
buffer- byte array used as the source of this stream
-
reInit
public void reInit(byte[] buffer, int startPos, int endPos)Initialize the stream with a given byte array as the source of the stream, at the specific range: [startPos, endPos)- Parameters:
buffer- byte array used as the source of this streamstartPos- first index (inclusive) to the data lying in the given bufferendPos- an index (exclusive) where the data ends. data @ buffer[endPos] will never be read
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
read
public int read() throws IOExceptionRead a byte. Data returned as an integer [0,255] If end of stream reached, returns -1- Specified by:
readin classInputStream- Throws:
IOException
-
reset
public void reset() throws IOExceptionResets the stream back to its original state. Basically - moving the index back to start position.- Overrides:
resetin classInputStream- Throws:
IOException
-
-