Class SkipAvailableInputStream
java.lang.Object
java.io.InputStream
org.refcodes.io.SkipAvailableInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable, Skippable, org.refcodes.mixin.ReadTimeoutMillisAccessor
public class SkipAvailableInputStream
extends InputStream
implements org.refcodes.mixin.ReadTimeoutMillisAccessor, Skippable
The
SkipAvailableInputStream decorates an InputStream with
skip timed functionality, e.g. skip all currently available bytes
(Skippable.skipAvailable()), all bytes available for a period of time
(skipAvailableWithin(long)) or skip all bytes available till a send
pause of a given time is detected (skipAvailableTillSilenceFor(long)
or Skippable.skipAvailableTillSilenceFor(long, long)).
Ehttps://www.metacodes.proly the latter two methods are useful when a send
pause is used by some kind of handshake mechanism. ATTENTION: This
implementation relies upon the available() method to return some
useful information!-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.mixin.ReadTimeoutMillisAccessor
org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisBuilder<B>, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisMutator, org.refcodes.mixin.ReadTimeoutMillisAccessor.ReadTimeoutMillisProperty -
Constructor Summary
ConstructorsConstructorDescriptionSkipAvailableInputStream(InputStream aInputStream) Constructs aSkipAvailableInputStreamdecorating anInputStreamwith additional timeout functionality.SkipAvailableInputStream(InputStream aInputStream, long aTimeoutMillis) Constructs aSkipAvailableInputStreamdecorating anInputStreamwith additional timeout functionality. -
Method Summary
Modifier and TypeMethodDescriptionintReturns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.voidclose()longvoidmark(int readlimit) booleanintread()intread(byte[] aB) intread(byte[] aB, int aOff, int aLen) byte[]intreadNBytes(byte[] b, int off, int len) byte[]readNBytes(int aArg0) voidreset()longskip(long n) Skips over and discardsnbytes of data from this input stream.voidskipAvailableTillSilenceFor(long aSilenceTimeSpanInMs) Skips all available bytes till a given time span of silence is detected: The number of bytes available must be 0 for the given time span for this operation to conclude.voidskipAvailableWithin(long aSkipTimeSpanInMs) Skips all available bytes till after the given time no more bytes are available, else skipping is repeated till no more bytes are available within the given time span.This method blocks until the according available bytes have been skipped.toString()longtransferTo(OutputStream aOut) Methods inherited from class InputStream
nullInputStream, skipNBytesMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Skippable
skipAvailable, skipAvailableExcept, skipAvailableTill, skipAvailableTillSilenceFor, skipAvailableWithin, skipAvailableWithin
-
Constructor Details
-
SkipAvailableInputStream
Constructs aSkipAvailableInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.
-
SkipAvailableInputStream
Constructs aSkipAvailableInputStreamdecorating anInputStreamwith additional timeout functionality.- Parameters:
aInputStream- TheInputStreamto be decorated.aTimeoutMillis- The default timeout for read operations not explicitly called with a timeout argument. With a value of -1 timeout handling is disabled (blocking mode).
-
-
Method Details
-
skipAvailableTillSilenceFor
Skips all available bytes till a given time span of silence is detected: The number of bytes available must be 0 for the given time span for this operation to conclude. This method blocks until the time span of silence has been reached. ATTENTION: To apply a custom timeout, please useSkippable.skipAvailableTillSilenceFor(long, long). In case the read timeout as ofgetReadTimeoutMillis()) is not -1 and the overall time while skipping bytes exceeds the read timeout aTimeoutIOExceptionis thrown. ATTENTION: To apply a custom timeout, please useSkippable.skipAvailableTillSilenceFor(long, long).- Specified by:
skipAvailableTillSilenceForin interfaceSkippable- Parameters:
aSilenceTimeSpanInMs- The time span in milliseconds of silence (0 bytes available) till skipping is terminated.- Throws:
org.refcodes.exception.TimeoutIOException- in case the the read timeout is not -1 as ofgetReadTimeoutMillis()and the overall time while skipping bytes exceeds the read timeout .IOException- thrown in case of I/O issues.
-
skipAvailableWithin
Skips all available bytes till after the given time no more bytes are available, else skipping is repeated till no more bytes are available within the given time span.This method blocks until the according available bytes have been skipped. ATTENTION: To apply a custom timeout, please useSkippable.skipAvailableWithin(long, long). In case the read timeout as ofgetReadTimeoutMillis()) is not -1 and the overall time while skipping bytes exceeds the read timeout aTimeoutIOExceptionis thrown. ATTENTION: To apply a custom timeout, please useSkippable.skipAvailableWithin(long, long).- Specified by:
skipAvailableWithinin interfaceSkippable- Parameters:
aSkipTimeSpanInMs- the skip time span in ms- Throws:
org.refcodes.exception.TimeoutIOException- in case the the read timeout is not -1 as ofgetReadTimeoutMillis()and the overall time while skipping bytes exceeds the read timeout .IOException- thrown in case of I/O issues.
-
available
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. Note that while some implementations as of theInputStreamwill return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream. A subclass's implementation of this method may choose to throw anIOExceptionif this input stream has been closed. Theavailablemethod ofInputStreamalways returns0.- Specified by:
availablein interfaceSkippable- Overrides:
availablein classInputStream- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking or
0when it reaches the end of the input stream. - Throws:
IOException- if an I/O error occurs.
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
readAllBytes
- Overrides:
readAllBytesin classInputStream- Throws:
IOException
-
readNBytes
- Overrides:
readNBytesin classInputStream- Throws:
IOException
-
readNBytes
- Overrides:
readNBytesin classInputStream- Throws:
IOException
-
toString
-
transferTo
- Overrides:
transferToin classInputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
markin classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
reset
- Overrides:
resetin classInputStream- Throws:
IOException
-
skip
Skips over and discardsnbytes of data from this input stream. Theskipmethod may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly0. This may result from any of a number of conditions; reaching end of file beforenbytes have been skipped is only one possibility. The actual number of bytes skipped is returned. Ifnis negative, theskipmethod for classInputStreamalways returns 0, and no bytes are skipped. Subclasses may handle the negative value differently. Theskipmethod implementation of this class creates a byte array and then repeatedly reads into it untilnbytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. For instance, the implementation may depend on the ability to seek.- Specified by:
skipin interfaceSkippable- Overrides:
skipin classInputStream- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped which might be zero.
- Throws:
IOException- if an I/O error occurs.
-
getReadTimeoutMillis
public long getReadTimeoutMillis()- Specified by:
getReadTimeoutMillisin interfaceorg.refcodes.mixin.ReadTimeoutMillisAccessor
-