Package jexer.io
Class TimeoutInputStream
- java.lang.Object
-
- java.io.InputStream
-
- jexer.io.TimeoutInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class TimeoutInputStream extends java.io.InputStreamThis class provides an optional millisecond timeout on its read() operations. This permits callers to bail out rather than block.
-
-
Constructor Summary
Constructors Constructor Description TimeoutInputStream(java.io.InputStream stream)Public constructor, at the default timeout of 10000 millis (10 seconds).TimeoutInputStream(java.io.InputStream stream, int timeoutMillis)Public constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.voidcancelRead()Request that the current read() operation timeout immediately.voidclose()Closes this input stream and releases any system resources associated with the stream.java.io.InputStreamgetStream()Get the underlying stream.voidmark(int readLimit)Marks the current position in this input stream.booleanmarkSupported()Tests if this input stream supports the mark and reset methods.intread()Reads the next byte of data from the input stream.intread(byte[] b)Reads some number of bytes from the input stream and stores them into the buffer array b.intread(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into an array of bytes.voidreset()Repositions this stream to the position at the time the mark method was last called on this input stream.longskip(long n)Skips over and discards n bytes of data from this input stream.
-
-
-
Constructor Detail
-
TimeoutInputStream
public TimeoutInputStream(java.io.InputStream stream)
Public constructor, at the default timeout of 10000 millis (10 seconds).- Parameters:
stream- the wrapped InputStream
-
TimeoutInputStream
public TimeoutInputStream(java.io.InputStream stream, int timeoutMillis)Public constructor.- Parameters:
stream- the wrapped InputStreamtimeoutMillis- the timeout value in millis. If it takes longer than this for bytes to be available for read then a ReadTimeoutException is thrown. A value of 0 means to block as a normal InputStream would.
-
-
Method Detail
-
read
public int read() throws java.io.IOExceptionReads the next byte of data from the input stream.- Specified by:
readin classjava.io.InputStream- Returns:
- the next byte of data, or -1 if there is no more data because the end of the stream has been reached.
- Throws:
java.io.IOException- if an I/O error occurs
-
read
public int read(byte[] b) throws java.io.IOExceptionReads some number of bytes from the input stream and stores them into the buffer array b.- Overrides:
readin classjava.io.InputStream- Parameters:
b- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- Throws:
java.io.IOException- if an I/O error occurs
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionReads up to len bytes of data from the input stream into an array of bytes.- Overrides:
readin classjava.io.InputStream- Parameters:
b- the buffer into which the data is read.off- the start offset in array b at which the data is written.len- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
- Throws:
java.io.IOException- if an I/O error occurs
-
available
public int available() throws java.io.IOExceptionReturns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.- Overrides:
availablein classjava.io.InputStream- Returns:
- an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.
- Throws:
java.io.IOException- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionCloses this input stream and releases any system resources associated with the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error occurs
-
mark
public void mark(int readLimit)
Marks the current position in this input stream.- Overrides:
markin classjava.io.InputStream- Parameters:
readLimit- the maximum limit of bytes that can be read before the mark position becomes invalid
-
markSupported
public boolean markSupported()
Tests if this input stream supports the mark and reset methods.- Overrides:
markSupportedin classjava.io.InputStream- Returns:
- true if this stream instance supports the mark and reset methods; false otherwise
-
reset
public void reset() throws java.io.IOExceptionRepositions this stream to the position at the time the mark method was last called on this input stream.- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error occurs
-
skip
public long skip(long n) throws java.io.IOExceptionSkips over and discards n bytes of data from this input stream.- Overrides:
skipin classjava.io.InputStream- Parameters:
n- the number of bytes to be skipped- Returns:
- the actual number of bytes skipped
- Throws:
java.io.IOException- if an I/O error occurs
-
cancelRead
public void cancelRead()
Request that the current read() operation timeout immediately.
-
getStream
public java.io.InputStream getStream()
Get the underlying stream.- Returns:
- the stream
-
-