2 // ========================================================================
3 // Copyright (c) 1995-2014 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 LOG.debug("ishut {}", this);
71 public void shutdownOutput()
73 LOG.debug("oshut {}", this);
75 if (_channel.isOpen())
79 if (!_socket.isOutputShutdown())
80 _socket.shutdownOutput();
97 public boolean isOutputShutdown()
99 return _oshut || !_channel.isOpen() || _socket.isOutputShutdown();
103 public boolean isInputShutdown()
105 return _ishut || !_channel.isOpen() || _socket.isInputShutdown();
112 LOG.debug("close {}", this);
117 catch (IOException e)
129 public int fill(ByteBuffer buffer) throws IOException
134 int pos=BufferUtil.flipToFill(buffer);
137 int filled = _channel.read(buffer);
138 if (LOG.isDebugEnabled()) // Avoid boxing of variable 'filled'
139 LOG.debug("filled {} {}", filled, this);
156 BufferUtil.flipToFlush(buffer,pos);
161 public boolean flush(ByteBuffer... buffers) throws IOException
166 if (buffers.length==1)
167 flushed=_channel.write(buffers[0]);
168 else if (buffers.length>1 && _channel instanceof GatheringByteChannel)
169 flushed= (int)((GatheringByteChannel)_channel).write(buffers,0,buffers.length);
172 for (ByteBuffer b : buffers)
174 if (b.hasRemaining())
176 int l=_channel.write(b);
179 if (b.hasRemaining())
184 if (LOG.isDebugEnabled())
185 LOG.debug("flushed {} {}", flushed, this);
187 catch (IOException e)
189 throw new EofException(e);
195 for (ByteBuffer b : buffers)
196 if (!BufferUtil.isEmpty(b))
202 public ByteChannel getChannel()
208 public Object getTransport()
213 public Socket getSocket()
219 protected void onIncompleteFlush()
221 throw new UnsupportedOperationException();
225 protected boolean needsFill() throws IOException
227 throw new UnsupportedOperationException();