--- a/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java Tue Feb 27 23:11:26 2018 -0800
+++ b/src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java Wed Feb 28 09:54:38 2018 +0000
@@ -41,6 +41,7 @@
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.IllegalBlockingModeException;
+import java.util.Objects;
// Make a datagram-socket channel look like a datagram socket.
@@ -53,7 +54,6 @@
public class DatagramSocketAdaptor
extends DatagramSocket
{
-
// The channel being adapted
private final DatagramChannelImpl dc;
@@ -63,7 +63,7 @@
// ## super will create a useless impl
private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
// Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
- // passing a dummy DatagramSocketImpl object to aovid any native
+ // passing a dummy DatagramSocketImpl object to avoid any native
// resource allocation in super class and invoking our bind method
// before the dc field is initialized.
super(dummyDatagramSocket);
@@ -87,10 +87,10 @@
throw new IllegalArgumentException("connect: " + port);
if (remote == null)
throw new IllegalArgumentException("connect: null address");
- if (isClosed())
- return;
try {
dc.connect(remote);
+ } catch (ClosedChannelException e) {
+ // ignore
} catch (Exception x) {
Net.translateToSocketException(x);
}
@@ -115,8 +115,7 @@
}
public void connect(SocketAddress remote) throws SocketException {
- if (remote == null)
- throw new IllegalArgumentException("Address can't be null");
+ Objects.requireNonNull(remote, "Address can't be null");
connectInternal(remote);
}
@@ -137,15 +136,13 @@
}
public InetAddress getInetAddress() {
- return (isConnected()
- ? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
- : null);
+ InetSocketAddress remote = dc.remoteAddress();
+ return (remote != null) ? remote.getAddress() : null;
}
public int getPort() {
- return (isConnected()
- ? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
- : -1);
+ InetSocketAddress remote = dc.remoteAddress();
+ return (remote != null) ? remote.getPort() : -1;
}
public void send(DatagramPacket p) throws IOException {
@@ -161,8 +158,7 @@
if (p.getAddress() == null) {
// Legacy DatagramSocket will send in this case
// and set address and port of the packet
- InetSocketAddress isa = (InetSocketAddress)
- dc.remoteAddress();
+ InetSocketAddress isa = dc.remoteAddress();
p.setPort(isa.getPort());
p.setAddress(isa.getAddress());
dc.write(bb);
@@ -181,36 +177,24 @@
}
}
- // Must hold dc.blockingLock()
- //
private SocketAddress receive(ByteBuffer bb) throws IOException {
- if (timeout == 0) {
- return dc.receive(bb);
- }
+ assert Thread.holdsLock(dc.blockingLock()) && dc.isBlocking();
- dc.configureBlocking(false);
- try {
- SocketAddress sender;
- if ((sender = dc.receive(bb)) != null)
- return sender;
- long to = timeout;
+ long to = this.timeout;
+ if (to == 0) {
+ return dc.receive(bb);
+ } else {
for (;;) {
if (!dc.isOpen())
- throw new ClosedChannelException();
+ throw new ClosedChannelException();
long st = System.currentTimeMillis();
- int result = dc.poll(Net.POLLIN, to);
- if (result > 0 && ((result & Net.POLLIN) != 0)) {
- if ((sender = dc.receive(bb)) != null)
- return sender;
+ if (dc.pollRead(to)) {
+ return dc.receive(bb);
}
to -= System.currentTimeMillis() - st;
if (to <= 0)
throw new SocketTimeoutException();
}
- } finally {
- try {
- dc.configureBlocking(true);
- } catch (ClosedChannelException e) { }
}
}
@@ -236,10 +220,10 @@
public InetAddress getLocalAddress() {
if (isClosed())
return null;
- SocketAddress local = dc.localAddress();
+ InetSocketAddress local = dc.localAddress();
if (local == null)
local = new InetSocketAddress(0);
- InetAddress result = ((InetSocketAddress)local).getAddress();
+ InetAddress result = local.getAddress();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
@@ -255,9 +239,9 @@
if (isClosed())
return -1;
try {
- SocketAddress local = dc.getLocalAddress();
+ InetSocketAddress local = dc.localAddress();
if (local != null) {
- return ((InetSocketAddress)local).getPort();
+ return local.getPort();
}
} catch (Exception x) {
}