1package com.squareup.okhttp.internal;
2
3import java.io.IOException;
4import okio.Buffer;
5import okio.ForwardingSink;
6import okio.Sink;
7
8/** A sink that never throws IOExceptions, even if the underlying sink does. */
9class FaultHidingSink extends ForwardingSink {
10  private boolean hasErrors;
11
12  public FaultHidingSink(Sink delegate) {
13    super(delegate);
14  }
15
16  @Override public void write(Buffer source, long byteCount) throws IOException {
17    if (hasErrors) {
18      source.skip(byteCount);
19      return;
20    }
21    try {
22      super.write(source, byteCount);
23    } catch (IOException e) {
24      hasErrors = true;
25      onException(e);
26    }
27  }
28
29  @Override public void flush() throws IOException {
30    if (hasErrors) return;
31    try {
32      super.flush();
33    } catch (IOException e) {
34      hasErrors = true;
35      onException(e);
36    }
37  }
38
39  @Override public void close() throws IOException {
40    if (hasErrors) return;
41    try {
42      super.close();
43    } catch (IOException e) {
44      hasErrors = true;
45      onException(e);
46    }
47  }
48
49  protected void onException(IOException e) {
50  }
51}
52