类java.net.SocketOption源码实例Demo

下面列出了怎么用java.net.SocketOption的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
@Override
public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (writeShutdown)
            throw new IOException("Connection has been shutdown for writing");
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            isReuseAddress = (Boolean)value;
        } else {
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
        }
        return this;
    } finally {
        end();
    }
}
 
源代码3 项目: Bytecoder   文件: ExtendedSocketOptions.java
protected ExtendedSocketOptions(Set<SocketOption<?>> options) {
    this.options = options;
    var datagramOptions = new HashSet<SocketOption<?>>();
    var serverStreamOptions = new HashSet<SocketOption<?>>();
    var clientStreamOptions = new HashSet<SocketOption<?>>();
    for (var option : options) {
        if (isDatagramOption(option)) {
            datagramOptions.add(option);
        }
        if (isStreamOption(option, true)) {
            serverStreamOptions.add(option);
        }
        if (isStreamOption(option, false)) {
            clientStreamOptions.add(option);
        }
    }
    this.datagramOptions = Set.copyOf(datagramOptions);
    this.serverStreamOptions = Set.copyOf(serverStreamOptions);
    this.clientStreamOptions = Set.copyOf(clientStreamOptions);
}
 
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
@Override
@SuppressWarnings("unchecked")
public final <T> T getOption(SocketOption<T> name) throws IOException {
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            return (T)Boolean.valueOf(isReuseAddress);
        }
        return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
    } finally {
        end();
    }
}
 
源代码7 项目: jdk8u60   文件: AsynchronousSocketChannelImpl.java
@Override
public final <T> AsynchronousSocketChannel setOption(SocketOption<T> name, T value)
    throws IOException
{
    if (name == null)
        throw new NullPointerException();
    if (!supportedOptions().contains(name))
        throw new UnsupportedOperationException("'" + name + "' not supported");

    try {
        begin();
        if (writeShutdown)
            throw new IOException("Connection has been shutdown for writing");
        if (name == StandardSocketOptions.SO_REUSEADDR &&
                Net.useExclusiveBind())
        {
            // SO_REUSEADDR emulated when using exclusive bind
            isReuseAddress = (Boolean)value;
        } else {
            Net.setSocketOption(fd, Net.UNSPEC, name, value);
        }
        return this;
    } finally {
        end();
    }
}
 
源代码8 项目: openjdk-jdk9   文件: ExtendedSocketOptions.java
@Override
public Object getOption(FileDescriptor fd,
                        SocketOption<?> option)
    throws SocketException
{
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
        sm.checkPermission(new NetworkPermission("getOption." + option.name()));

    if (fd == null || !fd.valid())
        throw new SocketException("socket closed");

    if (option == SO_FLOW_SLA) {
        assert flowSupported;
        SocketFlow flow = SocketFlow.create();
        getFlowOption(fd, flow);
        return flow;
    } else {
        throw new InternalError("Unexpected option " + option);
    }
}
 
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.add(StandardSocketOptions.TCP_NODELAY);
    ExtendedSocketOptions extendedOptions =
            ExtendedSocketOptions.getInstance();
    set.addAll(extendedOptions.options());
    return Collections.unmodifiableSet(set);
}
 
源代码10 项目: openjdk-jdk8u   文件: SocketAdaptor.java
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        sc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
源代码11 项目: Bytecoder   文件: DatagramSocketAdaptor.java
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return dc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    set.add(StandardSocketOptions.TCP_NODELAY);
    if (ExtendedOptionsImpl.flowSupported()) {
        set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
    }
    return Collections.unmodifiableSet(set);
}
 
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(2);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.addAll(ExtendedSocketOptions.serverSocketOptions());
    return Collections.unmodifiableSet(set);
}
 
源代码14 项目: jdk8u_jdk   文件: SocketAdaptor.java
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
    try {
        return sc.getOption(name).booleanValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return false;       // keep compiler happy
    }
}
 
源代码15 项目: dragonwell8_jdk   文件: DatagramSocketAdaptor.java
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
源代码16 项目: Bytecoder   文件: AsynchronousSocketChannelImpl.java
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    if (Net.isReusePortAvailable()) {
        set.add(StandardSocketOptions.SO_REUSEPORT);
    }
    set.add(StandardSocketOptions.TCP_NODELAY);
    set.addAll(ExtendedSocketOptions.clientSocketOptions());
    return Collections.unmodifiableSet(set);
}
 
源代码17 项目: pinpoint   文件: ReusePortSocketOptionHolder.java
public static ReusePortSocketOptionHolder create(int socketCount) {
    SocketOption reusePortSocketOption = getReusePortSocketOption();
    if (reusePortSocketOption == null) {
        LOGGER.warn("Failed to get ReusePort SocketOption. Please use Jvm9+ for using ReusePort SocketOption");
        return null;
    }

    return new ReusePortSocketOptionHolder(reusePortSocketOption, true, socketCount);
}
 
源代码18 项目: jdk8u_jdk   文件: SocketAdaptor.java
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return sc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
源代码19 项目: TencentKona-8   文件: DatagramSocketAdaptor.java
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
源代码20 项目: TencentKona-8   文件: DatagramSocketAdaptor.java
private void setIntOption(SocketOption<Integer> name, int value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
源代码21 项目: TencentKona-8   文件: DatagramSocketAdaptor.java
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return dc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
源代码22 项目: Bytecoder   文件: ExtendedSocketOptions.java
private Set<SocketOption<?>> options0(short type, boolean server) {
    switch (type) {
        case SOCK_DGRAM:
            return datagramOptions;
        case SOCK_STREAM:
            if (server) {
                return serverStreamOptions;
            } else {
                return clientStreamOptions;
            }
        default:
            //this will never happen
            throw new IllegalArgumentException("Invalid socket option type");
    }
}
 
源代码23 项目: openjdk-jdk8u   文件: DatagramSocketAdaptor.java
private void setBooleanOption(SocketOption<Boolean> name, boolean value)
    throws SocketException
{
    try {
        dc.setOption(name, value);
    } catch (IOException x) {
        Net.translateToSocketException(x);
    }
}
 
源代码24 项目: openjdk-jdk8u   文件: SocketAdaptor.java
private int getIntOption(SocketOption<Integer> name) throws SocketException {
    try {
        return sc.getOption(name).intValue();
    } catch (IOException x) {
        Net.translateToSocketException(x);
        return -1;          // keep compiler happy
    }
}
 
源代码25 项目: hottub   文件: AsynchronousSocketChannelImpl.java
private static Set<SocketOption<?>> defaultOptions() {
    HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(5);
    set.add(StandardSocketOptions.SO_SNDBUF);
    set.add(StandardSocketOptions.SO_RCVBUF);
    set.add(StandardSocketOptions.SO_KEEPALIVE);
    set.add(StandardSocketOptions.SO_REUSEADDR);
    set.add(StandardSocketOptions.TCP_NODELAY);
    if (ExtendedOptionsImpl.flowSupported()) {
        set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
    }
    return Collections.unmodifiableSet(set);
}
 
源代码26 项目: jmeter-plugins   文件: ServerSocketChannelEmul.java
@Override
public <T> T getOption(SocketOption<T> socketOption) throws IOException {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
源代码27 项目: Bytecoder   文件: SocketAdaptor.java
@Override
public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
    sc.setOption(name, value);
    return this;
}
 
源代码28 项目: trufflesqueak   文件: SqueakSocket.java
protected final SocketOption<?> socketOptionFromString(final String name) {
    return asNetworkChannel().supportedOptions().stream().filter(o -> o.name().equals(name)).findFirst().orElseThrow(() -> new UnsupportedOperationException("Unknown socket option: " + name));
}
 
@Override
public Set<SocketOption<?>> supportedOptions() {
    return asynchronousSocketChannel.supportedOptions();
}
 
源代码30 项目: j2objc   文件: SocketOptionRegistry.java
RegistryKey(SocketOption<?> name, ProtocolFamily family) {             
    this.name = name;                                                  
    this.family = family;                                              
}
 
 类所在包
 类方法
 同包方法