• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.urlconnection;
6 
7 import org.chromium.net.UploadDataProvider;
8 
9 import androidx.annotation.VisibleForTesting;
10 
11 import java.io.IOException;
12 import java.io.OutputStream;
13 
14 /**
15  * An abstract class of {@link OutputStream} that concrete implementations must
16  * extend in order to be used in {@link CronetHttpURLConnection}.
17  */
18 @VisibleForTesting
19 public abstract class CronetOutputStream extends OutputStream {
20     private IOException mException;
21     private boolean mClosed;
22     private boolean mRequestCompleted;
23 
24     @Override
close()25     public void close() throws IOException {
26         mClosed = true;
27     }
28 
29     /**
30      * Tells the underlying implementation that connection has been established.
31      * Used in {@link CronetHttpURLConnection}.
32      */
setConnected()33     abstract void setConnected() throws IOException;
34 
35     /**
36      * Checks whether content received is less than Content-Length.
37      * Used in {@link CronetHttpURLConnection}.
38      */
checkReceivedEnoughContent()39     abstract void checkReceivedEnoughContent() throws IOException;
40 
41     /** Returns {@link UploadDataProvider} implementation. */
getUploadDataProvider()42     abstract UploadDataProvider getUploadDataProvider();
43 
44     /**
45      * Signals that the request is done. If there is no error,
46      * {@code exception} is null. Used by {@link CronetHttpURLConnection}.
47      */
setRequestCompleted(IOException exception)48     void setRequestCompleted(IOException exception) {
49         mException = exception;
50         mRequestCompleted = true;
51     }
52 
53     /** Throws an IOException if the stream is closed or the request is done. */
checkNotClosed()54     protected void checkNotClosed() throws IOException {
55         if (mRequestCompleted) {
56             checkNoException();
57             throw new IOException("Writing after request completed.");
58         }
59         if (mClosed) {
60             throw new IOException("Stream has been closed.");
61         }
62     }
63 
64     /**
65      * Throws the same IOException that the request is failed with. If there
66      * is no exception reported, this method is no-op.
67      */
checkNoException()68     protected void checkNoException() throws IOException {
69         if (mException != null) {
70             throw mException;
71         }
72     }
73 }
74