Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.io.nio;
 
 
Channel End Point.

Holds the channel and socket for an NIO endpoint.

 
 public class ChannelEndPoint implements EndPoint
 {
     private static final Logger LOG = Log.getLogger(ChannelEndPoint.class);
 
     protected final ByteChannel _channel;
     protected final ByteBuffer[] _gather2=new ByteBuffer[2];
     protected final Socket _socket;
     protected final InetSocketAddress _local;
     protected final InetSocketAddress _remote;
     protected volatile int _maxIdleTime;
     private volatile boolean _ishut;
     private volatile boolean _oshut;
 
     public ChannelEndPoint(ByteChannel channelthrows IOException
     {
         super();
         this. = channel;
         =(channel instanceof SocketChannel)?((SocketChannel)channel).socket():null;
         if (!=null)
         {
             =.getSoTimeout();
         }
         else
         {
             ==null;
         }
     }
 
     protected ChannelEndPoint(ByteChannel channelint maxIdleTimethrows IOException
     {
         this. = channel;
         =maxIdleTime;
         =(channel instanceof SocketChannel)?((SocketChannel)channel).socket():null;
         if (!=null)
         {
             .setSoTimeout();
         }
         else
         {
             ==null;
         }
     }
 
     public boolean isBlocking()
     {
         return  !( instanceof SelectableChannel) || ((SelectableChannel)).isBlocking();
     }
 
     public boolean blockReadable(long millisecsthrows IOException
     {
         return true;
     }
 
     public boolean blockWritable(long millisecsthrows IOException
    {
        return true;
    }
    /*
     * @see org.eclipse.io.EndPoint#isOpen()
     */
    public boolean isOpen()
    {
        return .isOpen();
    }

    
Shutdown the channel Input. Cannot be overridden. To override, see shutdownInput()

    protected final void shutdownChannelInput() throws IOException
    {
        .debug("ishut {}"this);
         = true;
        if (.isOpen())
        {
            if ( != null)
            {
                try
                {
                    if (!.isInputShutdown())
                    {
                        .shutdownInput();
                    }
                }
                catch (SocketException e)
                {
                    .debug(e.toString());
                    .ignore(e);
                }
                finally
                {
                    if ()
                    {
                        close();
                    }
                }
            }
        }
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#close()
     */
    public void shutdownInput() throws IOException
    {
        shutdownChannelInput();
    }
    protected final void shutdownChannelOutput() throws IOException
    {
        .debug("oshut {}",this);
         = true;
        if (.isOpen())
        {
            if ( != null)
            {
                try
                {
                    if (!.isOutputShutdown())
                    {
                        .shutdownOutput();
                    }
                }
                catch (SocketException e)
                {
                    .debug(e.toString());
                    .ignore(e);
                }
                finally
                {
                    if ()
                    {
                        close();
                    }
                }
            }
        }
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#close()
     */
    public void shutdownOutput() throws IOException
    {
        shutdownChannelOutput();
    }
    public boolean isOutputShutdown()
    {
        return  || !.isOpen() ||  != null && .isOutputShutdown();
    }
    public boolean isInputShutdown()
    {
        return  || !.isOpen() ||  != null && .isInputShutdown();
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#close()
     */
    public void close() throws IOException
    {
        .debug("close {}",this);
        .close();
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer)
     */
    public int fill(Buffer bufferthrows IOException
    {
        if ()
            return -1;
        Buffer buf = buffer.buffer();
        int len=0;
        if (buf instanceof NIOBuffer)
        {
            final NIOBuffer nbuf = (NIOBuffer)buf;
            final ByteBuffer bbuf=nbuf.getByteBuffer();
            //noinspection SynchronizationOnLocalVariableOrMethodParameter
            try
            {
                synchronized(bbuf)
                {
                    try
                    {
                        bbuf.position(buffer.putIndex());
                        len=.read(bbuf);
                    }
                    finally
                    {
                        buffer.setPutIndex(bbuf.position());
                        bbuf.position(0);
                    }
                }
                if (len<0 && isOpen())
                {
                    if (!isInputShutdown())
                        shutdownInput();
                    if (isOutputShutdown())
                        .close();
                }
            }
            catch (IOException x)
            {
                .debug("Exception while filling"x);
                try
                {
                    if (.isOpen())
                        .close();
                }
                catch (Exception xx)
                {
                    .ignore(xx);
                }
                if (len>0)
                    throw x;
                len=-1;
            }
        }
        else
        {
            throw new IOException("Not Implemented");
        }
        return len;
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer)
     */
    public int flush(Buffer bufferthrows IOException
    {
        Buffer buf = buffer.buffer();
        int len=0;
        if (buf instanceof NIOBuffer)
        {
            final NIOBuffer nbuf = (NIOBuffer)buf;
            final ByteBuffer bbuf=nbuf.getByteBuffer().asReadOnlyBuffer();
            try
            {
                bbuf.position(buffer.getIndex());
                bbuf.limit(buffer.putIndex());
                len=.write(bbuf);
            }
            finally
            {
                if (len>0)
                    buffer.skip(len);
            }
        }
        else if (buf instanceof RandomAccessFileBuffer)
        {
            len = ((RandomAccessFileBuffer)buf).writeTo(,buffer.getIndex(),buffer.length());
            if (len>0)
                buffer.skip(len);
        }
        else if (buffer.array()!=null)
        {
            ByteBuffer b = ByteBuffer.wrap(buffer.array(), buffer.getIndex(), buffer.length());
            len=.write(b);
            if (len>0)
                buffer.skip(len);
        }
        else
        {
            throw new IOException("Not Implemented");
        }
        return len;
    }
    /* (non-Javadoc)
     * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
     */
    public int flush(Buffer headerBuffer bufferBuffer trailerthrows IOException
    {
        int length=0;
        Buffer buf0 = header==null?null:header.buffer();
        Buffer buf1 = buffer==null?null:buffer.buffer();
        if ( instanceof GatheringByteChannel &&
            header!=null && header.length()!=0 && buf0 instanceof NIOBuffer &&
            buffer!=null && buffer.length()!=0 && buf1 instanceof NIOBuffer)
        {
            length = gatheringFlush(header,((NIOBuffer)buf0).getByteBuffer(),buffer,((NIOBuffer)buf1).getByteBuffer());
        }
        else
        {
            // flush header
            if (header!=null && header.length()>0)
                length=flush(header);
            // flush buffer
            if ((header==null || header.length()==0) &&
                 buffer!=null && buffer.length()>0)
                length+=flush(buffer);
            // flush trailer
            if ((header==null || header.length()==0) &&
                (buffer==null || buffer.length()==0) &&
                 trailer!=null && trailer.length()>0)
                length+=flush(trailer);
        }
        return length;
    }
    protected int gatheringFlush(Buffer headerByteBuffer bbuf0Buffer bufferByteBuffer bbuf1throws 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());
            [0]=bbuf0;
            [1]=bbuf1;
            // do the gathering write.
            length=(int)((GatheringByteChannel)).write();
            int hl=header.length();
            if (length>hl)
            {
                header.clear();
                buffer.skip(length-hl);
            }
            else if (length>0)
            {
                header.skip(length);
            }
        }
        return length;
    }
    /* ------------------------------------------------------------ */
    

Returns:
Returns the channel.
    public ByteChannel getChannel()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getLocalAddr()
     */
    public String getLocalAddr()
    {
        if (==null)
            return null;
       if (==null || .getAddress()==null || .getAddress().isAnyLocalAddress())
           return .;
        return .getAddress().getHostAddress();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getLocalHost()
     */
    public String getLocalHost()
    {
        if (==null)
            return null;
       if (==null || .getAddress()==null || .getAddress().isAnyLocalAddress())
           return .;
        return .getAddress().getCanonicalHostName();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getLocalPort()
     */
    public int getLocalPort()
    {
        if (==null)
            return 0;
        if (==null)
            return -1;
        return .getPort();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getRemoteAddr()
     */
    public String getRemoteAddr()
    {
        if (==null)
            return null;
        if (==null)
            return null;
        return .getAddress().getHostAddress();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getRemoteHost()
     */
    public String getRemoteHost()
    {
        if (==null)
            return null;
        if (==null)
            return null;
        return .getAddress().getCanonicalHostName();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getRemotePort()
     */
    public int getRemotePort()
    {
        if (==null)
            return 0;
        return ==null?-1:.getPort();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.io.EndPoint#getConnection()
     */
    public Object getTransport()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public void flush()
        throws IOException
    {
    }
    /* ------------------------------------------------------------ */
    public int getMaxIdleTime()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    
    public void setMaxIdleTime(int timeMsthrows IOException
    {
        if (!=null && timeMs!=)
            .setSoTimeout(timeMs>0?timeMs:0);
        =timeMs;
    }
New to GrepCode? Check out our FAQ X