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.
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 {}
- csr of
-
JDK-8241305 Add protocol specific factory creation methods to SocketChannel and ServerSocketChannel
- Resolved