diff src/org/eclipse/jetty/io/nio/ChannelEndPoint.java @ 978:bdb6eb0fbf93

simplify ChannelEndPoint
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 16 Oct 2016 14:53:43 -0600
parents 866f2e801618
children 4f2d04c72781
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Sun Oct 16 01:31:05 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/ChannelEndPoint.java	Sun Oct 16 14:53:43 2016 -0600
@@ -25,7 +25,6 @@
 import java.nio.ByteBuffer;
 import java.nio.channels.ByteChannel;
 import java.nio.channels.GatheringByteChannel;
-import java.nio.channels.SelectableChannel;
 import java.nio.channels.SocketChannel;
 
 import org.eclipse.jetty.io.Buffer;
@@ -43,36 +42,29 @@
 {
 	private static final Logger LOG = LoggerFactory.getLogger(ChannelEndPoint.class);
 
-	protected final ByteChannel _channel;
-	protected final ByteBuffer[] _gather2=new ByteBuffer[2];
+	private final SocketChannel _channel;
+	private final ByteBuffer[] _gather2 = new ByteBuffer[2];
 	protected final Socket _socket;
-	protected final InetSocketAddress _local;
-	protected final InetSocketAddress _remote;
+	private final InetSocketAddress _local;
+	private final InetSocketAddress _remote;
 	protected volatile int _maxIdleTime;
-	private volatile boolean _ishut;
-	private volatile boolean _oshut;
+	private volatile boolean _ishut = false;
+	private volatile boolean _oshut = false;
 
-	protected ChannelEndPoint(ByteChannel channel, int maxIdleTime) throws IOException
+	protected ChannelEndPoint(SocketChannel channel, int maxIdleTime) throws IOException
 	{
 		this._channel = channel;
 		_maxIdleTime = maxIdleTime;
-		_socket = (channel instanceof SocketChannel)?((SocketChannel)channel).socket():null;
-		if (_socket!=null)
-		{
-			_local = (InetSocketAddress)_socket.getLocalSocketAddress();
-			_remote = (InetSocketAddress)_socket.getRemoteSocketAddress();
-			_socket.setSoTimeout(_maxIdleTime);
-		}
-		else
-		{
-			_local = _remote = null;
-		}
+		_socket = channel.socket();
+		_local = (InetSocketAddress)_socket.getLocalSocketAddress();
+		_remote = (InetSocketAddress)_socket.getRemoteSocketAddress();
+		_socket.setSoTimeout(_maxIdleTime);
 	}
 
 	@Override
 	public final boolean isBlocking()
 	{
-		return  !(_channel instanceof SelectableChannel) || ((SelectableChannel)_channel).isBlocking();
+		return _channel.isBlocking();
 	}
 
 	@Override
@@ -100,26 +92,23 @@
 		_ishut = true;
 		if (_channel.isOpen())
 		{
-			if (_socket != null)
+			try
 			{
-				try
+				if (!_socket.isInputShutdown())
 				{
-					if (!_socket.isInputShutdown())
-					{
-						_socket.shutdownInput();
-					}
+					_socket.shutdownInput();
 				}
-				catch (SocketException e)
+			}
+			catch (SocketException e)
+			{
+				LOG.debug(e.toString());
+				LOG.trace("",e);
+			}
+			finally
+			{
+				if (_oshut)
 				{
-					LOG.debug(e.toString());
-					LOG.trace("",e);
-				}
-				finally
-				{
-					if (_oshut)
-					{
-						close();
-					}
+					close();
 				}
 			}
 		}
@@ -132,26 +121,23 @@
 		_oshut = true;
 		if (_channel.isOpen())
 		{
-			if (_socket != null)
+			try
 			{
-				try
+				if (!_socket.isOutputShutdown())
 				{
-					if (!_socket.isOutputShutdown())
-					{
-						_socket.shutdownOutput();
-					}
+					_socket.shutdownOutput();
 				}
-				catch (SocketException e)
+			}
+			catch (SocketException e)
+			{
+				LOG.debug(e.toString());
+				LOG.trace("",e);
+			}
+			finally
+			{
+				if (_ishut)
 				{
-					LOG.debug(e.toString());
-					LOG.trace("",e);
-				}
-				finally
-				{
-					if (_ishut)
-					{
-						close();
-					}
+					close();
 				}
 			}
 		}
@@ -160,13 +146,13 @@
 	@Override
 	public final boolean isOutputShutdown()
 	{
-		return _oshut || !_channel.isOpen() || _socket != null && _socket.isOutputShutdown();
+		return _oshut || !_channel.isOpen() || _socket.isOutputShutdown();
 	}
 
 	@Override
 	public final boolean isInputShutdown()
 	{
-		return _ishut || !_channel.isOpen() || _socket != null && _socket.isInputShutdown();
+		return _ishut || !_channel.isOpen() || _socket.isInputShutdown();
 	}
 
 	@Override
@@ -176,9 +162,7 @@
 		_channel.close();
 	}
 
-	/* (non-Javadoc)
-	 * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer)
-	 */
+	@Override
 	public int fill(Buffer buffer) throws IOException
 	{
 		if (_ishut)
@@ -241,9 +225,7 @@
 		return len;
 	}
 
-	/* (non-Javadoc)
-	 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer)
-	 */
+	@Override
 	public int flush(Buffer buffer) throws IOException
 	{
 		Buffer buf = buffer.buffer();
@@ -284,9 +266,7 @@
 		return len;
 	}
 
-	/* (non-Javadoc)
-	 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
-	 */
+	@Override
 	public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
 	{
 		int length=0;
@@ -321,7 +301,7 @@
 		return length;
 	}
 
-	protected int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
+	private int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException
 	{
 		int length;
 
@@ -355,110 +335,67 @@
 		return length;
 	}
 
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return Returns the channel.
-	 */
-	public ByteChannel getChannel()
+	public final SocketChannel getChannel()
 	{
 		return _channel;
 	}
 
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getLocalAddr()
-	 */
-	public String getLocalAddr()
+	@Override
+	public final String getLocalAddr()
 	{
-		if (_socket==null)
-			return null;
-	   if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
+	   if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
 		   return StringUtil.ALL_INTERFACES;
 		return _local.getAddress().getHostAddress();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getLocalHost()
-	 */
-	public String getLocalHost()
+	@Override
+	public final String getLocalHost()
 	{
-		if (_socket==null)
-			return null;
-	   if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
+	   if (_local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
 		   return StringUtil.ALL_INTERFACES;
 		return _local.getAddress().getCanonicalHostName();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getLocalPort()
-	 */
-	public int getLocalPort()
+	@Override
+	public final int getLocalPort()
 	{
-		if (_socket==null)
-			return 0;
-		if (_local==null)
-			return -1;
 		return _local.getPort();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getRemoteAddr()
-	 */
-	public String getRemoteAddr()
+	@Override
+	public final String getRemoteAddr()
 	{
-		if (_socket==null)
-			return null;
-		if (_remote==null)
-			return null;
 		return _remote.getAddress().getHostAddress();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getRemoteHost()
-	 */
-	public String getRemoteHost()
+	@Override
+	public final String getRemoteHost()
 	{
-		if (_socket==null)
-			return null;
-		if (_remote==null)
-			return null;
 		return _remote.getAddress().getCanonicalHostName();
 	}
 
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see org.eclipse.io.EndPoint#getRemotePort()
-	 */
-	public int getRemotePort()
+	@Override
+	public final int getRemotePort()
 	{
-		if (_socket==null)
-			return 0;
-		return _remote==null?-1:_remote.getPort();
+		return _remote.getPort();
 	}
 
-	public Object getTransport()
-	{
-		return _channel;
-	}
-
+	@Override
 	public void flush()
 		throws IOException
 	{
 	}
 
-	public int getMaxIdleTime()
+	@Override
+	public final int getMaxIdleTime()
 	{
 		return _maxIdleTime;
 	}
 
+	@Override
 	public void setMaxIdleTime(int timeMs) throws IOException
 	{
-		if (_socket!=null && timeMs!=_maxIdleTime)
+		if (timeMs!=_maxIdleTime)
 			_socket.setSoTimeout(timeMs>0?timeMs:0);
 		_maxIdleTime=timeMs;
 	}