Example usage for java.nio.channels SelectableChannel close

List of usage examples for java.nio.channels SelectableChannel close

Introduction

In this page you can find the example usage for java.nio.channels SelectableChannel close.

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:de.kapsi.net.daap.nio.DaapServerNIO.java

/**
 * Cancel SelesctionKey, close Channel and "free" the attachment
 *///from w  ww .  ja  v  a2s. com
private void cancel(SelectionKey sk) {

    sk.cancel();

    SelectableChannel channel = (SelectableChannel) sk.channel();

    try {
        channel.close();
    } catch (IOException err) {
        LOG.error("Channel.close()", err);
    }

    DaapConnection connection = (DaapConnection) sk.attachment();

    if (connection != null) {

        DaapSession session = connection.getSession(false);
        if (session != null) {
            sessionIds.remove(session.getSessionId());
        }

        connection.close();

        if (connection.isDaapConnection()) {
            connections.remove(connection);
        } else if (connection.isAudioStream()) {
            streams.remove(connection);
        }
    }
}

From source file:net.sf.cindy.impl.ChannelSession.java

protected void startSession(SelectableChannel readChannel, SelectableChannel writeChannel, boolean block) {
    synchronized (startLock) {
        if (isStarted())
            return;
        try {/*from w  w w .ja  v a 2  s  .  co  m*/
            if (readChannel != null)
                readChannel.configureBlocking(false);
            if (writeChannel != null)
                writeChannel.configureBlocking(false);
        } catch (IOException e) {
            dispatchException(e);
            if (readChannel != null)
                try {
                    readChannel.close();
                } catch (IOException ioe) {
                }
            if (writeChannel != null) {
                try {
                    writeChannel.close();
                } catch (IOException ioe) {
                }
            }
            dispatchSessionClosed();
            return;
        }

        this.readChannel = readChannel;
        this.writeChannel = writeChannel;
        started = true; //Set started
        EventGeneratorSpi generator = (EventGeneratorSpi) getEventGenerator();
        if (!generator.isStarted())
            generator.start();
        generator.register(this, Constants.EV_REGISTER); //If can't started, will close the session, then the started status become false
        if (block && !generator.isEventGeneratorThread())
            try {
                startLock.wait();
            } catch (InterruptedException e) {
            }
    }
}

From source file:com.alibaba.napoli.gecko.core.nio.impl.Reactor.java

final void unregisterChannel(final SelectableChannel channel) {
    try {/*w  ww. jav a 2 s.c o  m*/
        final Selector selector = this.selector;
        if (selector != null) {
            if (channel != null) {
                final SelectionKey key = channel.keyFor(selector);
                if (key != null) {
                    key.cancel();
                    this.cancelledKeys++;
                }
            }
        }
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
    } catch (final Throwable t) {
        // ignore
    }
    this.wakeup();
}

From source file:org.pvalsecc.misc.SystemUtilities.java

public static void safeClose(SelectableChannel selectableChannel) {
    try {//w w w.  j  a  va  2s .co m
        if (selectableChannel != null)
            selectableChannel.close();
    } catch (IOException e) {
        LOGGER.warn("Cannot close", e);
    }
}