JDK-8242926 : Add protocol specific factory creation methods to SocketChannel and ServerSocketChannel
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.nio
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 15
  • Submitted: 2020-04-16
  • Updated: 2020-04-24
  • Resolved: 2020-04-24
Related Reports
CSR :  
Description
Summary
-------

Add static factory creation methods to `java.nio.channels.SocketChannel` and `java.nio.channels.ServerSocketChannel` which take a `java.net.ProtocolFamily`. These will allow channels to be created IPv4 only or IPv4/IPv6 regardless of the VM wide setting of the `"java.net.preferIPv4Stack"` system property. It will also facilitate the support of future non-IP protocol families as anticipated in [JEP 380](https://openjdk.java.net/jeps/380).

Problem
-------

SocketChannel and ServerSocketChannel do not allow to specify a protocol family when opening a new channel. `DatagramChannel` has had this capability since Java 1.7. For consistency, it should be available for TCP sockets as well as UDP sockets.

Solution
--------

The solution is straightforward and basically identical to that previously added to `DatagramChannel`, namely the addition of one new static factory method to `SocketChannel` and `ServerSocketChannel`, and the equivalent supporting methods to `SelectorProvider`.

Specification
-------------

Add the following new method to java.nio.channels.SocketChannel

    /**
     * Opens a socket channel. The {@code family} parameter specifies the
     * {@link ProtocolFamily protocol family} of the channel's socket.
     *
     * <p> The new channel is created by invoking the {@link
     * java.nio.channels.spi.SelectorProvider#openSocketChannel(ProtocolFamily)
     * openSocketChannel(ProtocolFamily)} method of the system-wide default.
     * {@link java.nio.channels.spi.SelectorProvider} object. </p>
     *
     * @param   family
     *          The protocol family
     *
     * @return  A new socket channel
     *
     * @throws  UnsupportedOperationException
     *          If the specified protocol family is not supported. For example,
     *          suppose the parameter is specified as {@link
     *          java.net.StandardProtocolFamily#INET6 StandardProtocolFamily.INET6}
     *          but IPv6 is not enabled on the platform.
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 15
     */

    public static SocketChannel open(ProtocolFamily family) throws IOException {}

Add the following new method to java.nio.channels.ServerSocketChannel

    /**
     * Opens a server-socket channel.The {@code family} parameter specifies the
     * {@link ProtocolFamily protocol family} of the channel's socket.
     *
     * <p> The new channel is created by invoking the {@link
     * java.nio.channels.spi.SelectorProvider#openServerSocketChannel(ProtocolFamily)
     * openServerSocketChannel(ProtocolFamily)} method of the system-wide default {@link
     * java.nio.channels.spi.SelectorProvider} object. </p>
     *
     * @param   family
     *          The protocol family
     *
     * @return  A new socket channel
     *
     * @throws  UnsupportedOperationException
     *          If the specified protocol family is not supported. For example,
     *          suppose the parameter is specified as {@link
     *          java.net.StandardProtocolFamily#INET6 StandardProtocolFamily.INET6}
     *          but IPv6 is not enabled on the platform.
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 15
     */
    public static ServerSocketChannel open(ProtocolFamily family) throws IOException {}

Add the following methods to java.nio.channels.spi.SelectorProvider

    /**
     * Opens a socket channel.
     *
     * @implSpec The default implementation of this method first checks that
     *          the given protocol {@code family} is not {@code null},
     *          then throws {@link UnsupportedOperationException}.
     *
     * @param   family
     *          The protocol family
     *
     * @return  The new channel
     *
     * @throws  UnsupportedOperationException
     *          If the specified protocol family is not supported
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 15
     */
    public SocketChannel openSocketChannel(ProtocolFamily family) throws IOException {}

    /**
     * Opens a server-socket channel.
     *
     * @implSpec The default implementation of this method first checks that
     *          the given protocol {@code family} is not {@code null},
     *          then throws {@link UnsupportedOperationException}.
     *
     * @param   family
     *          The protocol family
     *
     * @return  The new channel
     *
     * @throws  UnsupportedOperationException
     *          If the specified protocol family is not supported
     * @throws  IOException
     *          If an I/O error occurs
     *
     * @since 15
     */
    public ServerSocketChannel openServerSocketChannel(ProtocolFamily family) throws IOException {}

Comments
Thanks for the additional information; moving to Approved.
24-04-2020

There is a blanket statement for null handling in the java.nio.channels and java.nio.channels.spi package descriptions so that none of the methods on classes in these packages need to be specify that they throw NPE when with parameters that are null.
24-04-2020

Moving to Provisional. Please discuss how a null ProtocolFamily should be handled before the request is re-Finalized. (I didn't see blanket null-handling statements at a class level for the classes in question.)
23-04-2020