Package com.helger.commons.io.stream
Class NonBlockingByteArrayInputStream
java.lang.Object
java.io.InputStream
com.helger.commons.io.stream.NonBlockingByteArrayInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
StringInputStream
A non-synchronized copy of the class
ByteArrayInputStream.- Author:
- Philip Helger
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanprotected byte[]An array of bytes that was provided by the creator of the stream.protected intThe index one greater than the last valid character in the input stream buffer.protected intThe currently marked position in the stream.protected intThe index of the next character to read from the input stream buffer. -
Constructor Summary
ConstructorsConstructorDescriptionNonBlockingByteArrayInputStream(byte[] aBuf) Creates aNonBlockingByteArrayInputStreamso that it usesbufas its buffer array.NonBlockingByteArrayInputStream(byte[] aBuf, boolean bIsCopyNeeded) Creates aNonBlockingByteArrayInputStreamso that it usesbufas its buffer array.NonBlockingByteArrayInputStream(byte[] aBuf, int nOfs, int nLen) CreatesNonBlockingByteArrayInputStreamthat usesaBufas its buffer array.NonBlockingByteArrayInputStream(byte[] aBuf, int nOfs, int nLen, boolean bIsCopyNeeded) CreatesNonBlockingByteArrayInputStreamthat usesaBufas its buffer array. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of remaining bytes that can be read (or skipped over) from this input stream.voidclose()Closing aNonBlockingByteArrayInputStreamhas no effect.intfinal booleanisCopy()voidmark(int readAheadLimit) Set the current marked position in the stream.booleanTests if thisInputStreamsupports mark/reset.intread()Reads the next byte of data from this input stream.intread(byte[] aBuf, int nOfs, int nLen) Reads up tolenbytes of data into an array of bytes from this input stream.voidreset()Resets the buffer to the marked position.longskip(long n) Skipsnbytes of input from this input stream.Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
DEFAULT_COPY_NEEDED
public static final boolean DEFAULT_COPY_NEEDED- See Also:
-
m_aBuf
protected byte[] m_aBufAn array of bytes that was provided by the creator of the stream. Elementsbuf[0]throughbuf[count-1]are the only bytes that can ever be read from the stream; elementbuf[pos]is the next byte to be read. -
m_nPos
protected int m_nPosThe index of the next character to read from the input stream buffer. This value should always be nonnegative and not larger than the value ofcount. The next byte to be read from the input stream buffer will bebuf[pos]. -
m_nMark
protected int m_nMarkThe currently marked position in the stream. NonBlockingByteArrayInputStream objects are marked at position zero by default when constructed. They may be marked at another position within the buffer by themark()method. The current buffer position is set to this point by thereset()method.If no mark has been set, then the value of mark is the offset passed to the constructor (or 0 if the offset was not supplied).
-
m_nCount
protected int m_nCountThe index one greater than the last valid character in the input stream buffer. This value should always be nonnegative and not larger than the length ofbuf. It is one greater than the position of the last byte withinbufthat can ever be read from the input stream buffer.
-
-
Constructor Details
-
NonBlockingByteArrayInputStream
Creates aNonBlockingByteArrayInputStreamso that it usesbufas its buffer array. The buffer array is not copied. The initial value ofposis0and the initial value ofcountis the length ofbuf.- Parameters:
aBuf- the input buffer.
-
NonBlockingByteArrayInputStream
Creates aNonBlockingByteArrayInputStreamso that it usesbufas its buffer array. The buffer array is not copied. The initial value ofposis0and the initial value ofcountis the length ofbuf.- Parameters:
aBuf- the input buffer.bIsCopyNeeded-trueif the array should be copied,falseif not.
-
NonBlockingByteArrayInputStream
public NonBlockingByteArrayInputStream(@Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen) CreatesNonBlockingByteArrayInputStreamthat usesaBufas its buffer array. The initial value ofnOfsisoffsetand the initial value ofm_nCountis the minimum ofnOfs+nLenandaBuf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.- Parameters:
aBuf- the input buffer.nOfs- the offset in the buffer of the first byte to read.nLen- the maximum number of bytes to read from the buffer.
-
NonBlockingByteArrayInputStream
public NonBlockingByteArrayInputStream(@Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen, boolean bIsCopyNeeded) CreatesNonBlockingByteArrayInputStreamthat usesaBufas its buffer array. The initial value ofnOfsisoffsetand the initial value ofm_nCountis the minimum ofnOfs+nLenandaBuf.length. The buffer's mark is set to the specified offset.- Parameters:
aBuf- the input buffer.nOfs- the offset in the buffer of the first byte to read.nLen- the maximum number of bytes to read from the buffer.bIsCopyNeeded-trueif the provided byte array needs to be copied,falseif not.
-
-
Method Details
-
isCopy
public final boolean isCopy() -
read
public int read()Reads the next byte of data from this input stream. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is returned.This
readmethod cannot block.- Specified by:
readin classInputStream- Returns:
- the next byte of data, or
-1if the end of the stream has been reached.
-
read
public int read(byte[] aBuf, int nOfs, int nLen) Reads up tolenbytes of data into an array of bytes from this input stream. Ifposequalscount, then-1is returned to indicate end of file. Otherwise, the numberkof bytes read is equal to the smaller oflenandcount-pos. Ifkis positive, then bytesbuf[pos]throughbuf[pos+k-1]are copied intob[off]throughb[off+k-1]in the manner performed bySystem.arraycopy. The valuekis added intoposandkis returned.This
readmethod cannot block.- Overrides:
readin classInputStream- Parameters:
aBuf- the buffer into which the data is read.nOfs- the start offset in the destination arraybnLen- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1if there is no more data because the end of the stream has been reached.
-
skip
public long skip(long n) Skipsnbytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual numberkof bytes to be skipped is equal to the smaller ofnandcount-pos. The valuekis added intoposandkis returned.- Overrides:
skipin classInputStream- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
-
available
public int available()Returns the number of remaining bytes that can be read (or skipped over) from this input stream.The value returned is
count - pos, which is the number of bytes remaining to be read from the input buffer.- Overrides:
availablein classInputStream- Returns:
- the number of remaining bytes that can be read (or skipped over) from this input stream without blocking.
-
markSupported
public boolean markSupported()Tests if thisInputStreamsupports mark/reset. ThemarkSupportedmethod ofNonBlockingByteArrayInputStreamalways returnstrue.- Overrides:
markSupportedin classInputStream- Returns:
- Always true
-
mark
public void mark(int readAheadLimit) Set the current marked position in the stream. NonBlockingByteArrayInputStream objects are marked at position zero by default when constructed. They may be marked at another position within the buffer by this method.If no mark has been set, then the value of the mark is the offset passed to the constructor (or 0 if the offset was not supplied).
Note: The
readAheadLimitfor this class has no meaning.- Overrides:
markin classInputStream
-
reset
public void reset()Resets the buffer to the marked position. The marked position is 0 unless another position was marked or an offset was specified in the constructor.- Overrides:
resetin classInputStream
-
close
public void close()Closing aNonBlockingByteArrayInputStreamhas no effect. The methods in this class can be called after the stream has been closed without generating anIOException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream
-
getPosition
- Returns:
- The position where we are in the stream.
-