Package okio
Class AsyncTimeout
- java.lang.Object
-
- okio.Timeout
-
- okio.AsyncTimeout
-
public class AsyncTimeout extends Timeout
This timeout uses a background thread to take action exactly when the timeout occurs. Use this to implement timeouts where they aren't supported natively, such as to sockets that are blocked on writing.Subclasses should override
timedOut()
to take action when a timeout occurs. This method will be invoked by the shared watchdog thread so it should not do any long-running operations. Otherwise we risk starving other timeouts from being triggered.Use
sink(okio.Sink)
andsource(okio.Source)
to apply this timeout to a stream. The returned value will apply the timeout to each operation on the wrapped stream.Callers should call
enter()
before doing work that is subject to timeouts, andexit()
afterwards. The return value ofexit()
indicates whether a timeout was triggered. Note that the call totimedOut()
is asynchronous, and may be called afterexit()
.
-
-
Constructor Summary
Constructors Constructor Description AsyncTimeout()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
enter()
boolean
exit()
Returns true if the timeout occurred.protected java.io.IOException
newTimeoutException(java.io.IOException cause)
Returns anIOException
to represent a timeout.Sink
sink(Sink sink)
Returns a new sink that delegates tosink
, using this to implement timeouts.Source
source(Source source)
Returns a new source that delegates tosource
, using this to implement timeouts.protected void
timedOut()
-
Methods inherited from class okio.Timeout
clearDeadline, clearTimeout, deadline, deadlineNanoTime, deadlineNanoTime, hasDeadline, throwIfReached, timeout, timeoutNanos, waitUntilNotified
-
-
-
-
Method Detail
-
enter
public final void enter()
-
exit
public final boolean exit()
Returns true if the timeout occurred.
-
timedOut
protected void timedOut()
-
sink
public final Sink sink(Sink sink)
Returns a new sink that delegates tosink
, using this to implement timeouts. This works best iftimedOut()
is overridden to interruptsink
's current operation.
-
source
public final Source source(Source source)
Returns a new source that delegates tosource
, using this to implement timeouts. This works best iftimedOut()
is overridden to interruptsink
's current operation.
-
newTimeoutException
protected java.io.IOException newTimeoutException(@Nullable java.io.IOException cause)
Returns anIOException
to represent a timeout. By default this method returnsInterruptedIOException
. Ifcause
is non-null it is set as the cause of the returned exception.
-
-