public abstract class BufferedInput extends InputStream implements ObjectInput
null
.
seek(long)
, skip(long)
, rewind(long)
and other.
All BufferedInput implementations are required to support marking because proper marking plays a vital role in definition and implementation of certain reposition methods.
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer |
protected static byte[] |
EMPTY_BYTE_ARRAY |
protected int |
limit |
protected long |
markPosition |
protected int |
position |
protected long |
totalPositionBase |
Modifier | Constructor and Description |
---|---|
protected |
BufferedInput()
Creates new instance with empty buffer.
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes that can be read without blocking.
|
protected void |
checkEncapsulatedLength(long length,
long min,
long max)
Validate encapsulated content length according to
specified bounds and stream limits.
|
protected void |
checkEOB()
Throws
IllegalStateException if expression (position != limit) is true. |
protected void |
checkRewind(long n)
Throws appropriate exception if rewind for specified distance is impossible.
|
boolean |
hasAvailable()
Checks if more bytes can be read from this input.
|
boolean |
hasAvailable(int bytes)
Checks if the specified number of bytes can be read from this input.
|
void |
mark()
Marks the current position in this buffered input.
|
void |
mark(int readLimit)
Deprecated.
This method is for compatibility with interface of
InputStream class only,
do not use it, because the actual value of readLimit is irrelevant for
all implementations of BufferedInput .
Use mark and unmark methods instead. |
void |
mark(long readLimit)
Deprecated.
This method is for conceptual compatibility with interface of
InputStream class only,
do not use it, because the actual value of readLimit is irrelevant for
all implementations of BufferedInput .
Use mark and unmark methods instead. |
boolean |
markSupported()
|
protected void |
needData()
This method is invoked when input methods need more bytes to read data.
|
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int off,
int len) |
boolean |
readBoolean() |
byte |
readByte() |
byte[] |
readByteArray()
Reads an array of bytes in a compact encapsulation format.
|
char |
readChar() |
int |
readCompactInt()
Reads an
int value in a compact format (see IOUtil ). |
long |
readCompactLong()
Reads a
long value in a compact format (see IOUtil ). |
protected abstract int |
readData()
This method is invoked when input methods need more bytes to read data.
|
double |
readDouble() |
float |
readFloat() |
void |
readFully(byte[] b) |
void |
readFully(byte[] b,
int off,
int len) |
int |
readInt() |
String |
readLine() |
long |
readLong() |
<T> Marshalled<T> |
readMarshalled(Marshaller<T> marshaller)
Reads
Marshalled object from a byte stream with a specified marshaller. |
<T> Marshalled<T> |
readMarshalled(Marshaller<T> marshaller,
SerialClassContext serialContext)
Reads
Marshalled object from a byte stream with a specified marshaller. |
Object |
readObject()
Read and return an object.
|
Object |
readObject(ClassLoader cl)
Read and return an object with a specified class loader.
|
Object |
readObject(SerialClassContext serialContext)
Read and return an object with a specified serial class context.
|
short |
readShort() |
void |
readToByteBuffer(ByteBuffer buffer)
Reads data from this buffered input into specified byte buffer.
|
long |
readToDataOutput(DataOutput out,
long length)
Reads data from this buffered input into specified data output.
|
long |
readToOutputStream(OutputStream out,
long length)
Reads data from this buffered input into specified output stream.
|
int |
readUnsignedByte() |
int |
readUnsignedShort() |
String |
readUTF() |
int |
readUTFBody(long utfLength,
char[] chars,
int offset)
Reads Unicode body in a UTF-8 format.
|
int |
readUTFChar()
Reads Unicode code point in a UTF-8 format.
|
String |
readUTFString()
Reads Unicode string in a UTF-8 format with compact encapsulation (see
IOUtil ). |
void |
reset()
Repositions this buffered input to the position at the time the
mark method was last called on this input. |
abstract void |
rewind(long n)
Rewinds specified number of bytes.
|
void |
seek(long totalPosition)
Repositions this buffered input to specified total position.
|
long |
skip(long n) |
int |
skipBytes(int n) |
protected void |
throwEOFException()
Throws
EOFException when requested by needData() method. |
long |
totalPosition()
Returns total position of this input stream.
|
void |
unmark()
Invalidates
mark position in this buffered input. |
close
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
close
protected static final byte[] EMPTY_BYTE_ARRAY
protected byte[] buffer
protected int position
protected int limit
protected long totalPositionBase
protected long markPosition
protected void needData() throws IOException
(position < limit)
is true or throw an exception.
This method may block if needed.
This method is allowed to update buffer, position, limit and totalPositionBase fields as needed.
This method shall throw an EOFException
if the end of stream is reached.
IOException
- if an I/O error occurs or end of stream is reachedprotected abstract int readData() throws IOException
-1
if no bytes can be read because the end of the stream has been reached.
This method may block if needed.
This method is allowed to update buffer, position, limit and totalPositionBase fields as needed.
This method shall never throw an EOFException
.
-1
if there is no more data because the end of the stream has been reachedIOException
- if an I/O error occursprotected void throwEOFException() throws EOFException
EOFException
when requested by needData()
method.
This implementation throws same reused instance with truncated stack trace to avoid garbage.EOFException
protected final void checkEOB()
IllegalStateException
if expression (position != limit)
is true.protected void checkEncapsulatedLength(long length, long min, long max) throws IOException
Data retrieval methods getting variable amount of data can invoke this method to check an upper bound of available data for validation.
length
- encapsulated length value to be checkedmin
- minimal theoretically valid length valuemax
- maximal theoretically valid length valueIOException
- if length < min || length > max
or length
bytes definitely cannot
be retrievedpublic final int read() throws IOException
read
in interface ObjectInput
read
in class InputStream
IOException
public final int read(byte[] b) throws IOException
read
in interface ObjectInput
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in interface ObjectInput
read
in class InputStream
IOException
public long skip(long n) throws IOException
skip
in interface ObjectInput
skip
in class InputStream
IOException
public boolean hasAvailable() throws IOException
available
() > 0
check.
This implementation returns
hasAvailable
(1)
true
if more bytes can be read from this input.IOException
- if an I/O error occurs.public boolean hasAvailable(int bytes) throws IOException
available
() >= bytes
check.
This implementation returns
available
() >= bytes
bytes
- the number of bytes.true
if the specified number of bytes can be read from this input.IOException
- if an I/O error occurs.public int available() throws IOException
available
in interface ObjectInput
available
in class InputStream
IOException
- If an I/O error has occurred.public final void readFully(byte[] b) throws IOException
readFully
in interface DataInput
IOException
public void readFully(byte[] b, int off, int len) throws IOException
readFully
in interface DataInput
IOException
public final int skipBytes(int n) throws IOException
skipBytes
in interface DataInput
IOException
public final boolean readBoolean() throws IOException
readBoolean
in interface DataInput
IOException
public final byte readByte() throws IOException
readByte
in interface DataInput
IOException
public final int readUnsignedByte() throws IOException
readUnsignedByte
in interface DataInput
IOException
public final short readShort() throws IOException
readShort
in interface DataInput
IOException
public final int readUnsignedShort() throws IOException
readUnsignedShort
in interface DataInput
IOException
public final char readChar() throws IOException
readChar
in interface DataInput
IOException
public final int readInt() throws IOException
readInt
in interface DataInput
IOException
public final long readLong() throws IOException
readLong
in interface DataInput
IOException
public final float readFloat() throws IOException
readFloat
in interface DataInput
IOException
public final double readDouble() throws IOException
readDouble
in interface DataInput
IOException
public final String readLine() throws IOException
readLine
in interface DataInput
IOException
public final String readUTF() throws IOException
readUTF
in interface DataInput
IOException
public final Object readObject() throws IOException
Thread.getContextClassLoader
) or
using the same classloader that loaded classes for this com.devexperts.io
package when
context class loader is not defined.
This is a shortcut for readObject
(null)
.readObject
in interface ObjectInput
IOException
- if an I/O error occurs or if object cannot be deserializedpublic final Object readObject(ClassLoader cl) throws IOException
cl
- the ClassLoader that will be used to load classes;
null
for context
class loader.IOException
- if an I/O error occurs or if object cannot be deserializedpublic final Object readObject(SerialClassContext serialContext) throws IOException
serialContext
- the serial class context.IOException
- if an I/O error occurs or if object cannot be deserializedpublic final <T> Marshalled<T> readMarshalled(Marshaller<T> marshaller) throws IOException
Marshalled
object from a byte stream with a specified marshaller.
This method determines the number of marshalled bytes using
Marshaller.readMarshalledLength
,
reads the corresponding number of bytes, and wraps them into Marshalled
object instance.
The result of this method is not null
.
When the serialization
marshaller is used, then this method
reads the same bytes as would have been been read by readObject
method,
so in.
is mostly equivalent to
readObject
()in.readMarshalled(
.
Marshaller.SERIALIZATION
).getObject
()
To read an arbitrary marshalled object, this method must specify the same marshaller
that was
used in a marshalled object that was written by a corresponding
BufferedOutput.writeMarshalled
method invocation.
marshaller
- a strategy that defines how an object is represented in a byte arrayIOException
- if an I/O error occurspublic final <T> Marshalled<T> readMarshalled(Marshaller<T> marshaller, SerialClassContext serialContext) throws IOException
Marshalled
object from a byte stream with a specified marshaller.
This method determines the number of marshalled bytes using
Marshaller.readMarshalledLength
,
reads the corresponding number of bytes, and wraps them into Marshalled
object instance.
The result of this method is not null
.
When the serialization
marshaller is used, then this method
reads the same bytes as would have been been read by readObject
method,
so in.
is mostly equivalent to
readObject
()in.readMarshalled(
.
Marshaller.SERIALIZATION
).getObject
()
To read an arbitrary marshalled object, this method must specify the same marshaller
that was
used in a marshalled object that was written by a corresponding
BufferedOutput.writeMarshalled
method invocation.
marshaller
- a strategy that defines how an object is represented in a byte arrayserialContext
- a serial class context that will be used to load classes;IOException
- if an I/O error occurspublic final int readCompactInt() throws IOException
int
value in a compact format (see IOUtil
).
If actual encoded value does not fit into an int
data type,
then it is truncated to int
value (only lower 32 bits are returned);
the number is read entirely in this case.
Equivalent to IOUtil.readCompactInt(java.io.DataInput)
method.int
value readIOException
- if an I/O error occurspublic final long readCompactLong() throws IOException
long
value in a compact format (see IOUtil
).
Equivalent to IOUtil.readCompactLong(java.io.DataInput)
method.long
value readIOException
- if an I/O error occurspublic final byte[] readByteArray() throws IOException
IOUtil.readByteArray(java.io.DataInput)
method.IOException
- if an I/O error occurspublic final int readUTFChar() throws IOException
IOUtil.readUTFChar(java.io.DataInput)
method.UTFDataFormatException
- if the bytes do not represent a valid UTF-8 encoding of a characterIOException
- if an I/O error occurspublic final String readUTFString() throws IOException
IOUtil
).
Overlong UTF-8 and CESU-8-encoded surrogates are accepted and read without errors.
This method defines length as a number of bytes.
Equivalent to IOUtil.readUTFString(java.io.DataInput)
method.UTFDataFormatException
- if the bytes do not represent a valid UTF-8 encoding of a stringIOException
- if an I/O error occurspublic final int readUTFBody(long utfLength, char[] chars, int offset) throws IOException
IOUtil.readUTFBody(java.io.DataInput, long, char[], int)
method.utfLength
- the number of bytes to readchars
- the char array in which read characters are stored in UTF-16 formatoffset
- the start offset into the chars
array where read characters are storedchars
arrayArrayIndexOutOfBoundsException
- if the chars
array is too small to accommodate read charactersUTFDataFormatException
- if the bytes do not represent a valid UTF-8 encoding of a stringIOException
- if an I/O error occurspublic final long totalPosition()
seek(long)
, skip(long)
, rewind(long)
, reset()
, etc.public final void seek(long totalPosition) throws IOException
rewind(long)
operation
for appropriate distance is performed; the stream has to be properly marked to support this operation.
If specified total position is larger than current total position, then skip(long)
operation
for appropriate distance is performed until requested total position is reached; if end of stream
happens before this operation can be completed then EOFException
is thrown.IllegalArgumentException
- if total position is negativeIllegalStateException
- if this stream is not marked or if attempting to rewind past the marked positionIOException
- if an I/O error occursEOFException
- if end of stream is reachedpublic final boolean markSupported()
markSupported
in class InputStream
public void mark()
public void unmark()
mark
position in this buffered input.
This method does nothing is this input was not marked.public final void mark(int readLimit)
InputStream
class only,
do not use it, because the actual value of readLimit
is irrelevant for
all implementations of BufferedInput
.
Use mark
and unmark
methods instead.reset()
method repositions this stream at the last marked
position so that subsequent reads re-read the same bytes.
The readLimit
argument tells this input stream to allow
that many bytes to be read before the mark position gets invalidated.
Negative value for readLimit
may be used in order to invalidate the mark.
This implementation calls mark()
when readLimit >= 0
and
unmark()
otherwise.
mark
in class InputStream
public final void mark(long readLimit)
InputStream
class only,
do not use it, because the actual value of readLimit
is irrelevant for
all implementations of BufferedInput
.
Use mark
and unmark
methods instead.reset()
method repositions this stream at the last marked
position so that subsequent reads re-read the same bytes.
The readLimit
argument tells this input stream to allow
that many bytes to be read before the mark position gets invalidated.
Negative value for readLimit
may be used in order to invalidate the mark.
This implementation calls mark()
when readLimit >= 0
and
unmark()
otherwise.
public final void reset() throws IllegalStateException
mark
method was last called on this input.
If the mark
method has not been called since
the buffered input was created, then an
IllegalStateException
is thrown.
Otherwise the input is reset to a state such that all the bytes read since the
most recent call to mark
will be resupplied
to subsequent callers of the read
methods, followed by
any bytes that otherwise would have been the next input data as of
the time of the call to reset
.
NOTE: This buffered input continues to be marked.
Use unmark()
after reset if mark on this input is no longer needed.
reset
in class InputStream
IllegalStateException
- if this stream is not markedpublic abstract void rewind(long n) throws IllegalStateException
n
- the number of bytes to rewindIllegalStateException
- if this stream is not marked or if attempting to rewind past the marked positionprotected final void checkRewind(long n)
public long readToOutputStream(OutputStream out, long length) throws IOException
out
- the destination output stream to write tolength
- maximum number of bytes to readIOException
- if an I/O error occurspublic long readToDataOutput(DataOutput out, long length) throws IOException
out
- the destination data output to write tolength
- maximum number of bytes to readIOException
- if an I/O error occurspublic void readToByteBuffer(ByteBuffer buffer) throws IOException
buffer
- the destination byte buffer to write toIOException
Copyright © 2002–2025 Devexperts LLC. All rights reserved.