diff src/org/eclipse/jetty/io/nio/ChannelEndPoint.java @ 1062:4a50422596b6

fix JBuffer.getByteBuffer()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2016 02:04:02 -0700
parents 4afdf0f0c5bc
children ebb0f1343ef6
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Tue Nov 08 05:54:53 2016 -0700
+++ b/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Wed Nov 09 02:04:02 2016 -0700
@@ -43,7 +43,6 @@
 	private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class);
 
 	private final SocketChannel _channel;
-	private final ByteBuffer[] _gather2 = new ByteBuffer[2];
 	protected final Socket _socket;
 	private final InetSocketAddress _local;
 	private final InetSocketAddress _remote;
@@ -169,24 +168,15 @@
 		if (_ishut)
 			return -1;
 		int len = 0;
-		final ByteBuffer bbuf = buffer.getByteBuffer();
+		final ByteBuffer bbuf = buffer.getByteBuffer().duplicate();
 
 		//noinspection SynchronizationOnLocalVariableOrMethodParameter
 		try
 		{
-			synchronized(bbuf)
-			{
-				try
-				{
-					bbuf.position(buffer.putIndex());
-					len = _channel.read(bbuf);
-				}
-				finally
-				{
-					buffer.setPutIndex(bbuf.position());
-					bbuf.position(0);
-				}
-			}
+			bbuf.limit(bbuf.capacity());
+			bbuf.position(buffer.limit());
+			len = _channel.read(bbuf);
+			buffer.limit(bbuf.position());
 
 			if (len<0 && isOpen())
 			{
@@ -220,20 +210,8 @@
 	@Override
 	public int flush(JBuffer buffer) throws IOException
 	{
-		int len=0;
-		final ByteBuffer bbuf = buffer.getByteBuffer().asReadOnlyBuffer();
-		try
-		{
-			bbuf.position(buffer.getIndex());
-			bbuf.limit(buffer.putIndex());
-			len=_channel.write(bbuf);
-		}
-		finally
-		{
-			if (len>0)
-				buffer.skip(len);
-		}
-		return len;
+		final ByteBuffer bbuf = buffer.getByteBuffer();
+		return _channel.write(bbuf);
 	}
 
 	@Override
@@ -245,7 +223,7 @@
 			header!=null && header.remaining()!=0 &&
 			buffer!=null && buffer.remaining()!=0)
 		{
-			length = gatheringFlush(header,header.getByteBuffer(),buffer,buffer.getByteBuffer());
+			length = gatheringFlush(header,buffer);
 		}
 		else
 		{
@@ -268,38 +246,11 @@
 		return length;
 	}
 
-	private int gatheringFlush(JBuffer header, ByteBuffer bbuf0, JBuffer buffer, ByteBuffer bbuf1) throws IOException
+	private synchronized int gatheringFlush(JBuffer header, JBuffer buffer) throws IOException
 	{
-		int length;
-
-		synchronized(this)
-		{
-			// Adjust position indexs of buf0 and buf1
-			bbuf0=bbuf0.asReadOnlyBuffer();
-			bbuf0.position(header.getIndex());
-			bbuf0.limit(header.putIndex());
-			bbuf1=bbuf1.asReadOnlyBuffer();
-			bbuf1.position(buffer.getIndex());
-			bbuf1.limit(buffer.putIndex());
-
-			_gather2[0]=bbuf0;
-			_gather2[1]=bbuf1;
-
-			// do the gathering write.
-			length=(int)((GatheringByteChannel)_channel).write(_gather2);
-
-			int hl=header.remaining();
-			if (length>hl)
-			{
-				header.clear();
-				buffer.skip(length-hl);
-			}
-			else if (length>0)
-			{
-				header.skip(length);
-			}
-		}
-		return length;
+		ByteBuffer bbuf0 = header.getByteBuffer();
+		ByteBuffer bbuf1 = buffer.getByteBuffer();
+		return (int)_channel.write(new ByteBuffer[]{bbuf0,bbuf1});
 	}
 
 	public final SocketChannel getChannel()