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