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 import org.chromium.net.UploadDataSink; | 8 import org.chromium.net.UploadDataSink; |
9 | 9 |
10 import java.io.IOException; | 10 import java.io.IOException; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 | 65 |
66 mConnection = connection; | 66 mConnection = connection; |
67 mInitialContentLength = -1; | 67 mInitialContentLength = -1; |
68 // Buffering without knowing content-length. | 68 // Buffering without knowing content-length. |
69 mBuffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE); | 69 mBuffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE); |
70 } | 70 } |
71 | 71 |
72 @Override | 72 @Override |
73 public void write(int oneByte) throws IOException { | 73 public void write(int oneByte) throws IOException { |
| 74 checkNotClosed(); |
74 ensureCanWrite(1); | 75 ensureCanWrite(1); |
75 mBuffer.put((byte) oneByte); | 76 mBuffer.put((byte) oneByte); |
76 } | 77 } |
77 | 78 |
78 @Override | 79 @Override |
79 public void write(byte[] buffer, int offset, int count) throws IOException { | 80 public void write(byte[] buffer, int offset, int count) throws IOException { |
| 81 checkNotClosed(); |
80 ensureCanWrite(count); | 82 ensureCanWrite(count); |
81 mBuffer.put(buffer, offset, count); | 83 mBuffer.put(buffer, offset, count); |
82 } | 84 } |
83 | 85 |
84 // TODO(xunjieli): implement close(). | |
85 | |
86 /** | 86 /** |
87 * Ensures that {@code count} bytes can be written to the internal buffer. | 87 * Ensures that {@code count} bytes can be written to the internal buffer. |
88 */ | 88 */ |
89 private void ensureCanWrite(int count) throws IOException { | 89 private void ensureCanWrite(int count) throws IOException { |
90 if (mInitialContentLength != -1 | 90 if (mInitialContentLength != -1 |
91 && mBuffer.position() + count > mInitialContentLength) { | 91 && mBuffer.position() + count > mInitialContentLength) { |
92 // Error message is to match that of the default implementation. | 92 // Error message is to match that of the default implementation. |
93 throw new ProtocolException("exceeded content-length limit of " | 93 throw new ProtocolException("exceeded content-length limit of " |
94 + mInitialContentLength + " bytes"); | 94 + mInitialContentLength + " bytes"); |
95 } | 95 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 uploadDataSink.onReadSucceeded(false); | 164 uploadDataSink.onReadSucceeded(false); |
165 } | 165 } |
166 | 166 |
167 @Override | 167 @Override |
168 public void rewind(UploadDataSink uploadDataSink) { | 168 public void rewind(UploadDataSink uploadDataSink) { |
169 mBuffer.position(0); | 169 mBuffer.position(0); |
170 uploadDataSink.onRewindSucceeded(); | 170 uploadDataSink.onRewindSucceeded(); |
171 } | 171 } |
172 } | 172 } |
173 } | 173 } |
OLD | NEW |