mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 14:24:46 +02:00
8343791: Socket.connect API should document whether the socket will be closed when hostname resolution fails or another error occurs
Reviewed-by: dfuchs, alanb
This commit is contained in:
parent
4ac2e477b9
commit
3eb5461578
5 changed files with 226 additions and 42 deletions
|
@ -40,6 +40,7 @@ import java.net.StandardProtocolFamily;
|
|||
import java.net.StandardSocketOptions;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.channels.AlreadyBoundException;
|
||||
import java.nio.channels.AlreadyConnectedException;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.NotYetBoundException;
|
||||
import java.nio.channels.NotYetConnectedException;
|
||||
|
@ -166,6 +167,8 @@ public class Net {
|
|||
nx = newSocketException("Socket is not connected");
|
||||
else if (x instanceof AlreadyBoundException)
|
||||
nx = newSocketException("Already bound");
|
||||
else if (x instanceof AlreadyConnectedException)
|
||||
nx = newSocketException("Already connected");
|
||||
else if (x instanceof NotYetBoundException)
|
||||
nx = newSocketException("Socket is not bound yet");
|
||||
else if (x instanceof UnsupportedAddressTypeException)
|
||||
|
@ -190,32 +193,12 @@ public class Net {
|
|||
return new SocketException(msg);
|
||||
}
|
||||
|
||||
static void translateException(Exception x,
|
||||
boolean unknownHostForUnresolved)
|
||||
throws IOException
|
||||
{
|
||||
static void translateException(Exception x) throws IOException {
|
||||
if (x instanceof IOException ioe)
|
||||
throw ioe;
|
||||
// Throw UnknownHostException from here since it cannot
|
||||
// be thrown as a SocketException
|
||||
if (unknownHostForUnresolved &&
|
||||
(x instanceof UnresolvedAddressException))
|
||||
{
|
||||
throw new UnknownHostException();
|
||||
}
|
||||
translateToSocketException(x);
|
||||
}
|
||||
|
||||
static void translateException(Exception x)
|
||||
throws IOException
|
||||
{
|
||||
translateException(x, false);
|
||||
}
|
||||
|
||||
private static InetSocketAddress getLoopbackAddress(int port) {
|
||||
return new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
|
||||
}
|
||||
|
||||
private static final InetAddress ANY_LOCAL_INET4ADDRESS;
|
||||
private static final InetAddress ANY_LOCAL_INET6ADDRESS;
|
||||
private static final InetAddress INET4_LOOPBACK_ADDRESS;
|
||||
|
|
|
@ -599,8 +599,11 @@ public final class NioSocketImpl extends SocketImpl implements PlatformSocketImp
|
|||
}
|
||||
} catch (IOException ioe) {
|
||||
close();
|
||||
if (ioe instanceof InterruptedIOException) {
|
||||
if (ioe instanceof SocketTimeoutException) {
|
||||
throw ioe;
|
||||
} else if (ioe instanceof InterruptedIOException) {
|
||||
assert Thread.currentThread().isVirtual();
|
||||
throw new SocketException("Closed by interrupt");
|
||||
} else {
|
||||
throw SocketExceptions.of(ioe, isa);
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import java.net.SocketAddress;
|
|||
import java.net.SocketException;
|
||||
import java.net.SocketOption;
|
||||
import java.net.StandardSocketOptions;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -85,6 +86,14 @@ class SocketAdaptor
|
|||
public void connect(SocketAddress remote, int timeout) throws IOException {
|
||||
if (remote == null)
|
||||
throw new IllegalArgumentException("connect: The address can't be null");
|
||||
if (remote instanceof InetSocketAddress isa && isa.isUnresolved()) {
|
||||
if (!sc.isOpen())
|
||||
throw new SocketException("Socket is closed");
|
||||
if (sc.isConnected())
|
||||
throw new SocketException("Already connected");
|
||||
close();
|
||||
throw new UnknownHostException(remote.toString());
|
||||
}
|
||||
if (timeout < 0)
|
||||
throw new IllegalArgumentException("connect: timeout can't be negative");
|
||||
try {
|
||||
|
@ -95,7 +104,7 @@ class SocketAdaptor
|
|||
sc.blockingConnect(remote, Long.MAX_VALUE);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Net.translateException(e, true);
|
||||
Net.translateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue