2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.io;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.net.Socket;
24 import java.nio.ByteBuffer;
25 import java.nio.channels.ByteChannel;
26 import java.nio.channels.GatheringByteChannel;
27 import java.nio.channels.SocketChannel;
29 import org.eclipse.jetty.util.BufferUtil;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.log.Logger;
32 import org.eclipse.jetty.util.thread.Scheduler;
36 * <p>Holds the channel and socket for an NIO endpoint.
38 public class ChannelEndPoint extends AbstractEndPoint
40 private static final Logger LOG = Log.getLogger(ChannelEndPoint.class);
42 private final ByteChannel _channel;
43 private final Socket _socket;
44 private volatile boolean _ishut;
45 private volatile boolean _oshut;
47 public ChannelEndPoint(Scheduler scheduler,SocketChannel channel)
50 (InetSocketAddress)channel.socket().getLocalSocketAddress(),
51 (InetSocketAddress)channel.socket().getRemoteSocketAddress());
53 _socket=channel.socket();
57 public boolean isOpen()
59 return _channel.isOpen();
62 protected void shutdownInput()
64 if (LOG.isDebugEnabled())
65 LOG.debug("ishut {}", this);
72 public void shutdownOutput()
74 if (LOG.isDebugEnabled())
75 LOG.debug("oshut {}", this);
77 if (_channel.isOpen())
81 if (!_socket.isOutputShutdown())
82 _socket.shutdownOutput();
99 public boolean isOutputShutdown()
101 return _oshut || !_channel.isOpen() || _socket.isOutputShutdown();
105 public boolean isInputShutdown()
107 return _ishut || !_channel.isOpen() || _socket.isInputShutdown();
114 if (LOG.isDebugEnabled())
115 LOG.debug("close {}", this);
120 catch (IOException e)
132 public int fill(ByteBuffer buffer) throws IOException
137 int pos=BufferUtil.flipToFill(buffer);
140 int filled = _channel.read(buffer);
141 if (LOG.isDebugEnabled()) // Avoid boxing of variable 'filled'
142 LOG.debug("filled {} {}", filled, this);
159 BufferUtil.flipToFlush(buffer,pos);
164 public boolean flush(ByteBuffer... buffers) throws IOException
169 if (buffers.length==1)
170 flushed=_channel.write(buffers[0]);
171 else if (buffers.length>1 && _channel instanceof GatheringByteChannel)
172 flushed= (int)((GatheringByteChannel)_channel).write(buffers,0,buffers.length);
175 for (ByteBuffer b : buffers)
177 if (b.hasRemaining())
179 int l=_channel.write(b);
182 if (b.hasRemaining())
187 if (LOG.isDebugEnabled())
188 LOG.debug("flushed {} {}", flushed, this);
190 catch (IOException e)
192 throw new EofException(e);
198 for (ByteBuffer b : buffers)
199 if (!BufferUtil.isEmpty(b))
205 public ByteChannel getChannel()
211 public Object getTransport()
216 public Socket getSocket()
222 protected void onIncompleteFlush()
224 throw new UnsupportedOperationException();
228 protected boolean needsFill() throws IOException
230 throw new UnsupportedOperationException();