You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/02/20 22:16:21 UTC
svn commit: r1448420 [1/4] - in /httpcomponents/httpcore/trunk: ./
httpcore-ab/src/main/java/org/apache/http/benchmark/
httpcore-nio/src/main/java/org/apache/http/impl/nio/
httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/
httpcore-nio/src/ma...
Author: olegk
Date: Wed Feb 20 21:16:19 2013
New Revision: 1448420
URL: http://svn.apache.org/r1448420
Log:
Reduced TCP packet fragmentation when writing out message content with non-blocking content codecs
Added:
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ByteChannelMock.java
- copied, changed from r1444641, httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java (with props)
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpClientConnection.java (with props)
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpServerConnection.java (with props)
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestNHttpConnectionBase.java (with props)
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/CodecTestUtils.java (with props)
Modified:
httpcomponents/httpcore/trunk/.gitignore
httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnectionFactory.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnectionFactory.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/NHttpConnectionBase.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpClientConnectionFactory.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpServerConnectionFactory.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionInputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionOutputBufferImpl.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestIdentityDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestIdentityEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestLengthDelimitedDecoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestLengthDelimitedEncoder.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/ConnectionConfig.java
Modified: httpcomponents/httpcore/trunk/.gitignore
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/.gitignore?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/.gitignore (original)
+++ httpcomponents/httpcore/trunk/.gitignore Wed Feb 20 21:16:19 2013
@@ -2,5 +2,6 @@
.project
.settings
.pydevproject
+.clover
target
maven-eclipse.xml
Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Wed Feb 20 21:16:19 2013
@@ -1,6 +1,10 @@
Changes since 4.3-ALPHA1
-------------------
+* Reduced TCP packet fragmentation when writing out message content with non-blocking content
+ codecs.
+ Contributed by Oleg Kalnichevski <olegk at apache.org>
+
* [HTTPCORE-331] BasicFuture no longer executes notification callbacks inside a synchronized block.
Contributed by Oleg Kalnichevski <olegk at apache.org>
Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/Config.java Wed Feb 20 21:16:19 2013
@@ -247,4 +247,33 @@ public class Config {
public void setIdentityStorePassword(final String identityStorePassword) {
this.identityStorePassword = identityStorePassword;
}
+
+ public Config copy() {
+ final Config copy = new Config();
+ copy.url = this.url;
+ copy.requests = this.requests;
+ copy.threads = this.threads;
+ copy.keepAlive = this.keepAlive;
+ copy.verbosity = this.verbosity;
+ copy.headInsteadOfGet = this.headInsteadOfGet;
+ copy.useHttp1_0 = this.useHttp1_0;
+ copy.contentType = this.contentType;
+ copy.headers = this.headers;
+ copy.socketTimeout = this.socketTimeout;
+ copy.method = this.method;
+ copy.useChunking = this.useChunking;
+ copy.useExpectContinue = this.useExpectContinue;
+ copy.useAcceptGZip = this.useAcceptGZip;
+ copy.payloadFile = this.payloadFile;
+ copy.payloadText = this.payloadText;
+ copy.soapAction = this.soapAction;
+
+ copy.disableSSLVerification = this.disableSSLVerification;
+ copy.trustStorePath = this.trustStorePath;
+ copy.identityStorePath = this.identityStorePath;
+ copy.trustStorePassword = this.trustStorePassword;
+ copy.identityStorePassword = this.identityStorePassword;
+ return copy;
+ }
+
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java Wed Feb 20 21:16:19 2013
@@ -110,6 +110,7 @@ public class DefaultNHttpClientConnectio
*
* @param session the underlying I/O session.
* @param buffersize buffer size. Must be a positive number.
+ * @param fragmentSizeHint fragment size hint.
* @param allocator memory allocator.
* If <code>null</code> {@link HeapByteBufferAllocator#INSTANCE} will be used.
* @param chardecoder decoder to be used for decoding HTTP protocol elements.
@@ -128,6 +129,7 @@ public class DefaultNHttpClientConnectio
public DefaultNHttpClientConnection(
final IOSession session,
final int buffersize,
+ final int fragmentSizeHint,
final ByteBufferAllocator allocator,
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
@@ -136,8 +138,8 @@ public class DefaultNHttpClientConnectio
final ContentLengthStrategy outgoingContentStrategy,
final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
final NHttpMessageParserFactory<HttpResponse> responseParserFactory) {
- super(session, buffersize, allocator, chardecoder, charencoder, constraints,
- incomingContentStrategy, outgoingContentStrategy);
+ super(session, buffersize, fragmentSizeHint, allocator, chardecoder, charencoder,
+ constraints, incomingContentStrategy, outgoingContentStrategy);
this.requestWriter = (requestWriterFactory != null ? requestWriterFactory :
DefaultHttpRequestWriterFactory.INSTANCE).create(this.outbuf);
this.responseParser = (responseParserFactory != null ? responseParserFactory :
@@ -153,7 +155,7 @@ public class DefaultNHttpClientConnectio
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
final MessageConstraints constraints) {
- this(session, buffersize, null, chardecoder, charencoder, constraints,
+ this(session, buffersize, buffersize, null, chardecoder, charencoder, constraints,
null, null, null, null);
}
@@ -161,7 +163,7 @@ public class DefaultNHttpClientConnectio
* @since 4.3
*/
public DefaultNHttpClientConnection(final IOSession session, final int buffersize) {
- this(session, buffersize, null, null, null, null, null, null, null, null);
+ this(session, buffersize, buffersize, null, null, null, null, null, null, null, null);
}
/**
@@ -280,6 +282,17 @@ public class DefaultNHttpClientConnectio
public void produceOutput(final NHttpClientEventHandler handler) {
try {
+ if (this.status == ACTIVE) {
+ if (this.contentEncoder == null) {
+ handler.requestReady(this);
+ }
+ if (this.contentEncoder != null) {
+ handler.outputReady(this, this.contentEncoder);
+ if (this.contentEncoder.isCompleted()) {
+ resetOutput();
+ }
+ }
+ }
if (this.outbuf.hasData()) {
final int bytesWritten = this.outbuf.flush(this.session.channel());
if (bytesWritten > 0) {
@@ -291,31 +304,15 @@ public class DefaultNHttpClientConnectio
this.session.close();
this.status = CLOSED;
resetOutput();
- return;
- } else {
- if (this.contentEncoder != null) {
- handler.outputReady(this, this.contentEncoder);
- if (this.contentEncoder.isCompleted()) {
- resetOutput();
- }
- }
}
-
- if (this.contentEncoder == null && !this.outbuf.hasData()) {
- if (this.status == CLOSING) {
- this.session.close();
- this.status = CLOSED;
- }
- if (this.status != CLOSED) {
- this.session.clearEvent(EventMask.WRITE);
- handler.requestReady(this);
- }
+ if (this.contentEncoder == null && this.status != CLOSED) {
+ this.session.clearEvent(EventMask.WRITE);
}
}
} catch (final Exception ex) {
handler.exception(this, ex);
} finally {
- // Finally set buffered output flag
+ // Finally set the buffered output flag
this.hasBufferedOutput = this.outbuf.hasData();
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnectionFactory.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnectionFactory.java Wed Feb 20 21:16:19 2013
@@ -139,7 +139,9 @@ public class DefaultNHttpClientConnectio
charencoder.onUnmappableCharacter(unmappableInputAction);
}
return new DefaultNHttpClientConnection(
- session, 8 * 1024,
+ session,
+ this.config.getBufferSize(),
+ this.config.getFragmentSizeHint(),
this.allocator,
chardecoder, charencoder, this.config.getMessageConstraints(),
null, null, null,
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java Wed Feb 20 21:16:19 2013
@@ -108,6 +108,7 @@ public class DefaultNHttpServerConnectio
*
* @param session the underlying I/O session.
* @param buffersize buffer size. Must be a positive number.
+ * @param fragmentSizeHint fragment size hint.
* @param allocator memory allocator.
* If <code>null</code> {@link HeapByteBufferAllocator#INSTANCE} will be used.
* @param chardecoder decoder to be used for decoding HTTP protocol elements.
@@ -130,6 +131,7 @@ public class DefaultNHttpServerConnectio
public DefaultNHttpServerConnection(
final IOSession session,
final int buffersize,
+ final int fragmentSizeHint,
final ByteBufferAllocator allocator,
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
@@ -138,9 +140,12 @@ public class DefaultNHttpServerConnectio
final ContentLengthStrategy outgoingContentStrategy,
final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
- super(session, buffersize, allocator, chardecoder, charencoder, constraints,
+ super(session, buffersize, fragmentSizeHint, allocator, chardecoder, charencoder,
+ constraints,
incomingContentStrategy != null ? incomingContentStrategy :
- DisallowIdentityContentLengthStrategy.INSTANCE, outgoingContentStrategy);
+ DisallowIdentityContentLengthStrategy.INSTANCE,
+ outgoingContentStrategy != null ? outgoingContentStrategy :
+ StrictContentLengthStrategy.INSTANCE);
this.requestParser = (requestParserFactory != null ? requestParserFactory :
DefaultHttpRequestParserFactory.INSTANCE).create(this.inbuf, constraints);
this.responseWriter = (responseWriterFactory != null ? responseWriterFactory :
@@ -156,7 +161,7 @@ public class DefaultNHttpServerConnectio
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
final MessageConstraints constraints) {
- this(session, buffersize, null, chardecoder, charencoder, constraints,
+ this(session, buffersize, buffersize, null, chardecoder, charencoder, constraints,
null, null, null, null);
}
@@ -164,7 +169,7 @@ public class DefaultNHttpServerConnectio
* @since 4.3
*/
public DefaultNHttpServerConnection(final IOSession session, final int buffersize) {
- this(session, buffersize, null, null, null, null, null, null, null, null);
+ this(session, buffersize, buffersize, null, null, null, null, null, null, null, null);
}
/**
@@ -295,6 +300,17 @@ public class DefaultNHttpServerConnectio
public void produceOutput(final NHttpServerEventHandler handler) {
try {
+ if (this.status == ACTIVE) {
+ if (this.contentEncoder == null) {
+ handler.responseReady(this);
+ }
+ if (this.contentEncoder != null) {
+ handler.outputReady(this, this.contentEncoder);
+ if (this.contentEncoder.isCompleted()) {
+ resetOutput();
+ }
+ }
+ }
if (this.outbuf.hasData()) {
final int bytesWritten = this.outbuf.flush(this.session.channel());
if (bytesWritten > 0) {
@@ -306,25 +322,9 @@ public class DefaultNHttpServerConnectio
this.session.close();
this.status = CLOSED;
resetOutput();
- return;
- } else {
- if (this.contentEncoder != null) {
- handler.outputReady(this, this.contentEncoder);
- if (this.contentEncoder.isCompleted()) {
- resetOutput();
- }
- }
}
-
- if (this.contentEncoder == null && !this.outbuf.hasData()) {
- if (this.status == CLOSING) {
- this.session.close();
- this.status = CLOSED;
- }
- if (this.status != CLOSED) {
- this.session.clearEvent(EventMask.WRITE);
- handler.responseReady(this);
- }
+ if (this.contentEncoder == null && this.status != CLOSED) {
+ this.session.clearEvent(EventMask.WRITE);
}
}
} catch (final Exception ex) {
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnectionFactory.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnectionFactory.java Wed Feb 20 21:16:19 2013
@@ -138,7 +138,9 @@ public class DefaultNHttpServerConnectio
charencoder.onMalformedInput(malformedInputAction);
charencoder.onUnmappableCharacter(unmappableInputAction);
}
- return new DefaultNHttpServerConnection(session, 8 * 1024,
+ return new DefaultNHttpServerConnection(session,
+ this.config.getBufferSize(),
+ this.config.getFragmentSizeHint(),
this.allocator,
chardecoder, charencoder, this.config.getMessageConstraints(),
null, null,
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/NHttpConnectionBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/NHttpConnectionBase.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/NHttpConnectionBase.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/NHttpConnectionBase.java Wed Feb 20 21:16:19 2013
@@ -102,6 +102,7 @@ public class NHttpConnectionBase
protected final SessionInputBufferImpl inbuf;
protected final SessionOutputBufferImpl outbuf;
+ private final int fragmentSizeHint;
protected final HttpTransportMetricsImpl inTransportMetrics;
protected final HttpTransportMetricsImpl outTransportMetrics;
@@ -164,6 +165,7 @@ public class NHttpConnectionBase
}
this.inbuf = new SessionInputBufferImpl(buffersize, linebuffersize, decoder, allocator);
this.outbuf = new SessionOutputBufferImpl(buffersize, linebuffersize, encoder, allocator);
+ this.fragmentSizeHint = buffersize;
this.incomingContentStrategy = createIncomingContentStrategy();
this.outgoingContentStrategy = createOutgoingContentStrategy();
@@ -183,6 +185,7 @@ public class NHttpConnectionBase
*
* @param session the underlying I/O session.
* @param buffersize buffer size. Must be a positive number.
+ * @param fragmentSizeHint fragment size hint.
* @param allocator memory allocator.
* If <code>null</code> {@link HeapByteBufferAllocator#INSTANCE} will be used.
* @param chardecoder decoder to be used for decoding HTTP protocol elements.
@@ -201,6 +204,7 @@ public class NHttpConnectionBase
protected NHttpConnectionBase(
final IOSession session,
final int buffersize,
+ final int fragmentSizeHint,
final ByteBufferAllocator allocator,
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
@@ -215,6 +219,7 @@ public class NHttpConnectionBase
}
this.inbuf = new SessionInputBufferImpl(buffersize, linebuffersize, chardecoder, allocator);
this.outbuf = new SessionOutputBufferImpl(buffersize, linebuffersize, charencoder, allocator);
+ this.fragmentSizeHint = fragmentSizeHint >= 0 ? fragmentSizeHint : buffersize;
this.inTransportMetrics = new HttpTransportMetricsImpl();
this.outTransportMetrics = new HttpTransportMetricsImpl();
@@ -422,11 +427,11 @@ public class NHttpConnectionBase
final SessionOutputBuffer buffer,
final HttpTransportMetricsImpl metrics) {
if (len == ContentLengthStrategy.CHUNKED) {
- return new ChunkEncoder(channel, buffer, metrics);
+ return new ChunkEncoder(channel, buffer, metrics, this.fragmentSizeHint);
} else if (len == ContentLengthStrategy.IDENTITY) {
- return new IdentityEncoder(channel, buffer, metrics);
+ return new IdentityEncoder(channel, buffer, metrics, this.fragmentSizeHint);
} else {
- return new LengthDelimitedEncoder(channel, buffer, metrics, len);
+ return new LengthDelimitedEncoder(channel, buffer, metrics, len, this.fragmentSizeHint);
}
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpClientConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpClientConnectionFactory.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpClientConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpClientConnectionFactory.java Wed Feb 20 21:16:19 2013
@@ -204,7 +204,9 @@ public class SSLNHttpClientConnectionFac
charencoder.onUnmappableCharacter(unmappableInputAction);
}
return new DefaultNHttpClientConnection(
- ssliosession, 8 * 1024,
+ ssliosession,
+ this.config.getBufferSize(),
+ this.config.getFragmentSizeHint(),
this.allocator,
chardecoder, charencoder, this.config.getMessageConstraints(),
null, null, null,
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpServerConnectionFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpServerConnectionFactory.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpServerConnectionFactory.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/SSLNHttpServerConnectionFactory.java Wed Feb 20 21:16:19 2013
@@ -203,7 +203,9 @@ public class SSLNHttpServerConnectionFac
charencoder.onMalformedInput(malformedInputAction);
charencoder.onUnmappableCharacter(unmappableInputAction);
}
- return new DefaultNHttpServerConnection(ssliosession, 8 * 1024,
+ return new DefaultNHttpServerConnection(ssliosession,
+ this.config.getBufferSize(),
+ this.config.getFragmentSizeHint(),
this.allocator,
chardecoder, charencoder, this.config.getMessageConstraints(),
null, null,
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentDecoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentDecoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentDecoder.java Wed Feb 20 21:16:19 2013
@@ -27,6 +27,8 @@
package org.apache.http.impl.nio.codecs;
+import java.io.IOException;
+import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import org.apache.http.annotation.NotThreadSafe;
@@ -75,4 +77,60 @@ public abstract class AbstractContentDec
return this.completed;
}
+ /**
+ * Reads from the channel to the destination.
+ *
+ * @param dst destination.
+ * @return number of bytes transferred.
+ *
+ * @since 4.3
+ */
+ protected int readFromChannel(final ByteBuffer dst) throws IOException {
+ final int bytesRead = this.channel.read(dst);
+ if (bytesRead > 0) {
+ this.metrics.incrementBytesTransferred(bytesRead);
+ }
+ return bytesRead;
+ }
+
+ /**
+ * Reads from the channel to the session buffer.
+ * @return number of bytes transferred.
+ *
+ * @since 4.3
+ */
+ protected int fillBufferFromChannel() throws IOException {
+ final int bytesRead = this.buffer.fill(this.channel);
+ if (bytesRead > 0) {
+ this.metrics.incrementBytesTransferred(bytesRead);
+ }
+ return bytesRead;
+ }
+
+ /**
+ * Reads from the channel to the destination.
+ *
+ * @param dst destination.
+ * @param limit max number of bytes to transfer.
+ * @return number of bytes transferred.
+ *
+ * @since 4.3
+ */
+ protected int readFromChannel(final ByteBuffer dst, final int limit) throws IOException {
+ final int bytesRead;
+ if (dst.remaining() > limit) {
+ final int oldLimit = dst.limit();
+ final int newLimit = oldLimit - (dst.remaining() - limit);
+ dst.limit(newLimit);
+ bytesRead = this.channel.read(dst);
+ dst.limit(oldLimit);
+ } else {
+ bytesRead = this.channel.read(dst);
+ }
+ if (bytesRead > 0) {
+ this.metrics.incrementBytesTransferred(bytesRead);
+ }
+ return bytesRead;
+ }
+
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentEncoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentEncoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentEncoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/AbstractContentEncoder.java Wed Feb 20 21:16:19 2013
@@ -28,6 +28,7 @@
package org.apache.http.impl.nio.codecs;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import org.apache.http.annotation.NotThreadSafe;
@@ -85,4 +86,95 @@ public abstract class AbstractContentEnc
Asserts.check(!this.completed, "Encoding process already completed");
}
+ /**
+ * Flushes content of the session buffer to the channel and updates transport metrics.
+ *
+ * @return number of bytes written to the channel.
+ *
+ * @since 4.3
+ */
+ protected int flushToChannel() throws IOException {
+ if (!this.buffer.hasData()) {
+ return 0;
+ }
+ final int bytesWritten = this.buffer.flush(this.channel);
+ if (bytesWritten > 0) {
+ this.metrics.incrementBytesTransferred(bytesWritten);
+ }
+ return bytesWritten;
+ }
+
+ /**
+ * Flushes content of the given buffer to the channel and updates transport metrics.
+ *
+ * @return number of bytes written to the channel.
+ *
+ * @since 4.3
+ */
+ protected int writeToChannel(final ByteBuffer src) throws IOException {
+ if (!src.hasRemaining()) {
+ return 0;
+ }
+ final int bytesWritten = this.channel.write(src);
+ if (bytesWritten > 0) {
+ this.metrics.incrementBytesTransferred(bytesWritten);
+ }
+ return bytesWritten;
+ }
+
+ /**
+ * Transfers content of the source to the channel and updates transport metrics.
+ *
+ * @param src source.
+ * @param limit max number of bytes to transfer.
+ * @return number of bytes transferred.
+ *
+ * @since 4.3
+ */
+ protected int writeToChannel(final ByteBuffer src, final int limit) throws IOException {
+ return doWriteChunk(src, limit, true);
+ }
+
+ /**
+ * Transfers content of the source to the buffer and updates transport metrics.
+ *
+ * @param src source.
+ * @param limit max number of bytes to transfer.
+ * @return number of bytes transferred.
+ *
+ * @since 4.3
+ */
+ protected int writeToBuffer(final ByteBuffer src, final int limit) throws IOException {
+ return doWriteChunk(src, limit, false);
+ }
+
+ private int doWriteChunk(
+ final ByteBuffer src, final int chunk, final boolean direct) throws IOException {
+ int bytesWritten;
+ if (src.remaining() > chunk) {
+ final int oldLimit = src.limit();
+ final int newLimit = oldLimit - (src.remaining() - chunk);
+ src.limit(newLimit);
+ bytesWritten = doWriteChunk(src, direct);
+ src.limit(oldLimit);
+ } else {
+ bytesWritten = doWriteChunk(src, direct);
+ }
+ return bytesWritten;
+ }
+
+ private int doWriteChunk(final ByteBuffer src, final boolean direct) throws IOException {
+ if (direct) {
+ final int bytesWritten = this.channel.write(src);
+ if (bytesWritten > 0) {
+ this.metrics.incrementBytesTransferred(bytesWritten);
+ }
+ return bytesWritten;
+ } else {
+ final int chunk = src.remaining();
+ this.buffer.write(src);
+ return chunk;
+ }
+ }
+
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java Wed Feb 20 21:16:19 2013
@@ -163,10 +163,7 @@ public class ChunkDecoder extends Abstra
while (this.state != COMPLETED) {
if (!this.buffer.hasData() || this.chunkSize == -1) {
- final int bytesRead = this.buffer.fill(this.channel);
- if (bytesRead > 0) {
- this.metrics.incrementBytesTransferred(bytesRead);
- }
+ final int bytesRead = fillBufferFromChannel();
if (bytesRead == -1) {
this.endOfStream = true;
}
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java Wed Feb 20 21:16:19 2013
@@ -46,15 +46,28 @@ import org.apache.http.util.CharArrayBuf
@NotThreadSafe
public class ChunkEncoder extends AbstractContentEncoder {
+ private final int fragHint;
private final CharArrayBuffer lineBuffer;
private final BufferInfo bufferinfo;
+ /**
+ * @since 4.3
+ *
+ * @param channel underlying channel.
+ * @param buffer session buffer.
+ * @param metrics transport metrics.
+ * @param fragementSizeHint fragment size hint defining an minimal size of a fragment
+ * that should be written out directly to the channel bypassing the session buffer.
+ * Value <code>0</code> disables fragment buffering.
+ */
public ChunkEncoder(
final WritableByteChannel channel,
final SessionOutputBuffer buffer,
- final HttpTransportMetricsImpl metrics) {
+ final HttpTransportMetricsImpl metrics,
+ final int fragementSizeHint) {
super(channel, buffer, metrics);
+ this.fragHint = fragementSizeHint > 0 ? fragementSizeHint : 0;
this.lineBuffer = new CharArrayBuffer(16);
if (buffer instanceof BufferInfo) {
this.bufferinfo = (BufferInfo) buffer;
@@ -63,53 +76,63 @@ public class ChunkEncoder extends Abstra
}
}
+ public ChunkEncoder(
+ final WritableByteChannel channel,
+ final SessionOutputBuffer buffer,
+ final HttpTransportMetricsImpl metrics) {
+ this(channel, buffer, metrics, 0);
+ }
+
public int write(final ByteBuffer src) throws IOException {
if (src == null) {
return 0;
}
assertNotCompleted();
- int chunk = src.remaining();
- if (chunk == 0) {
- return 0;
- }
- final long bytesWritten = this.buffer.flush(this.channel);
- if (bytesWritten > 0) {
- this.metrics.incrementBytesTransferred(bytesWritten);
+ int total = 0;
+ while (src.hasRemaining()) {
+ int chunk = src.remaining();
+ int avail;
+ if (this.bufferinfo != null) {
+ avail = this.bufferinfo.available();
+ } else {
+ avail = 4096;
+ }
+
+ // subtract the length of the longest chunk header
+ // 12345678\r\n
+ // <chunk-data>\r\n
+ avail -= 12;
+ if (avail > 0) {
+ if (avail < chunk) {
+ // write no more than 'avail' bytes
+ chunk = avail;
+ this.lineBuffer.clear();
+ this.lineBuffer.append(Integer.toHexString(chunk));
+ this.buffer.writeLine(this.lineBuffer);
+ final int oldlimit = src.limit();
+ src.limit(src.position() + chunk);
+ this.buffer.write(src);
+ src.limit(oldlimit);
+ } else {
+ // write all
+ this.lineBuffer.clear();
+ this.lineBuffer.append(Integer.toHexString(chunk));
+ this.buffer.writeLine(this.lineBuffer);
+ this.buffer.write(src);
+ }
+ this.lineBuffer.clear();
+ this.buffer.writeLine(this.lineBuffer);
+ total += chunk;
+ }
+ if (this.buffer.length() >= this.fragHint || src.hasRemaining()) {
+ final int bytesWritten = flushToChannel();
+ if (bytesWritten == 0) {
+ break;
+ }
+ }
}
- int avail;
- if (this.bufferinfo != null) {
- avail = this.bufferinfo.available();
- } else {
- avail = 4096;
- }
-
- // subtract the length of the longest chunk header
- // 12345678\r\n
- // <chunk-data>\r\n
- avail -= 12;
- if (avail <= 0) {
- return 0;
- } else if (avail < chunk) {
- // write no more than 'avail' bytes
- chunk = avail;
- this.lineBuffer.clear();
- this.lineBuffer.append(Integer.toHexString(chunk));
- this.buffer.writeLine(this.lineBuffer);
- final int oldlimit = src.limit();
- src.limit(src.position() + chunk);
- this.buffer.write(src);
- src.limit(oldlimit);
- } else {
- // write all
- this.lineBuffer.clear();
- this.lineBuffer.append(Integer.toHexString(chunk));
- this.buffer.writeLine(this.lineBuffer);
- this.buffer.write(src);
- }
- this.lineBuffer.clear();
- this.buffer.writeLine(this.lineBuffer);
- return chunk;
+ return total;
}
@Override
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java Wed Feb 20 21:16:19 2013
@@ -82,10 +82,7 @@ public class IdentityDecoder extends Abs
if (this.buffer.hasData()) {
bytesRead = this.buffer.read(dst);
} else {
- bytesRead = this.channel.read(dst);
- if (bytesRead > 0) {
- this.metrics.incrementBytesTransferred(bytesRead);
- }
+ bytesRead = readFromChannel(dst);
}
if (bytesRead == -1) {
this.completed = true;
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityEncoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityEncoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityEncoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityEncoder.java Wed Feb 20 21:16:19 2013
@@ -53,11 +53,32 @@ import org.apache.http.nio.reactor.Sessi
public class IdentityEncoder extends AbstractContentEncoder
implements FileContentEncoder {
+ private final int fragHint;
+
+ /**
+ * @since 4.3
+ *
+ * @param channel underlying channel.
+ * @param buffer session buffer.
+ * @param metrics transport metrics.
+ * @param fragementSizeHint fragment size hint defining an minimal size of a fragment
+ * that should be written out directly to the channel bypassing the session buffer.
+ * Value <code>0</code> disables fragment buffering.
+ */
public IdentityEncoder(
final WritableByteChannel channel,
final SessionOutputBuffer buffer,
- final HttpTransportMetricsImpl metrics) {
+ final HttpTransportMetricsImpl metrics,
+ final int fragementSizeHint) {
super(channel, buffer, metrics);
+ this.fragHint = fragementSizeHint > 0 ? fragementSizeHint : 0;
+ }
+
+ public IdentityEncoder(
+ final WritableByteChannel channel,
+ final SessionOutputBuffer buffer,
+ final HttpTransportMetricsImpl metrics) {
+ this(channel, buffer, metrics, 0);
}
public int write(final ByteBuffer src) throws IOException {
@@ -65,11 +86,36 @@ public class IdentityEncoder extends Abs
return 0;
}
assertNotCompleted();
- final int bytesWritten = this.channel.write(src);
- if (bytesWritten > 0) {
- this.metrics.incrementBytesTransferred(bytesWritten);
+
+ int total = 0;
+ while (src.hasRemaining()) {
+ if (this.buffer.hasData() || this.fragHint > 0) {
+ if (src.remaining() <= this.fragHint) {
+ final int capacity = this.fragHint - this.buffer.length();
+ if (capacity > 0) {
+ final int limit = Math.min(capacity, src.remaining());
+ final int bytesWritten = writeToBuffer(src, limit);
+ total += bytesWritten;
+ }
+ }
+ }
+ if (this.buffer.hasData()) {
+ if (this.buffer.length() >= this.fragHint || src.hasRemaining()) {
+ final int bytesWritten = flushToChannel();
+ if (bytesWritten == 0) {
+ break;
+ }
+ }
+ }
+ if (!this.buffer.hasData() && src.remaining() > this.fragHint) {
+ final int bytesWritten = writeToChannel(src);
+ total += bytesWritten;
+ if (bytesWritten == 0) {
+ break;
+ }
+ }
}
- return bytesWritten;
+ return total;
}
public long transfer(
@@ -81,6 +127,12 @@ public class IdentityEncoder extends Abs
return 0;
}
assertNotCompleted();
+
+ flushToChannel();
+ if (this.buffer.hasData()) {
+ return 0;
+ }
+
final long bytesWritten = src.transferTo(position, count, this.channel);
if (bytesWritten > 0) {
this.metrics.incrementBytesTransferred(bytesWritten);
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedDecoder.java Wed Feb 20 21:16:19 2013
@@ -82,18 +82,7 @@ public class LengthDelimitedDecoder exte
final int maxLen = Math.min(chunk, this.buffer.length());
bytesRead = this.buffer.read(dst, maxLen);
} else {
- if (dst.remaining() > chunk) {
- final int oldLimit = dst.limit();
- final int newLimit = oldLimit - (dst.remaining() - chunk);
- dst.limit(newLimit);
- bytesRead = this.channel.read(dst);
- dst.limit(oldLimit);
- } else {
- bytesRead = this.channel.read(dst);
- }
- if (bytesRead > 0) {
- this.metrics.incrementBytesTransferred(bytesRead);
- }
+ bytesRead = readFromChannel(dst, chunk);
}
if (bytesRead == -1) {
this.completed = true;
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedEncoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedEncoder.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedEncoder.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/LengthDelimitedEncoder.java Wed Feb 20 21:16:19 2013
@@ -56,18 +56,44 @@ public class LengthDelimitedEncoder exte
implements FileContentEncoder {
private final long contentLength;
+ private final int fragHint;
- private long len;
+ private long remaining;
+ /**
+ * @since 4.3
+ *
+ * @param channel underlying channel.
+ * @param buffer session buffer.
+ * @param metrics transport metrics.
+ * @param contentLength content length.
+ * @param fragementSizeHint fragment size hint defining an minimal size of a fragment
+ * that should be written out directly to the channel bypassing the session buffer.
+ * Value <code>0</code> disables fragment buffering.
+ */
public LengthDelimitedEncoder(
final WritableByteChannel channel,
final SessionOutputBuffer buffer,
final HttpTransportMetricsImpl metrics,
- final long contentLength) {
+ final long contentLength,
+ final int fragementSizeHint) {
super(channel, buffer, metrics);
Args.notNegative(contentLength, "Content length");
this.contentLength = contentLength;
- this.len = 0;
+ this.fragHint = fragementSizeHint > 0 ? fragementSizeHint : 0;
+ this.remaining = contentLength;
+ }
+
+ public LengthDelimitedEncoder(
+ final WritableByteChannel channel,
+ final SessionOutputBuffer buffer,
+ final HttpTransportMetricsImpl metrics,
+ final long contentLength) {
+ this(channel, buffer, metrics, contentLength, 0);
+ }
+
+ private int nextChunk(final ByteBuffer src) {
+ return (int) Math.min(Math.min(this.remaining, Integer.MAX_VALUE), src.remaining());
}
public int write(final ByteBuffer src) throws IOException {
@@ -75,26 +101,47 @@ public class LengthDelimitedEncoder exte
return 0;
}
assertNotCompleted();
- final int chunk = (int) Math.min((this.contentLength - this.len), Integer.MAX_VALUE);
- int bytesWritten;
- if (src.remaining() > chunk) {
- final int oldLimit = src.limit();
- final int newLimit = oldLimit - (src.remaining() - chunk);
- src.limit(newLimit);
- bytesWritten = this.channel.write(src);
- src.limit(oldLimit);
- } else {
- bytesWritten = this.channel.write(src);
+ int total = 0;
+ while (src.hasRemaining() && this.remaining > 0) {
+ if (this.buffer.hasData() || this.fragHint > 0) {
+ final int chunk = nextChunk(src);
+ if (chunk <= this.fragHint) {
+ final int capacity = this.fragHint - this.buffer.length();
+ if (capacity > 0) {
+ final int limit = Math.min(capacity, chunk);
+ final int bytesWritten = writeToBuffer(src, limit);
+ this.remaining -= bytesWritten;
+ total += bytesWritten;
+ }
+ }
+ }
+ if (this.buffer.hasData()) {
+ final int chunk = nextChunk(src);
+ if (this.buffer.length() >= this.fragHint || chunk > 0) {
+ final int bytesWritten = flushToChannel();
+ if (bytesWritten == 0) {
+ break;
+ }
+ }
+ }
+ if (!this.buffer.hasData()) {
+ final int chunk = nextChunk(src);
+ if (chunk > this.fragHint) {
+ final int limit = chunk;
+ final int bytesWritten = writeToChannel(src, limit);
+ this.remaining -= bytesWritten;
+ total += bytesWritten;
+ if (bytesWritten == 0) {
+ break;
+ }
+ }
+ }
}
- if (bytesWritten > 0) {
- this.metrics.incrementBytesTransferred(bytesWritten);
- }
- this.len += bytesWritten;
- if (this.len >= this.contentLength) {
+ if (this.remaining <= 0) {
this.completed = true;
}
- return bytesWritten;
+ return total;
}
public long transfer(
@@ -106,13 +153,19 @@ public class LengthDelimitedEncoder exte
return 0;
}
assertNotCompleted();
- final long chunk = Math.min((this.contentLength - this.len), count);
+
+ flushToChannel();
+ if (this.buffer.hasData()) {
+ return 0;
+ }
+
+ final long chunk = Math.min(this.remaining, count);
final long bytesWritten = src.transferTo(position, chunk, this.channel);
if (bytesWritten > 0) {
this.metrics.incrementBytesTransferred(bytesWritten);
}
- this.len += bytesWritten;
- if (this.len >= this.contentLength) {
+ this.remaining -= bytesWritten;
+ if (this.remaining <= 0) {
this.completed = true;
}
return bytesWritten;
@@ -124,7 +177,7 @@ public class LengthDelimitedEncoder exte
buffer.append("[content length: ");
buffer.append(this.contentLength);
buffer.append("; pos: ");
- buffer.append(this.len);
+ buffer.append(this.contentLength - this.remaining);
buffer.append("; completed: ");
buffer.append(this.completed);
buffer.append("]");
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionInputBufferImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionInputBufferImpl.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionInputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionInputBufferImpl.java Wed Feb 20 21:16:19 2013
@@ -131,8 +131,12 @@ public class SessionInputBufferImpl exte
/**
* @since 4.3
*/
- public SessionInputBufferImpl(final int buffersize) {
- this(buffersize, 256, null, HeapByteBufferAllocator.INSTANCE);
+ public SessionInputBufferImpl(
+ final int buffersize,
+ final int lineBuffersize,
+ final Charset charset) {
+ this(buffersize, lineBuffersize,
+ charset != null ? charset.newDecoder() : null, HeapByteBufferAllocator.INSTANCE);
}
/**
@@ -140,10 +144,15 @@ public class SessionInputBufferImpl exte
*/
public SessionInputBufferImpl(
final int buffersize,
- final int lineBuffersize,
- final Charset charset) {
- this(buffersize, lineBuffersize,
- charset != null ? charset.newDecoder() : null, HeapByteBufferAllocator.INSTANCE);
+ final int lineBuffersize) {
+ this(buffersize, lineBuffersize, null, HeapByteBufferAllocator.INSTANCE);
+ }
+
+ /**
+ * @since 4.3
+ */
+ public SessionInputBufferImpl(final int buffersize) {
+ this(buffersize, 256, null, HeapByteBufferAllocator.INSTANCE);
}
public int fill(final ReadableByteChannel channel) throws IOException {
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionOutputBufferImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionOutputBufferImpl.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionOutputBufferImpl.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionOutputBufferImpl.java Wed Feb 20 21:16:19 2013
@@ -148,6 +148,15 @@ public class SessionOutputBufferImpl ext
charset != null ? charset.newEncoder() : null, HeapByteBufferAllocator.INSTANCE);
}
+ /**
+ * @since 4.3
+ */
+ public SessionOutputBufferImpl(
+ final int buffersize,
+ final int linebuffersize) {
+ this(buffersize, linebuffersize, null, HeapByteBufferAllocator.INSTANCE);
+ }
+
public void reset(final HttpParams params) {
clear();
}
Copied: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ByteChannelMock.java (from r1444641, httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ByteChannelMock.java?p2=httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ByteChannelMock.java&p1=httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java&r1=1444641&r2=1448420&rev=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ByteChannelMock.java Wed Feb 20 21:16:19 2013
@@ -29,58 +29,41 @@ package org.apache.http;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.ByteChannel;
-import org.apache.http.util.EncodingUtils;
+public class ByteChannelMock implements ByteChannel {
-public class ReadableByteChannelMock implements ReadableByteChannel {
+ private final ReadableByteChannelMock readableChannel;
+ private final WritableByteChannelMock writableChannel;
+ private boolean closed;
- private final String[] chunks;
- private final String charset;
-
- private int chunkCount = 0;
-
- private ByteBuffer currentChunk;
- private boolean closed = false;
-
- public ReadableByteChannelMock(final String[] chunks, final String charset) {
+ public ByteChannelMock(
+ final ReadableByteChannelMock readableChannel,
+ final WritableByteChannelMock writableChannel) {
super();
- this.chunks = chunks;
- this.charset = charset;
- }
-
- private void prepareChunk() {
- if (this.currentChunk == null || !this.currentChunk.hasRemaining()) {
- if (this.chunkCount < this.chunks.length) {
- final String s = this.chunks[this.chunkCount];
- this.chunkCount++;
- this.currentChunk = ByteBuffer.wrap(EncodingUtils.getBytes(s, this.charset));
- } else {
- this.closed = true;
- }
- }
+ this.readableChannel = readableChannel;
+ this.writableChannel = writableChannel;
}
public int read(final ByteBuffer dst) throws IOException {
- prepareChunk();
- if (this.closed) {
- return -1;
- }
- int i = 0;
- while (dst.hasRemaining() && this.currentChunk.hasRemaining()) {
- dst.put(this.currentChunk.get());
- i++;
- }
- return i;
+ return this.readableChannel.read(dst);
}
- public void close() throws IOException {
- this.closed = true;
+ public int write(final ByteBuffer src) throws IOException {
+ return this.writableChannel.write(src);
}
public boolean isOpen() {
return !this.closed;
}
+ public void close() throws IOException {
+ if (this.closed) {
+ return;
+ }
+ this.closed = true;
+ this.readableChannel.close();
+ this.writableChannel.close();
+ }
-}
+}
\ No newline at end of file
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java?rev=1448420&r1=1448419&r2=1448420&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/ReadableByteChannelMock.java Wed Feb 20 21:16:19 2013
@@ -29,21 +29,24 @@ package org.apache.http;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;
+import java.nio.charset.Charset;
import org.apache.http.util.EncodingUtils;
public class ReadableByteChannelMock implements ReadableByteChannel {
private final String[] chunks;
- private final String charset;
+ private final Charset charset;
private int chunkCount = 0;
private ByteBuffer currentChunk;
+ private boolean eof = false;
private boolean closed = false;
- public ReadableByteChannelMock(final String[] chunks, final String charset) {
+ public ReadableByteChannelMock(final String[] chunks, final Charset charset) {
super();
this.chunks = chunks;
this.charset = charset;
@@ -54,16 +57,19 @@ public class ReadableByteChannelMock imp
if (this.chunkCount < this.chunks.length) {
final String s = this.chunks[this.chunkCount];
this.chunkCount++;
- this.currentChunk = ByteBuffer.wrap(EncodingUtils.getBytes(s, this.charset));
+ this.currentChunk = ByteBuffer.wrap(EncodingUtils.getBytes(s, this.charset.name()));
} else {
- this.closed = true;
+ this.eof = true;
}
}
}
public int read(final ByteBuffer dst) throws IOException {
- prepareChunk();
if (this.closed) {
+ throw new ClosedChannelException();
+ }
+ prepareChunk();
+ if (this.eof) {
return -1;
}
int i = 0;
@@ -79,8 +85,7 @@ public class ReadableByteChannelMock imp
}
public boolean isOpen() {
- return !this.closed;
+ return !this.closed && !this.eof;
}
-
}
Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java?rev=1448420&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java Wed Feb 20 21:16:19 2013
@@ -0,0 +1,158 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.Charset;
+
+import org.apache.http.nio.util.ExpandableBuffer;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+
+public class WritableByteChannelMock implements WritableByteChannel {
+
+ static class InternalBuffer extends ExpandableBuffer {
+
+ private final int capacityLimit;
+ private int curCapacity;
+
+ public InternalBuffer(final int buffersize, final int capacityLimit) {
+ super(buffersize, HeapByteBufferAllocator.INSTANCE);
+ this.capacityLimit = capacityLimit;
+ this.curCapacity = capacityLimit;
+ }
+
+ public int write(final ByteBuffer src) {
+ if (src == null) {
+ return 0;
+ }
+ setInputMode();
+ if (this.capacityLimit > 0) {
+ if (this.curCapacity > 0) {
+ final int requiredCapacity = this.buffer.position() + this.curCapacity;
+ ensureCapacity(requiredCapacity);
+ int count = 0;
+ while (src.hasRemaining() && this.curCapacity > 0) {
+ this.buffer.put(src.get());
+ count++;
+ this.curCapacity--;
+ }
+ return count;
+ } else {
+ return 0;
+ }
+ } else {
+ final int chunk = src.remaining();
+ final int requiredCapacity = this.buffer.position() + src.remaining();
+ ensureCapacity(requiredCapacity);
+ this.buffer.put(src);
+ return chunk;
+ }
+ }
+
+ @Override
+ protected void clear() {
+ super.clear();
+ }
+
+ public void resetCapacity() {
+ this.curCapacity = this.capacityLimit;
+ }
+
+ private static String toString(
+ final byte[] b, final int off, final int len, final Charset charset) {
+ try {
+ return new String(b, off, len, charset.name());
+ } catch (final UnsupportedEncodingException e) {
+ return new String(b, off, len);
+ }
+ }
+
+ public String dump(final Charset charset) {
+ setOutputMode();
+ if (this.buffer.hasArray()) {
+ return toString(this.buffer.array(), this.buffer.position(), this.buffer.limit(),
+ charset);
+ } else {
+ final ByteBuffer dup = this.buffer.duplicate();
+ final byte[] b = new byte[dup.remaining()];
+ int i = 0;
+ while (dup.hasRemaining()) {
+ b[i] = dup.get();
+ i++;
+ }
+ return toString(b, 0, b.length, charset);
+ }
+ }
+
+ }
+
+ private final InternalBuffer buf;
+ private boolean closed;
+
+ public WritableByteChannelMock(final int size, final int capacityLimit) {
+ super();
+ this.buf = new InternalBuffer(size, capacityLimit);
+ }
+
+ public WritableByteChannelMock(final int size) {
+ this(size, 0);
+ }
+
+ public int write(final ByteBuffer src) throws IOException {
+ if (this.closed) {
+ throw new ClosedChannelException();
+ }
+ return this.buf.write(src);
+ }
+
+ public boolean isOpen() {
+ return !this.closed;
+ }
+
+ public void close() throws IOException {
+ this.closed = true;
+ }
+
+ public void flush() {
+ this.buf.resetCapacity();
+ }
+
+ public void reset() {
+ this.buf.resetCapacity();
+ this.buf.clear();
+ }
+
+ public String dump(final Charset charset){
+ return this.buf.dump(charset);
+ }
+
+}
\ No newline at end of file
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/WritableByteChannelMock.java
------------------------------------------------------------------------------
svn:mime-type = text/plain