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.

  • Constructor Details

    • FaultRecoveringOutputStream

      public FaultRecoveringOutputStream​(int maxReplayBufferLength, OutputStream out)
      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

      public final void write​(byte[] buffer, int offset, int count) throws IOException
      Description copied from class: OutputStream
      Writes count bytes from the byte array buffer starting at position offset to this stream.
      Overrides:
      write in class OutputStream
      Parameters:
      buffer - the buffer to be written.
      offset - the start position in buffer from where to get bytes.
      count - the number of bytes from buffer to write to this stream.
      Throws:
      IOException - if an error occurs while writing to this stream.
    • flush

      public final void flush() throws IOException
      Description copied from class: OutputStream
      Flushes this stream. Implementations of this method should ensure that any buffered data is written out. This implementation does nothing.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - if an error occurs while flushing this stream.
    • close

      public final void close() throws IOException
      Description copied from class: OutputStream
      Closes this stream. Implementations of this method should free any resources used by the stream. This implementation does nothing.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      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

      public final void replaceStream​(OutputStream replacementStream) throws IOException
      Replaces the current output stream with replacementStream, writing any replay bytes to it if they exist. The current output stream is closed.
      Throws:
      IOException
    • replacementStream

      protected abstract OutputStream replacementStream​(IOException e) throws IOException
      Returns a replacement output stream to recover from e thrown 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