Package com.squareup.okhttp.internal
Class FaultRecoveringOutputStream
java.lang.Object
java.io.OutputStream
com.squareup.okhttp.internal.AbstractOutputStream
com.squareup.okhttp.internal.FaultRecoveringOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public abstract class FaultRecoveringOutputStream extends AbstractOutputStream
An output stream wrapper that recovers from failures in the underlying stream
by replacing it with another stream. This class buffers a fixed amount of
data under the assumption that failures occur early in a stream's life.
If a failure occurs after the buffer has been exhausted, no recovery is
attempted.
Subclasses must override replacementStream(java.io.IOException) which will request a
replacement stream each time an IOException is encountered on the
current stream.
-
Field Summary
Fields inherited from class com.squareup.okhttp.internal.AbstractOutputStream
closed -
Constructor Summary
Constructors Constructor Description FaultRecoveringOutputStream(int maxReplayBufferLength, OutputStream out) -
Method Summary
Modifier and Type Method Description voidclose()Closes this stream.voidflush()Flushes this stream.booleanisRecoverable()Returns true if errors in the underlying stream can currently be recovered.protected abstract OutputStreamreplacementStream(IOException e)Returns a replacement output stream to recover fromethrown by the previous stream.voidreplaceStream(OutputStream replacementStream)Replaces the current output stream withreplacementStream, writing any replay bytes to it if they exist.voidwrite(byte[] buffer, int offset, int count)Writescountbytes from the byte arraybufferstarting at positionoffsetto this stream.Methods inherited from class com.squareup.okhttp.internal.AbstractOutputStream
checkNotClosed, isClosed, writeMethods inherited from class java.io.OutputStream
write
-
Constructor Details
-
FaultRecoveringOutputStream
- Parameters:
maxReplayBufferLength- the maximum number of successfully written bytes to buffer so they can be replayed in the event of an error. Failure recoveries are not possible once this limit has been exceeded.
-
-
Method Details
-
write
Description copied from class:OutputStreamWritescountbytes from the byte arraybufferstarting at positionoffsetto this stream.- Overrides:
writein classOutputStream- Parameters:
buffer- the buffer to be written.offset- the start position inbufferfrom where to get bytes.count- the number of bytes frombufferto write to this stream.- Throws:
IOException- if an error occurs while writing to this stream.
-
flush
Description copied from class:OutputStreamFlushes this stream. Implementations of this method should ensure that any buffered data is written out. This implementation does nothing.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an error occurs while flushing this stream.
-
close
Description copied from class:OutputStreamCloses this stream. Implementations of this method should free any resources used by the stream. This implementation does nothing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an error occurs while closing this stream.
-
isRecoverable
public boolean isRecoverable()Returns true if errors in the underlying stream can currently be recovered. -
replaceStream
Replaces the current output stream withreplacementStream, writing any replay bytes to it if they exist. The current output stream is closed.- Throws:
IOException
-
replacementStream
Returns a replacement output stream to recover fromethrown by the previous stream. Returns a new OutputStream if recovery was successful, in which case all previously-written data will be replayed. Returns null if the failure cannot be recovered.- Throws:
IOException
-