Search in sources :

Example 6 with Channel

use of java.nio.channels.Channel in project netty by netty.

the class DatagramUnicastIPv6Test method assumeIpv6Supported.

@SuppressJava6Requirement(reason = "Guarded by java version check")
@BeforeAll
public static void assumeIpv6Supported() {
    try {
        if (PlatformDependent.javaVersion() < 7) {
            throw new UnsupportedOperationException();
        }
        Channel channel = SelectorProvider.provider().openDatagramChannel(StandardProtocolFamily.INET6);
        channel.close();
    } catch (UnsupportedOperationException e) {
        throw new AssumptionViolatedException("IPv6 not supported", e);
    } catch (IOException ignore) {
    // Ignore
    }
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Channel(java.nio.channels.Channel) IOException(java.io.IOException) BeforeAll(org.junit.jupiter.api.BeforeAll) SuppressJava6Requirement(io.netty.util.internal.SuppressJava6Requirement)

Example 7 with Channel

use of java.nio.channels.Channel in project netty by netty.

the class TestUtils method isSctpSupported.

/**
 * Return {@code true} if SCTP is supported by the running os.
 */
public static boolean isSctpSupported() {
    String os = System.getProperty("os.name").toLowerCase(Locale.US);
    if ("unix".equals(os) || "linux".equals(os) || "sun".equals(os) || "solaris".equals(os)) {
        try {
            // Try to open a SCTP Channel, by using reflection to make it compile also on
            // operation systems that not support SCTP like OSX and Windows
            Class<?> sctpChannelClass = Class.forName("com.sun.nio.sctp.SctpChannel");
            Channel channel = (Channel) sctpChannelClass.getMethod("open").invoke(null);
            try {
                channel.close();
            } catch (IOException e) {
            // ignore
            }
        } catch (UnsupportedOperationException e) {
            // This exception may get thrown if the OS does not have
            // the shared libs installed.
            System.out.print("Not supported: " + e.getMessage());
            return false;
        } catch (Throwable t) {
            if (!(t instanceof IOException)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Example 8 with Channel

use of java.nio.channels.Channel in project j2objc by google.

the class InheritedChannel method createChannel.

/*
     * If standard inherited channel is connected to a socket then return a Channel
     * of the appropriate type based standard input.
     */
private static Channel createChannel() throws IOException {
    // dup the file descriptor - we do this so that for two reasons :-
    // 1. Avoids any timing issues with FileDescriptor.in being closed
    // or redirected while we create the channel.
    // 2. Allows streams based on file descriptor 0 to co-exist with
    // the channel (closing one doesn't impact the other)
    int fdVal = dup(0);
    // Examine the file descriptor - if it's not a socket then we don't
    // create a channel so we release the file descriptor.
    int st;
    st = soType0(fdVal);
    if (st != SOCK_STREAM && st != SOCK_DGRAM) {
        close0(fdVal);
        return null;
    }
    // Next we create a FileDescriptor for the dup'ed file descriptor
    // Have to use reflection and also make assumption on how FD
    // is implemented.
    Class[] paramTypes = { int.class };
    Constructor ctr = Reflect.lookupConstructor("java.io.FileDescriptor", paramTypes);
    Object[] args = { new Integer(fdVal) };
    FileDescriptor fd = (FileDescriptor) Reflect.invoke(ctr, args);
    // Now create the channel. If the socket is a streams socket then
    // we see if tthere is a peer (ie: connected). If so, then we
    // create a SocketChannel, otherwise a ServerSocketChannel.
    // If the socket is a datagram socket then create a DatagramChannel
    SelectorProvider provider = SelectorProvider.provider();
    assert provider instanceof sun.nio.ch.SelectorProviderImpl;
    Channel c;
    if (st == SOCK_STREAM) {
        InetAddress ia = peerAddress0(fdVal);
        if (ia == null) {
            c = new InheritedServerSocketChannelImpl(provider, fd);
        } else {
            int port = peerPort0(fdVal);
            assert port > 0;
            InetSocketAddress isa = new InetSocketAddress(ia, port);
            c = new InheritedSocketChannelImpl(provider, fd, isa);
        }
    } else {
        c = new InheritedDatagramChannelImpl(provider, fd);
    }
    return c;
}
Also used : Constructor(java.lang.reflect.Constructor) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) DatagramChannel(java.nio.channels.DatagramChannel) Channel(java.nio.channels.Channel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) FileDescriptor(java.io.FileDescriptor) SelectorProvider(java.nio.channels.spi.SelectorProvider) InetAddress(java.net.InetAddress)

Example 9 with Channel

use of java.nio.channels.Channel in project jstorm by alibaba.

the class NimbusData method createFileHandler.

public void createFileHandler() {
    ExpiredCallback<Object, Object> expiredCallback = new ExpiredCallback<Object, Object>() {

        @Override
        public void expire(Object key, Object val) {
            try {
                LOG.info("Close file " + String.valueOf(key));
                if (val != null) {
                    if (val instanceof Channel) {
                        Channel channel = (Channel) val;
                        channel.close();
                    } else if (val instanceof BufferFileInputStream) {
                        BufferFileInputStream is = (BufferFileInputStream) val;
                        is.close();
                    }
                }
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
            }
        }
    };
    int file_copy_expiration_secs = JStormUtils.parseInt(conf.get(Config.NIMBUS_FILE_COPY_EXPIRATION_SECS), 30);
    uploaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
    downloaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
}
Also used : ExpiredCallback(com.alibaba.jstorm.utils.ExpiredCallback) Channel(java.nio.channels.Channel) IOException(java.io.IOException) BufferFileInputStream(backtype.storm.utils.BufferFileInputStream)

Example 10 with Channel

use of java.nio.channels.Channel in project zm-mailbox by Zimbra.

the class Server method shutdown.

/**
 * Cleans up and shuts down the server thread.
 */
public synchronized void shutdown() {
    shutdown = true;
    for (SelectionKey key : selector.keys()) {
        try {
            key.cancel();
            Channel channel = key.channel();
            log.debug("server:closing channel %s", channel);
            channel.close();
        } catch (IOException e) {
            log.debug("shutdown", e);
        }
    }
    try {
        selector.close();
    } catch (IOException e) {
        log.debug("shutdown", e);
    }
    try {
        sschannel.close();
    } catch (IOException e) {
        log.debug("shutdown", e);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Channel(java.nio.channels.Channel) IOException(java.io.IOException)

Aggregations

Channel (java.nio.channels.Channel)33 IOException (java.io.IOException)22 FileChannel (java.nio.channels.FileChannel)10 ReadableByteChannel (java.nio.channels.ReadableByteChannel)8 SocketChannel (java.nio.channels.SocketChannel)7 WritableByteChannel (java.nio.channels.WritableByteChannel)7 InputStream (java.io.InputStream)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)5 FileNotFoundException (java.io.FileNotFoundException)4 InetSocketAddress (java.net.InetSocketAddress)4 ArrayList (java.util.ArrayList)4 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)4 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)4 DataFile (edu.harvard.iq.dataverse.DataFile)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 List (java.util.List)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3