Package org.htmlparser.lexer
Class Stream
- java.lang.Object
-
- java.io.InputStream
-
- org.htmlparser.lexer.Stream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Runnable
public class Stream extends java.io.InputStream implements java.lang.Runnable
Provides for asynchronous fetching from a stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
BUFFER_SIZE
An initial buffer size.protected static int
EOF
Return value when no more characters are left.int
fills
The number of calls to fill.byte[]
mBuffer
The bytes read so far.protected int
mContentLength
The content length from the HTTP header.protected java.io.InputStream
mIn
The underlying stream.int
mLevel
The number of valid bytes in the buffer.protected int
mMark
The bookmark.protected int
mOffset
The offset of the next byte returned by read().int
reallocations
The number of reallocations.int
synchronous
The number of synchronous (blocking) fills.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.void
close()
Closes this input stream and releases any system resources associated with the stream.protected boolean
fill(boolean force)
Fetch more bytes from the underlying stream.void
mark(int readlimit)
Marks the current position in this input stream.boolean
markSupported()
Tests if this input stream supports themark
andreset
methods.int
read()
Reads the next byte of data from the input stream.void
reset()
Repositions this stream to the position at the time themark
method was last called on this input stream.void
run()
Continually read the underlying stream untill exhausted.
-
-
-
Field Detail
-
fills
public int fills
The number of calls to fill. Note: to be removed.
-
reallocations
public int reallocations
The number of reallocations. Note: to be removed.
-
synchronous
public int synchronous
The number of synchronous (blocking) fills. Note: to be removed.
-
BUFFER_SIZE
protected static final int BUFFER_SIZE
An initial buffer size.- See Also:
- Constant Field Values
-
EOF
protected static final int EOF
Return value when no more characters are left.- See Also:
- Constant Field Values
-
mIn
protected volatile java.io.InputStream mIn
The underlying stream.
-
mBuffer
public volatile byte[] mBuffer
The bytes read so far.
-
mLevel
public volatile int mLevel
The number of valid bytes in the buffer.
-
mOffset
protected int mOffset
The offset of the next byte returned by read().
-
mContentLength
protected int mContentLength
The content length from the HTTP header.
-
mMark
protected int mMark
The bookmark.
-
-
Constructor Detail
-
Stream
public Stream(java.io.InputStream in)
Construct a stream with no assumptions about the number of bytes available.- Parameters:
in
- The input stream to use.
-
Stream
public Stream(java.io.InputStream in, int bytes)
Construct a stream to read the given number of bytes.- Parameters:
in
- The input stream to use.bytes
- The maximum number of bytes to read. This should be set to the ContentLength from the HTTP header. A negative or zero value indicates an unknown number of bytes.
-
-
Method Detail
-
fill
protected boolean fill(boolean force) throws java.io.IOException
Fetch more bytes from the underlying stream. Has no effect if the underlying stream has been drained.- Parameters:
force
- Iftrue
, an attempt is made to read from the underlying stream, even if bytes are available, Iffalse
, a read of the underlying stream will not occur if there are already bytes available.- Returns:
true
if not at the end of the input stream.- Throws:
java.io.IOException
- If the underlying stream read() or available() throws one.
-
run
public void run()
Continually read the underlying stream untill exhausted.- Specified by:
run
in interfacejava.lang.Runnable
- See Also:
Thread.run()
-
read
public int read() throws java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as anint
in the range0
to255
. If no byte is available because the end of the stream has been reached, the value-1
is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.- Specified by:
read
in classjava.io.InputStream
- Returns:
- The next byte of data, or
-1
if the end of the stream is reached. - Throws:
java.io.IOException
- If an I/O error occurs.
-
available
public int available() throws java.io.IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.- Overrides:
available
in classjava.io.InputStream
- Returns:
- The number of bytes that can be read from this input stream without blocking.
- Throws:
java.io.IOException
- If an I/O error occurs.
-
close
public void close() throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
- If an I/O error occurs.
-
reset
public void reset() throws java.io.IOException
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
might 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
may 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 classjava.io.InputStream
- Throws:
java.io.IOException
- Never thrown. Just for subclassers.- See Also:
InputStream.mark(int)
,IOException
- If the method
-
markSupported
public boolean markSupported()
Tests if this input stream supports themark
andreset
methods. Whether or notmark
andreset
are supported is an invariant property of a particular input stream instance. ThemarkSupported
method ofInputStream
returnsfalse
.- Overrides:
markSupported
in classjava.io.InputStream
- Returns:
true
.- See Also:
InputStream.mark(int)
,InputStream.reset()
-
mark
public void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to thereset
method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.The
readlimit
arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.The general contract of
mark
is that, if the methodmarkSupported
returnstrue
, the stream somehow remembers all the bytes read after the call tomark
and stands ready to supply those same bytes again if and whenever the methodreset
is called. However, the stream is not required to remember any data at all if more thanreadlimit
bytes are read from the stream beforereset
is called.- Overrides:
mark
in classjava.io.InputStream
- Parameters:
readlimit
- Not used.- See Also:
InputStream.reset()
-
-