< prev index next >

src/java.base/share/classes/java/net/SocketImpl.java

Print this page

        

*** 28,38 **** import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.FileDescriptor; import java.util.Set; - import sun.net.PlatformSocketImpl; /** * The abstract class {@code SocketImpl} is a common superclass * of all classes that actually implement sockets. It is used to --- 28,37 ----
*** 73,97 **** * The local port number to which this socket is connected. */ protected int localport; /** - * Whether this is a server or not. - */ - final boolean isServer; - - - SocketImpl(boolean isServer) { - this.isServer = isServer; - } - - /** * Initialize a new instance of this class */ ! public SocketImpl() { ! this.isServer = false; ! } /** * Creates either a stream or a datagram socket. * * @param stream if {@code true}, create a stream socket; --- 72,84 ---- * The local port number to which this socket is connected. */ protected int localport; /** * Initialize a new instance of this class */ ! public SocketImpl() { } /** * Creates either a stream or a datagram socket. * * @param stream if {@code true}, create a stream socket;
*** 374,456 **** } /** * Called to set a socket option. * * @param <T> The type of the socket option value * @param name The socket option - * * @param value The value of the socket option. A value of {@code null} * may be valid for some options. * * @throws UnsupportedOperationException if the SocketImpl does not * support the option ! * ! * @throws IOException if an I/O error occurs, or if the socket is closed. * * @since 9 */ protected <T> void setOption(SocketOption<T> name, T value) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && !isServer) { ! setOption(SocketOptions.SO_KEEPALIVE, value); ! } else if (name == StandardSocketOptions.SO_SNDBUF && !isServer) { ! setOption(SocketOptions.SO_SNDBUF, value); ! } else if (name == StandardSocketOptions.SO_RCVBUF) { ! setOption(SocketOptions.SO_RCVBUF, value); ! } else if (name == StandardSocketOptions.SO_REUSEADDR) { ! setOption(SocketOptions.SO_REUSEADDR, value); ! } else if (name == StandardSocketOptions.SO_REUSEPORT && ! supportedOptions().contains(name)) { ! setOption(SocketOptions.SO_REUSEPORT, value); ! } else if (name == StandardSocketOptions.SO_LINGER && !isServer) { ! setOption(SocketOptions.SO_LINGER, value); ! } else if (name == StandardSocketOptions.IP_TOS) { ! setOption(SocketOptions.IP_TOS, value); ! } else if (name == StandardSocketOptions.TCP_NODELAY && !isServer) { ! setOption(SocketOptions.TCP_NODELAY, value); ! } else { ! throw new UnsupportedOperationException("unsupported option"); ! } } /** * Called to get a socket option. * * @param <T> The type of the socket option value * @param name The socket option - * * @return the value of the named option * * @throws UnsupportedOperationException if the SocketImpl does not ! * support the option. ! * ! * @throws IOException if an I/O error occurs, or if the socket is closed. * * @since 9 */ - @SuppressWarnings("unchecked") protected <T> T getOption(SocketOption<T> name) throws IOException { ! if (name == StandardSocketOptions.SO_KEEPALIVE && !isServer) { ! return (T)getOption(SocketOptions.SO_KEEPALIVE); ! } else if (name == StandardSocketOptions.SO_SNDBUF && !isServer) { ! return (T)getOption(SocketOptions.SO_SNDBUF); ! } else if (name == StandardSocketOptions.SO_RCVBUF) { ! return (T)getOption(SocketOptions.SO_RCVBUF); ! } else if (name == StandardSocketOptions.SO_REUSEADDR) { ! return (T)getOption(SocketOptions.SO_REUSEADDR); ! } else if (name == StandardSocketOptions.SO_REUSEPORT && ! supportedOptions().contains(name)) { ! return (T)getOption(SocketOptions.SO_REUSEPORT); ! } else if (name == StandardSocketOptions.SO_LINGER && !isServer) { ! return (T)getOption(SocketOptions.SO_LINGER); ! } else if (name == StandardSocketOptions.IP_TOS) { ! return (T)getOption(SocketOptions.IP_TOS); ! } else if (name == StandardSocketOptions.TCP_NODELAY && !isServer) { ! return (T)getOption(SocketOptions.TCP_NODELAY); ! } else { ! throw new UnsupportedOperationException("unsupported option"); ! } } /** * Attempts to copy socket options from this SocketImpl to a target SocketImpl. * At this time, only the SO_TIMEOUT make sense to copy. --- 361,414 ---- } /** * Called to set a socket option. * + * @implSpec + * The default implementation of this method throws {@code + * UnsupportedOperationException}. Subclasses should override this method + * with an appropriate implementation. + * * @param <T> The type of the socket option value * @param name The socket option * @param value The value of the socket option. A value of {@code null} * may be valid for some options. * * @throws UnsupportedOperationException if the SocketImpl does not * support the option ! * @throws IllegalArgumentException if the value is not valid for ! * the option ! * @throws IOException if an I/O error occurs, or if the socket is closed ! * @throws NullPointerException if name is {@code null} * * @since 9 */ protected <T> void setOption(SocketOption<T> name, T value) throws IOException { ! throw new UnsupportedOperationException("'" + name + "' not supported"); } /** * Called to get a socket option. * + * @implSpec + * The default implementation of this method throws {@code + * UnsupportedOperationException}. Subclasses should override this method + * with an appropriate implementation. + * * @param <T> The type of the socket option value * @param name The socket option * @return the value of the named option * * @throws UnsupportedOperationException if the SocketImpl does not ! * support the option ! * @throws IOException if an I/O error occurs, or if the socket is closed ! * @throws NullPointerException if name is {@code null} * * @since 9 */ protected <T> T getOption(SocketOption<T> name) throws IOException { ! throw new UnsupportedOperationException("'" + name + "' not supported"); } /** * Attempts to copy socket options from this SocketImpl to a target SocketImpl. * At this time, only the SO_TIMEOUT make sense to copy.
*** 462,500 **** target.setOption(SocketOptions.SO_TIMEOUT, timeout); } } catch (IOException ignore) { } } - private static final Set<SocketOption<?>> socketOptions; - - private static final Set<SocketOption<?>> serverSocketOptions; - - static { - socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE, - StandardSocketOptions.SO_SNDBUF, - StandardSocketOptions.SO_RCVBUF, - StandardSocketOptions.SO_REUSEADDR, - StandardSocketOptions.SO_LINGER, - StandardSocketOptions.IP_TOS, - StandardSocketOptions.TCP_NODELAY); - - serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF, - StandardSocketOptions.SO_REUSEADDR, - StandardSocketOptions.IP_TOS); - } - /** * Returns a set of SocketOptions supported by this impl * and by this impl's socket (Socket or ServerSocket) * * @return a Set of SocketOptions * * @since 9 */ protected Set<SocketOption<?>> supportedOptions() { ! if (!isServer) { ! return socketOptions; ! } else { ! return serverSocketOptions; ! } } } --- 420,440 ---- target.setOption(SocketOptions.SO_TIMEOUT, timeout); } } catch (IOException ignore) { } } /** * Returns a set of SocketOptions supported by this impl * and by this impl's socket (Socket or ServerSocket) * + * @implSpec + * The default implementation of this method returns an empty set. + * Subclasses should override this method with an appropriate implementation. + * * @return a Set of SocketOptions * * @since 9 */ protected Set<SocketOption<?>> supportedOptions() { ! return Set.of(); } }
< prev index next >