Skip to content

Commit 9771544

Browse files
committed
8260428: Drop support for pre JDK 1.4 DatagramSocketImpl implementations
Reviewed-by: alanb, dfuchs, vtewari
1 parent e265f83 commit 9771544

File tree

3 files changed

+212
-93
lines changed

3 files changed

+212
-93
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.FileDescriptor;
2929
import java.io.IOException;
30+
import java.io.UncheckedIOException;
3031
import java.util.Objects;
3132
import java.util.Set;
3233

@@ -99,19 +100,30 @@ int dataAvailable() {
99100
* packet has been received for that address, then a subsequent call to
100101
* send or receive may throw a PortUnreachableException.
101102
* Note, there is no guarantee that the exception will be thrown.
103+
*
104+
* @implSpec The default implementation of this method throws {@code SocketException}.
105+
*
102106
* @param address the remote InetAddress to connect to
103107
* @param port the remote port number
104108
* @throws SocketException may be thrown if the socket cannot be
105109
* connected to the remote destination
106110
* @since 1.4
107111
*/
108-
protected void connect(InetAddress address, int port) throws SocketException {}
112+
protected void connect(InetAddress address, int port) throws SocketException {
113+
throw new SocketException("connect not implemented");
114+
}
109115

110116
/**
111117
* Disconnects a datagram socket from its remote destination.
118+
*
119+
* @implSpec The default implementation of this method throws {@code UncheckedIOException}.
120+
*
121+
* @throws UncheckedIOException if disconnect fails or no implementation is provided
112122
* @since 1.4
113123
*/
114-
protected void disconnect() {}
124+
protected void disconnect() {
125+
throw new UncheckedIOException(new SocketException("disconnect not implemented"));
126+
}
115127

116128
/**
117129
* Peek at the packet to see who it is from. Updates the specified {@code InetAddress}

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

Lines changed: 20 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ final class NetMulticastSocket extends MulticastSocket {
5656
*/
5757
private final DatagramSocketImpl impl;
5858

59-
/**
60-
* Are we using an older DatagramSocketImpl?
61-
*/
62-
private final boolean oldImpl;
63-
6459
/**
6560
* Set when a socket is ST_CONNECTED until we are certain
6661
* that any packets which might have been received prior
@@ -76,11 +71,9 @@ final class NetMulticastSocket extends MulticastSocket {
7671
* Connection state:
7772
* ST_NOT_CONNECTED = socket not connected
7873
* ST_CONNECTED = socket connected
79-
* ST_CONNECTED_NO_IMPL = socket connected but not at impl level
8074
*/
8175
static final int ST_NOT_CONNECTED = 0;
8276
static final int ST_CONNECTED = 1;
83-
static final int ST_CONNECTED_NO_IMPL = 2;
8477

8578
int connectState = ST_NOT_CONNECTED;
8679

@@ -97,7 +90,6 @@ final class NetMulticastSocket extends MulticastSocket {
9790
NetMulticastSocket(DatagramSocketImpl impl) {
9891
super((MulticastSocket) null);
9992
this.impl = Objects.requireNonNull(impl);
100-
this.oldImpl = checkOldImpl(impl);
10193
}
10294

10395
/**
@@ -135,59 +127,24 @@ private synchronized void connectInternal(InetAddress address, int port) throws
135127
if (!isBound())
136128
bind(new InetSocketAddress(0));
137129

138-
// old impls do not support connect/disconnect
139-
if (oldImpl) {
140-
connectState = ST_CONNECTED_NO_IMPL;
141-
} else {
142-
try {
143-
getImpl().connect(address, port);
144-
145-
// socket is now connected by the impl
146-
connectState = ST_CONNECTED;
147-
// Do we need to filter some packets?
148-
int avail = getImpl().dataAvailable();
149-
if (avail == -1) {
150-
throw new SocketException();
151-
}
152-
explicitFilter = avail > 0;
153-
if (explicitFilter) {
154-
bytesLeftToFilter = getReceiveBufferSize();
155-
}
156-
} catch (SocketException se) {
130+
getImpl().connect(address, port);
157131

158-
// connection will be emulated by DatagramSocket
159-
connectState = ST_CONNECTED_NO_IMPL;
160-
}
132+
// socket is now connected by the impl
133+
connectState = ST_CONNECTED;
134+
// Do we need to filter some packets?
135+
int avail = getImpl().dataAvailable();
136+
if (avail == -1) {
137+
throw new SocketException();
138+
}
139+
explicitFilter = avail > 0;
140+
if (explicitFilter) {
141+
bytesLeftToFilter = getReceiveBufferSize();
161142
}
162143

163144
connectedAddress = address;
164145
connectedPort = port;
165146
}
166147

167-
/**
168-
* Return true if the given DatagramSocketImpl is an "old" impl. An old impl
169-
* is one that doesn't implement the abstract methods added in Java SE 1.4.
170-
*/
171-
@SuppressWarnings("removal")
172-
private static boolean checkOldImpl(DatagramSocketImpl impl) {
173-
// DatagramSocketImpl.peekData() is a protected method, therefore we need to use
174-
// getDeclaredMethod, therefore we need permission to access the member
175-
try {
176-
AccessController.doPrivileged(
177-
new PrivilegedExceptionAction<>() {
178-
public Void run() throws NoSuchMethodException {
179-
Class<?>[] cl = new Class<?>[1];
180-
cl[0] = DatagramPacket.class;
181-
impl.getClass().getDeclaredMethod("peekData", cl);
182-
return null;
183-
}
184-
});
185-
return false;
186-
} catch (java.security.PrivilegedActionException e) {
187-
return true;
188-
}
189-
}
190-
191148
/**
192149
* Return the {@code DatagramSocketImpl} attached to this socket,
193150
* creating the socket if not already created.
@@ -382,19 +339,11 @@ public synchronized void receive(DatagramPacket p) throws IOException {
382339
SecurityManager security = System.getSecurityManager();
383340
if (security != null) {
384341
while (true) {
385-
String peekAd = null;
386342
int peekPort = 0;
387343
// peek at the packet to see who it is from.
388-
if (!oldImpl) {
389-
// We can use the new peekData() API
390-
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
391-
peekPort = getImpl().peekData(peekPacket);
392-
peekAd = peekPacket.getAddress().getHostAddress();
393-
} else {
394-
InetAddress adr = new InetAddress();
395-
peekPort = getImpl().peek(adr);
396-
peekAd = adr.getHostAddress();
397-
}
344+
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
345+
peekPort = getImpl().peekData(peekPacket);
346+
String peekAd = peekPacket.getAddress().getHostAddress();
398347
try {
399348
security.checkAccept(peekAd, peekPort);
400349
// security check succeeded - so now break
@@ -418,29 +367,19 @@ public synchronized void receive(DatagramPacket p) throws IOException {
418367
}
419368
}
420369
DatagramPacket tmp = null;
421-
if ((connectState == ST_CONNECTED_NO_IMPL) || explicitFilter) {
370+
if (explicitFilter) {
422371
// We have to do the filtering the old fashioned way since
423372
// the native impl doesn't support connect or the connect
424373
// via the impl failed, or .. "explicitFilter" may be set when
425374
// a socket is connected via the impl, for a period of time
426375
// when packets from other sources might be queued on socket.
427376
boolean stop = false;
428377
while (!stop) {
429-
InetAddress peekAddress = null;
430-
int peekPort = -1;
431378
// peek at the packet to see who it is from.
432-
if (!oldImpl) {
433-
// We can use the new peekData() API
434-
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
435-
peekPort = getImpl().peekData(peekPacket);
436-
peekAddress = peekPacket.getAddress();
437-
} else {
438-
// this api only works for IPv4
439-
peekAddress = new InetAddress();
440-
peekPort = getImpl().peek(peekAddress);
441-
}
442-
if ((!connectedAddress.equals(peekAddress)) ||
443-
(connectedPort != peekPort)) {
379+
DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
380+
int peekPort = getImpl().peekData(peekPacket);
381+
InetAddress peekAddress = peekPacket.getAddress();
382+
if ((!connectedAddress.equals(peekAddress)) || (connectedPort != peekPort)) {
444383
// throw the packet away and silently continue
445384
tmp = new DatagramPacket(
446385
new byte[1024], 1024);
@@ -578,11 +517,7 @@ public synchronized int getReceiveBufferSize() throws SocketException {
578517
public synchronized void setReuseAddress(boolean on) throws SocketException {
579518
if (isClosed())
580519
throw new SocketException("Socket is closed");
581-
// Integer instead of Boolean for compatibility with older DatagramSocketImpl
582-
if (oldImpl)
583-
getImpl().setOption(SocketOptions.SO_REUSEADDR, on ? -1 : 0);
584-
else
585-
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
520+
getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
586521
}
587522

588523
@Override
@@ -809,9 +744,6 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
809744
if (!(mcastaddr instanceof InetSocketAddress addr))
810745
throw new IllegalArgumentException("Unsupported address type");
811746

812-
if (oldImpl)
813-
throw new UnsupportedOperationException();
814-
815747
checkAddress(addr.getAddress(), "joinGroup");
816748
@SuppressWarnings("removal")
817749
SecurityManager security = System.getSecurityManager();
@@ -835,9 +767,6 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
835767
if (!(mcastaddr instanceof InetSocketAddress addr))
836768
throw new IllegalArgumentException("Unsupported address type");
837769

838-
if (oldImpl)
839-
throw new UnsupportedOperationException();
840-
841770
checkAddress(addr.getAddress(), "leaveGroup");
842771
@SuppressWarnings("removal")
843772
SecurityManager security = System.getSecurityManager();

0 commit comments

Comments
 (0)