Package java.nio.channels

Examples of java.nio.channels.SelectableChannel


    public int ready() throws IOException {
        if (descriptor.getChannel() instanceof SelectableChannel) {
            int ready_stat = 0;
            java.nio.channels.Selector sel = SelectorFactory.openWithRetryFrom(null, SelectorProvider.provider());;
            SelectableChannel selchan = (SelectableChannel)descriptor.getChannel();
            synchronized (selchan.blockingLock()) {
                boolean is_block = selchan.isBlocking();
                try {
                    selchan.configureBlocking(false);
                    selchan.register(sel, java.nio.channels.SelectionKey.OP_READ);
                    ready_stat = sel.selectNow();
                    sel.close();
                } catch (Throwable ex) {
                } finally {
                    if (sel != null) {
                        try {
                            sel.close();
                        } catch (Exception e) {
                        }
                    }
                    selchan.configureBlocking(is_block);
                }
            }
            return ready_stat;
        } else {
            return newInputStream().available();
View Full Code Here


        if (buf == null || buf.length() == 0) return 0;

        if (buffer.position() != 0 && !flushWrite(false)) return 0;

        if (descriptor.getChannel() instanceof SelectableChannel) {
            SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel();
            synchronized (selectableChannel.blockingLock()) {
                boolean oldBlocking = selectableChannel.isBlocking();
                try {
                    if (oldBlocking) {
                        selectableChannel.configureBlocking(false);
                    }
                    return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
                } finally {
                    if (oldBlocking) {
                        selectableChannel.configureBlocking(oldBlocking);
                    }
                }
            }
        } else {
            return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
View Full Code Here

        if (number == 0) {
            return null;
        }

        if (descriptor.getChannel() instanceof SelectableChannel) {
            SelectableChannel selectableChannel = (SelectableChannel)descriptor.getChannel();
            synchronized (selectableChannel.blockingLock()) {
                boolean oldBlocking = selectableChannel.isBlocking();
                try {
                    selectableChannel.configureBlocking(false);
                    return readpartial(number);
                } finally {
                    selectableChannel.configureBlocking(oldBlocking);
                }
            }
        } else if (descriptor.getChannel() instanceof FileChannel) {
            return fread(number);
        } else {
View Full Code Here

                synchronized(stream) {
                    final int available = stream.bufferedInputBytesRemaining();
                     if (available >= len) {
                        return stream.copyBufferedBytes(bytes, off, len);
                    } else if (stream.getDescriptor().getChannel() instanceof SelectableChannel) {
                        SelectableChannel ch = (SelectableChannel) stream.getDescriptor().getChannel();
                        synchronized (ch.blockingLock()) {
                            boolean oldBlocking = ch.isBlocking();
                            try {
                                if (!oldBlocking) {
                                    ch.configureBlocking(true);
                                }
                                return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
                            } finally {
                                if (!oldBlocking) {
                                    ch.configureBlocking(oldBlocking);
                                }
                            }
                        }
                    } else {
                        return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
View Full Code Here

                synchronized(stream) {
                    if (!stream.isSync() && stream.bufferedOutputSpaceRemaining() >= len) {
                        stream.buffer.put(bytes, off, len);

                    } else if (stream.getDescriptor().getChannel() instanceof SelectableChannel) {
                        SelectableChannel ch = (SelectableChannel) stream.getDescriptor().getChannel();
                        synchronized (ch.blockingLock()) {
                            boolean oldBlocking = ch.isBlocking();
                            try {
                                if (!oldBlocking) {
                                    ch.configureBlocking(true);
                                }
                                stream.bufferedWrite(ByteBuffer.wrap(bytes, off, len));
                            } finally {
                                if (!oldBlocking) {
                                    ch.configureBlocking(oldBlocking);
                                }
                            }
                        }
                    } else {
                        stream.bufferedWrite(ByteBuffer.wrap(bytes, off, len));
View Full Code Here

        return select(channel, io, ops, -1);
    }

    public boolean select(Channel channel, RubyIO io, int ops, long timeout) {
        if (channel instanceof SelectableChannel) {
            SelectableChannel selectable = (SelectableChannel)channel;
           
            synchronized (selectable.blockingLock()) {
                boolean oldBlocking = selectable.isBlocking();

                SelectionKey key = null;
                try {
                    selectable.configureBlocking(false);
                   
                    if (io != null) io.addBlockingThread(this);
                    currentSelector = getRuntime().getSelectorPool().get();

                    key = selectable.register(currentSelector, ops);

                    beforeBlockingCall();
                    int result;
                    if (timeout < 0) {
                        result = currentSelector.select();
                    } else if (timeout == 0) {
                        result = currentSelector.selectNow();
                    } else {
                        result = currentSelector.select(timeout);
                    }

                    // check for thread events, in case we've been woken up to die
                    pollThreadEvents();

                    if (result == 1) {
                        Set<SelectionKey> keySet = currentSelector.selectedKeys();

                        if (keySet.iterator().next() == key) {
                            return true;
                        }
                    }

                    return false;
                } catch (IOException ioe) {
                    throw getRuntime().newRuntimeError("Error with selector: " + ioe);
                } finally {
                    // Note: I don't like ignoring these exceptions, but it's
                    // unclear how likely they are to happen or what damage we
                    // might do by ignoring them. Note that the pieces are separate
                    // so that we can ensure one failing does not affect the others
                    // running.

                    // clean up the key in the selector
                    try {
                        if (key != null) key.cancel();
                        if (currentSelector != null) currentSelector.selectNow();
                    } catch (Exception e) {
                        // ignore
                    }

                    // shut down and null out the selector
                    try {
                        if (currentSelector != null) {
                            getRuntime().getSelectorPool().put(currentSelector);
                        }
                    } catch (Exception e) {
                        // ignore
                    } finally {
                        currentSelector = null;
                    }

                    // remove this thread as a blocker against the given IO
                    if (io != null) io.removeBlockingThread(this);

                    // go back to previous blocking state on the selectable
                    try {
                        selectable.configureBlocking(oldBlocking);
                    } catch (Exception e) {
                        // ignore
                    }

                    // clear thread state from blocking call
View Full Code Here

                OpenFile of = rubyIO.getOpenFile();
                Stream stream = of.getMainStreamSafe();
                if (stream instanceof ChannelStream) {
                    ChannelStream cStream = (ChannelStream)stream;
                    if (cStream.getDescriptor().getChannel() instanceof SelectableChannel)  {
                        SelectableChannel selChannel = (SelectableChannel)cStream.getDescriptor().getChannel();

                        ((RubyObject)recv).extend(
                                new IRubyObject[]{((RubyModule)recv.getRuntime().getModule("Net").getConstant("BufferedIO")).getConstant("NativeImplementation")});
                        SelectableChannel sc = (SelectableChannel)(selChannel);
                        recv.dataWrapStruct(new NativeImpl(sc));
                    }
                }
            }
View Full Code Here

  public void removeConnectionOpenListener(ConnectionOpenListener al) {
    for (SelectionKey key: selector.keys()) {
      if (al == key.attachment()) {
        try {
          key.cancel();
          SelectableChannel channel = key.channel();
          channel.close();
        } catch (Exception e) {
          log.log(Level.WARNING,
            "Exception during removing connection listener.", e);
        }
        break;
View Full Code Here

        return new IoSessionIterator(selector.selectedKeys());
    }

    @Override
    protected void init(NioSession session) throws Exception {
        SelectableChannel ch = (SelectableChannel) session.getChannel();
        ch.configureBlocking(false);
        session.setSelectionKey(ch.register(selector, SelectionKey.OP_READ, session));
    }
View Full Code Here

             */
            @Override
            public boolean onAcceptInterest(SelectionKey key, Context ctx)
                    throws IOException {

                SelectableChannel channel = acceptWithoutRegistration(key);

                if (channel != null) {
                    configureChannel(channel);

                    SelectionKey readKey = channel.register(selector,
                            SelectionKey.OP_READ);

                    // Cache the connection.
                    TargetTuple tt = connectionManager.add(readKey, this);

View Full Code Here

TOP

Related Classes of java.nio.channels.SelectableChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.