changeset 1062:4a50422596b6

fix JBuffer.getByteBuffer()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2016 02:04:02 -0700
parents c880589715c7
children 0157e92670f5
files src/org/eclipse/jetty/io/JBuffer.java src/org/eclipse/jetty/io/nio/ChannelEndPoint.java src/org/eclipse/jetty/io/nio/SslConnection.java
diffstat 3 files changed, 28 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/JBuffer.java	Tue Nov 08 05:54:53 2016 -0700
+++ b/src/org/eclipse/jetty/io/JBuffer.java	Wed Nov 09 02:04:02 2016 -0700
@@ -29,6 +29,10 @@
 		bb.position(i);
 	}
 
+	public int limit() {
+		return bb.limit();
+	}
+
 	public void limit(int i) {
 		bb.limit(i);
 	}
@@ -79,9 +83,7 @@
 
 
 	public ByteBuffer getByteBuffer() {
-		ByteBuffer dup = bb.duplicate();
-		dup.limit(dup.capacity());
-		return dup;
+		return bb;
 	}
 
 	public int getIndex() {
--- 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()
--- a/src/org/eclipse/jetty/io/nio/SslConnection.java	Tue Nov 08 05:54:53 2016 -0700
+++ b/src/org/eclipse/jetty/io/nio/SslConnection.java	Wed Nov 09 02:04:02 2016 -0700
@@ -124,7 +124,7 @@
 					if (_buffers==null)
 						_buffers=new SslBuffers(_session.getPacketBufferSize()*2,_session.getApplicationBufferSize()*2);
 					_inbound=_buffers._in;
-					_outbound=_buffers._out;
+					_outbound = _buffers._out;
 					_unwrapBuf=_buffers._unwrap;
 					__buffers.set(null);
 				}
@@ -144,7 +144,7 @@
 					_unwrapBuf.remaining()==0)
 				{
 					_inbound=null;
-					_outbound=null;
+					_outbound = null;
 					_unwrapBuf=null;
 					__buffers.set(_buffers);
 					_buffers=null;
@@ -378,22 +378,20 @@
 
 	private synchronized boolean wrap(final JBuffer buffer) throws IOException
 	{
-		ByteBuffer bbuf=extractByteBuffer(buffer);
+		ByteBuffer bbuf = buffer.getByteBuffer();
 		final SSLEngineResult result;
 
 		synchronized(bbuf)
 		{
 			_outbound.compact();
-			ByteBuffer out_buffer=_outbound.getByteBuffer();
+			ByteBuffer out_buffer = _outbound.getByteBuffer().duplicate();
 			synchronized(out_buffer)
 			{
 				try
 				{
-					bbuf.position(buffer.getIndex());
-					bbuf.limit(buffer.putIndex());
-					out_buffer.position(_outbound.putIndex());
+					out_buffer.position(_outbound.limit());
 					out_buffer.limit(out_buffer.capacity());
-					result=_engine.wrap(bbuf,out_buffer);
+					result = _engine.wrap(bbuf,out_buffer);
 					if (_logger.isDebugEnabled())
 						_logger.debug("{} wrap {} {} consumed={} produced={}",
 							_session,
@@ -402,9 +400,7 @@
 							result.bytesConsumed(),
 							result.bytesProduced());
 
-
-					buffer.skip(result.bytesConsumed());
-					_outbound.setPutIndex(_outbound.putIndex()+result.bytesProduced());
+					_outbound.limit(_outbound.limit()+result.bytesProduced());
 				}
 				catch(SSLException e)
 				{
@@ -412,13 +408,6 @@
 					_endp.close();
 					throw e;
 				}
-				finally
-				{
-					out_buffer.position(0);
-					out_buffer.limit(out_buffer.capacity());
-					bbuf.position(0);
-					bbuf.limit(bbuf.capacity());
-				}
 			}
 		}
 
@@ -454,20 +443,18 @@
 		if (!_inbound.hasRemaining())
 			return false;
 
-		ByteBuffer bbuf=extractByteBuffer(buffer);
+		ByteBuffer bbuf = buffer.getByteBuffer().duplicate();
 		final SSLEngineResult result;
 
 		synchronized(bbuf)
 		{
-			ByteBuffer in_buffer=_inbound.getByteBuffer();
+			ByteBuffer in_buffer = _inbound.getByteBuffer();
 			synchronized(in_buffer)
 			{
 				try
 				{
-					bbuf.position(buffer.putIndex());
+					bbuf.position(buffer.limit());
 					bbuf.limit(buffer.capacity());
-					in_buffer.position(_inbound.getIndex());
-					in_buffer.limit(_inbound.putIndex());
 
 					result=_engine.unwrap(in_buffer,bbuf);
 					if (_logger.isDebugEnabled())
@@ -478,9 +465,8 @@
 							result.bytesConsumed(),
 							result.bytesProduced());
 
-					_inbound.skip(result.bytesConsumed());
 					_inbound.compact();
-					buffer.setPutIndex(buffer.putIndex()+result.bytesProduced());
+					buffer.limit(buffer.limit()+result.bytesProduced());
 				}
 				catch(SSLException e)
 				{
@@ -488,13 +474,6 @@
 					_endp.close();
 					throw e;
 				}
-				finally
-				{
-					in_buffer.position(0);
-					in_buffer.limit(in_buffer.capacity());
-					bbuf.position(0);
-					bbuf.limit(bbuf.capacity());
-				}
 			}
 		}
 
@@ -530,12 +509,6 @@
 		return result.bytesConsumed()>0 || result.bytesProduced()>0;
 	}
 
-
-	private ByteBuffer extractByteBuffer(JBuffer buffer)
-	{
-		return buffer.getByteBuffer();
-	}
-
 	public SslEndPoint getSslEndPoint()
 	{
 		return _sslEndPoint;