Package net.jini.io
Class OptimizedByteArrayInputStream
java.lang.Object
java.io.InputStream
net.jini.io.OptimizedByteArrayInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
This class is an optimized porting of java.io.ByteArrayInputStream: a) All methods are not
synchronized. b) Most safety checks have been removed.
- Since:
- 6.6
- Author:
- niv
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected 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 index of the next character to read from the input stream buffer. -
Constructor Summary
ConstructorsConstructorDescriptionOptimizedByteArrayInputStream(byte[] buffer) Creates aByteArrayInputStreamso that it usesbufferas its buffer array. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of bytes that can be read from this input stream without blocking.intread()Reads the next byte of data from this input stream.intread(byte[] b, int off, int len) Reads up tolenbytes of data into an array of bytes from this input stream.bytereadByte()intvoidsetBuffer(byte[] buffer) Sets aGSByteArrayInputStreamso that it usesbufferas its buffer array.longskip(long n) Skipsnbytes of input from this input stream.Methods inherited from class java.io.InputStream
close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Field Details
-
_buffer
protected byte[] _bufferAn array of bytes that was provided by the creator of the stream. Elementsbuf[0]throughbuffer[count-1]are the only bytes that can ever be read from the stream; elementbuffer[pos]is the next byte to be read. -
_position
protected int _positionThe 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 bebuffer[position]. -
_count
protected int _countThe 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 ofbuffer. It is one greater than the position of the last byte withinbufferthat can ever be read from the input stream buffer.
-
-
Constructor Details
-
OptimizedByteArrayInputStream
public OptimizedByteArrayInputStream(byte[] buffer) Creates aByteArrayInputStreamso that it usesbufferas its buffer array. The buffer array is not copied. The initial value ofposis0and the initial value ofcountis the length ofbuffer.- Parameters:
buffer- the input buffer.
-
-
Method Details
-
setBuffer
public void setBuffer(byte[] buffer) Sets aGSByteArrayInputStreamso that it usesbufferas its buffer array. The buffer array is not copied. The initial value ofposis0and the initial value ofcountis the length ofbuffer.- Parameters:
buffer- the input buffer.
-
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.
-
readByte
public byte readByte() -
readUnsignedByte
public int readUnsignedByte() -
read
public int read(byte[] b, int off, int len) 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 bytesbuffer[pos]throughbuffer[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:
b- the buffer into which the data is read.off- the start offset of the data.len- 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 bytes that can be read from this input stream without blocking. The value returned iscount - pos, which is the number of bytes remaining to be read from the input buffer.- Overrides:
availablein classInputStream- Returns:
- the number of bytes that can be read from the input stream without blocking.
-