Update expectation to complete writing stuff to request content channel earlier
@@ -111,17 +111,14 @@ class ServletRequestReader implements ReadListener {
public void onDataAvailable() throws IOException {
while (servletInputStream.isReady()) {
final byte[] buffer = new byte[BUFFER_SIZE_BYTES];
- final int numBytesRead = servletInputStream.read(buffer);
+ int numBytesRead;
+
+ synchronized (monitor) {
+ numBytesRead = servletInputStream.read(buffer);
if (numBytesRead < 0) {
// End of stream; there should be no more data available, ever.
return;
}
- writeRequestContent(ByteBuffer.wrap(buffer, 0, numBytesRead));
- }
- }
-
- private void writeRequestContent(final ByteBuffer buf) {
- synchronized (monitor) {
if (state != State.READING) {
//We have a failure, so no point in giving the buffer to the user.
assert finishedFuture.isCompletedExceptionally();
@@ -131,19 +128,22 @@ class ServletRequestReader implements ReadListener {
// - requestContentChannel.write to finish
// - the write completion handler to be called
numberOfOutstandingUserCalls += 2;
+ bytesRead += numBytesRead;
}
+
try {
- int bytesReceived = buf.remaining();
- requestContentChannel.write(buf, writeCompletionHandler);
- metricReporter.successfulRead(bytesReceived);
- bytesRead += bytesReceived;
- } catch (Throwable t) {
+ requestContentChannel.write(ByteBuffer.wrap(buffer, 0, numBytesRead), writeCompletionHandler);
+ metricReporter.successfulRead(numBytesRead);
+ }
+ catch (Throwable t) {
finishedFuture.completeExceptionally(t);
- } finally {
+ }
+ finally {
//decrease due to this method completing.
decreaseOutstandingUserCallsAndCloseRequestContentChannelConditionally();
}
}
+ }
private void decreaseOutstandingUserCallsAndCloseRequestContentChannelConditionally() {
boolean shouldCloseRequestContentChannel;
