| 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.base.VisibleForTesting; | 7 import org.chromium.base.VisibleForTesting; |
| 8 import org.chromium.net.UploadDataProvider; | 8 import org.chromium.net.UploadDataProvider; |
| 9 import org.chromium.net.UploadDataSink; | 9 import org.chromium.net.UploadDataSink; |
| 10 | 10 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 mContentLength = contentLength; | 66 mContentLength = contentLength; |
| 67 int bufferSize = (int) Math.min(mContentLength, sDefaultBufferLength); | 67 int bufferSize = (int) Math.min(mContentLength, sDefaultBufferLength); |
| 68 mBuffer = ByteBuffer.allocate(bufferSize); | 68 mBuffer = ByteBuffer.allocate(bufferSize); |
| 69 mConnection = connection; | 69 mConnection = connection; |
| 70 mMessageLoop = messageLoop; | 70 mMessageLoop = messageLoop; |
| 71 mBytesWritten = 0; | 71 mBytesWritten = 0; |
| 72 } | 72 } |
| 73 | 73 |
| 74 @Override | 74 @Override |
| 75 public void write(int oneByte) throws IOException { | 75 public void write(int oneByte) throws IOException { |
| 76 checkNotClosed(); |
| 76 checkNotExceedContentLength(1); | 77 checkNotExceedContentLength(1); |
| 77 ensureBufferHasRemaining(); | 78 ensureBufferHasRemaining(); |
| 78 mBuffer.put((byte) oneByte); | 79 mBuffer.put((byte) oneByte); |
| 79 mBytesWritten++; | 80 mBytesWritten++; |
| 80 uploadIfComplete(); | 81 uploadIfComplete(); |
| 81 } | 82 } |
| 82 | 83 |
| 83 @Override | 84 @Override |
| 84 public void write(byte[] buffer, int offset, int count) throws IOException { | 85 public void write(byte[] buffer, int offset, int count) throws IOException { |
| 86 checkNotClosed(); |
| 85 if (buffer.length - offset < count || offset < 0 || count < 0) { | 87 if (buffer.length - offset < count || offset < 0 || count < 0) { |
| 86 throw new IndexOutOfBoundsException(); | 88 throw new IndexOutOfBoundsException(); |
| 87 } | 89 } |
| 88 checkNotExceedContentLength(count); | 90 checkNotExceedContentLength(count); |
| 89 int toSend = count; | 91 int toSend = count; |
| 90 while (toSend > 0) { | 92 while (toSend > 0) { |
| 91 ensureBufferHasRemaining(); | 93 ensureBufferHasRemaining(); |
| 92 int sent = Math.min(toSend, mBuffer.remaining()); | 94 int sent = Math.min(toSend, mBuffer.remaining()); |
| 93 mBuffer.put(buffer, offset + count - toSend, sent); | 95 mBuffer.put(buffer, offset + count - toSend, sent); |
| 94 toSend -= sent; | 96 toSend -= sent; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 119 uploadBufferInternal(); | 121 uploadBufferInternal(); |
| 120 } | 122 } |
| 121 } | 123 } |
| 122 | 124 |
| 123 /** | 125 /** |
| 124 * Helper function to upload {@code mBuffer} to the native stack. This | 126 * Helper function to upload {@code mBuffer} to the native stack. This |
| 125 * function blocks until {@code mBuffer} is consumed and there is space to | 127 * function blocks until {@code mBuffer} is consumed and there is space to |
| 126 * write more data. | 128 * write more data. |
| 127 */ | 129 */ |
| 128 private void uploadBufferInternal() throws IOException { | 130 private void uploadBufferInternal() throws IOException { |
| 131 checkNotClosed(); |
| 129 mBuffer.flip(); | 132 mBuffer.flip(); |
| 130 mMessageLoop.loop(); | 133 mMessageLoop.loop(); |
| 131 } | 134 } |
| 132 | 135 |
| 133 /** | 136 /** |
| 134 * Throws {@link java.net.ProtocolException} if adding {@code numBytes} will | 137 * Throws {@link java.net.ProtocolException} if adding {@code numBytes} will |
| 135 * exceed content length. | 138 * exceed content length. |
| 136 */ | 139 */ |
| 137 private void checkNotExceedContentLength(int numBytes) throws ProtocolExcept
ion { | 140 private void checkNotExceedContentLength(int numBytes) throws ProtocolExcept
ion { |
| 138 if (mBytesWritten + numBytes > mContentLength) { | 141 if (mBytesWritten + numBytes > mContentLength) { |
| 139 throw new ProtocolException("expected " | 142 throw new ProtocolException("expected " |
| 140 + (mContentLength - mBytesWritten) + " bytes but received " | 143 + (mContentLength - mBytesWritten) + " bytes but received " |
| 141 + numBytes); | 144 + numBytes); |
| 142 } | 145 } |
| 143 } | 146 } |
| 144 | 147 |
| 145 // TODO(xunjieli): implement close(). | |
| 146 | |
| 147 // Below are CronetOutputStream implementations: | 148 // Below are CronetOutputStream implementations: |
| 148 | 149 |
| 149 @Override | 150 @Override |
| 150 void setConnected() throws IOException { | 151 void setConnected() throws IOException { |
| 151 // Do nothing. | 152 // Do nothing. |
| 152 } | 153 } |
| 153 | 154 |
| 154 @Override | 155 @Override |
| 155 void checkReceivedEnoughContent() throws IOException { | 156 void checkReceivedEnoughContent() throws IOException { |
| 156 if (mBytesWritten < mContentLength) { | 157 if (mBytesWritten < mContentLength) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 196 } |
| 196 | 197 |
| 197 /** | 198 /** |
| 198 * Sets the default buffer length for use in tests. | 199 * Sets the default buffer length for use in tests. |
| 199 */ | 200 */ |
| 200 @VisibleForTesting | 201 @VisibleForTesting |
| 201 static void setDefaultBufferLengthForTesting(int length) { | 202 static void setDefaultBufferLengthForTesting(int length) { |
| 202 sDefaultBufferLength = length; | 203 sDefaultBufferLength = length; |
| 203 } | 204 } |
| 204 } | 205 } |
| OLD | NEW |