java.lang.Object
java.io.InputStream
org.bzdev.io.CounterInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
An input stream with a byte count.
This class provides an implementation of InputStream with
a byte counter giving the current position in the input stream.
One use for this class is for software instrumentation during
development (e.g., to empirically determine buffer sizes).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
long
getCount()
Get the current byte count.void
mark
(int readlimit) boolean
int
read()
int
read
(byte[] b) int
read
(byte[] b, int off, int len) void
reset()
Repositions this stream to the position at the time themark
method was last called on this input stream.long
skip
(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
Constructor Details
-
CounterInputStream
Constructor.- Parameters:
is
- the input stream to count.
-
-
Method Details
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
mark
in classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classInputStream
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
reset
Repositions this stream to the position at the time themark
method was last called on this input stream.The general contract of
reset
is:- If the method
markSupported
returnstrue
, then:- If the method
mark
has not been called since the stream was created, or the number of bytes read from the stream sincemark
was last called is larger than the argument tomark
at that last call, then anIOException
will be thrown. - If such an
IOException
is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call tomark
(or since the start of the file, ifmark
has not been called) will be resupplied to subsequent callers of theread
method, followed by any bytes that otherwise would have been the next input data as of the time of the call toreset
.
- If the method
- If the method
markSupported
returnsfalse
, then:- The call to
reset
will throw anIOException
. - If an
IOException
is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of theread
method depend on the particular type of the input stream.
- The call to
- Overrides:
reset
in classInputStream
- Throws:
IOException
- the read limit has been exceeded or if the mark is not supported or set.
- If the method
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
getCount
public long getCount()Get the current byte count. This returns the number of bytes that have been read and skipped.- Returns:
- the number of bytes read so far.
-