Skip to content

Commit

Permalink
Deque<ByteBufferHolder> is a little more complex than List<ByteBuffer>
Browse files Browse the repository at this point in the history
but using the same structure for all connectors will improve code re-use
and thereby improve maintainability.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1650268 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jan 8, 2015
1 parent 998e6d1 commit b7bc74c
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions java/org/apache/coyote/http11/InternalNio2OutputBuffer.java
Expand Up @@ -31,6 +31,7 @@
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;


import org.apache.coyote.Response; import org.apache.coyote.Response;
import org.apache.tomcat.util.buf.ByteBufferHolder;
import org.apache.tomcat.util.net.AbstractEndpoint; import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.Nio2Channel; import org.apache.tomcat.util.net.Nio2Channel;
import org.apache.tomcat.util.net.Nio2Endpoint; import org.apache.tomcat.util.net.Nio2Endpoint;
Expand Down Expand Up @@ -78,11 +79,6 @@ public InternalNio2OutputBuffer(Response response, int headerBufferSize) {
*/ */
protected AbstractEndpoint<Nio2Channel> endpoint = null; protected AbstractEndpoint<Nio2Channel> endpoint = null;


/**
* Used instead of the deque since it looks equivalent and simpler.
*/
protected ArrayList<ByteBuffer> bufferedWrites = new ArrayList<>();

/** /**
* Exception that occurred during writing. * Exception that occurred during writing.
*/ */
Expand All @@ -109,9 +105,9 @@ public void completed(Integer nBytes, ByteBuffer attachment) {
if (attachment.hasRemaining()) { if (attachment.hasRemaining()) {
arrayList.add(attachment); arrayList.add(attachment);
} }
for (ByteBuffer buffer : bufferedWrites) { for (ByteBufferHolder buffer : bufferedWrites) {
buffer.flip(); buffer.flip();
arrayList.add(buffer); arrayList.add(buffer.getBuf());
} }
bufferedWrites.clear(); bufferedWrites.clear();
ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY); ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY);
Expand Down Expand Up @@ -164,9 +160,9 @@ public void completed(Long nBytes, ByteBuffer[] attachment) {
arrayList.add(buffer); arrayList.add(buffer);
} }
} }
for (ByteBuffer buffer : bufferedWrites) { for (ByteBufferHolder buffer : bufferedWrites) {
buffer.flip(); buffer.flip();
arrayList.add(buffer); arrayList.add(buffer.getBuf());
} }
bufferedWrites.clear(); bufferedWrites.clear();
ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY); ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY);
Expand Down Expand Up @@ -303,7 +299,7 @@ protected void addToBB(byte[] buf, int offset, int length)
private void addToBuffers(byte[] buf, int offset, int length) { private void addToBuffers(byte[] buf, int offset, int length) {
ByteBuffer buffer = ByteBuffer.allocate(length); ByteBuffer buffer = ByteBuffer.allocate(length);
buffer.put(buf, offset, length); buffer.put(buf, offset, length);
bufferedWrites.add(buffer); bufferedWrites.add(new ByteBufferHolder(buffer, false));
} }




Expand Down Expand Up @@ -336,8 +332,9 @@ private boolean flushBufferInternal(boolean block, boolean hasPermit) throws IOE
} }
try { try {
if (bufferedWrites.size() > 0) { if (bufferedWrites.size() > 0) {
for (ByteBuffer buffer : bufferedWrites) { for (ByteBufferHolder holder : bufferedWrites) {
buffer.flip(); holder.flip();
ByteBuffer buffer = holder.getBuf();
while (buffer.hasRemaining()) { while (buffer.hasRemaining()) {
if (socketWrapper.getSocket().write(buffer).get(socketWrapper.getTimeout(), TimeUnit.MILLISECONDS).intValue() < 0) { if (socketWrapper.getSocket().write(buffer).get(socketWrapper.getTimeout(), TimeUnit.MILLISECONDS).intValue() < 0) {
throw new EOFException(sm.getString("iob.failedwrite")); throw new EOFException(sm.getString("iob.failedwrite"));
Expand Down Expand Up @@ -383,9 +380,9 @@ private boolean flushBufferInternal(boolean block, boolean hasPermit) throws IOE
if (socketWriteBuffer.hasRemaining()) { if (socketWriteBuffer.hasRemaining()) {
arrayList.add(socketWriteBuffer); arrayList.add(socketWriteBuffer);
} }
for (ByteBuffer buffer : bufferedWrites) { for (ByteBufferHolder buffer : bufferedWrites) {
buffer.flip(); buffer.flip();
arrayList.add(buffer); arrayList.add(buffer.getBuf());
} }
bufferedWrites.clear(); bufferedWrites.clear();
ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY); ByteBuffer[] array = arrayList.toArray(EMPTY_BUF_ARRAY);
Expand Down

0 comments on commit b7bc74c

Please sign in to comment.