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!
  • Constructor Details

    • SkipAvailableInputStream

      public SkipAvailableInputStream(InputStream aInputStream)
      Constructs a SkipAvailableInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream to be decorated.
    • SkipAvailableInputStream

      public SkipAvailableInputStream(InputStream aInputStream, long aTimeoutMillis)
      Constructs a SkipAvailableInputStream decorating an InputStream with additional timeout functionality.
      Parameters:
      aInputStream - The InputStream to 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

      public void skipAvailableTillSilenceFor(long aSilenceTimeSpanInMs) throws IOException
      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 use Skippable.skipAvailableTillSilenceFor(long, long). In case the read timeout as of getReadTimeoutMillis()) is not -1 and the overall time while skipping bytes exceeds the read timeout a TimeoutIOException is thrown. ATTENTION: To apply a custom timeout, please use Skippable.skipAvailableTillSilenceFor(long, long).
      Specified by:
      skipAvailableTillSilenceFor in interface Skippable
      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 of getReadTimeoutMillis() and the overall time while skipping bytes exceeds the read timeout .
      IOException - thrown in case of I/O issues.
    • skipAvailableWithin

      public void skipAvailableWithin(long aSkipTimeSpanInMs) throws IOException
      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 use Skippable.skipAvailableWithin(long, long). In case the read timeout as of getReadTimeoutMillis()) is not -1 and the overall time while skipping bytes exceeds the read timeout a TimeoutIOException is thrown. ATTENTION: To apply a custom timeout, please use Skippable.skipAvailableWithin(long, long).
      Specified by:
      skipAvailableWithin in interface Skippable
      Parameters:
      aSkipTimeSpanInMs - the skip time span in ms
      Throws:
      org.refcodes.exception.TimeoutIOException - in case the the read timeout is not -1 as of getReadTimeoutMillis() and the overall time while skipping bytes exceeds the read timeout .
      IOException - thrown in case of I/O issues.
    • available

      public int available() throws IOException
      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 the InputStream will 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 an IOException if this input stream has been closed. The available method of InputStream always returns 0.
      Specified by:
      available in interface Skippable
      Overrides:
      available in class 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:
      IOException - if an I/O error occurs.
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] aB, int aOff, int aLen) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] aB) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • readAllBytes

      public byte[] readAllBytes() throws IOException
      Overrides:
      readAllBytes in class InputStream
      Throws:
      IOException
    • readNBytes

      public int readNBytes(byte[] b, int off, int len) throws IOException
      Overrides:
      readNBytes in class InputStream
      Throws:
      IOException
    • readNBytes

      public byte[] readNBytes(int aArg0) throws IOException
      Overrides:
      readNBytes in class InputStream
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • transferTo

      public long transferTo(OutputStream aOut) throws IOException
      Overrides:
      transferTo in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, the skip method for class InputStream always returns 0, and no bytes are skipped. Subclasses may handle the negative value differently. The skip method implementation of this class creates a byte array and then repeatedly reads into it until n bytes 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:
      skip in interface Skippable
      Overrides:
      skip in class InputStream
      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:
      getReadTimeoutMillis in interface org.refcodes.mixin.ReadTimeoutMillisAccessor