< prev index next >
src/java.base/share/classes/java/net/DatagramSocket.java
Print this page
rev 57580 : [mq]: MulticastSocketAdaptor
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -113,19 +113,18 @@
*/
public class DatagramSocket implements java.io.Closeable {
/**
* Various states of this socket.
*/
- private boolean created = false;
private boolean bound = false;
private boolean closed = false;
private Object closeLock = new Object();
/*
* The implementation of this DatagramSocket.
*/
- DatagramSocketImpl impl;
+ private final DatagramSocketImpl impl;
/**
* Are we using an older DatagramSocketImpl?
*/
boolean oldImpl = false;
@@ -280,12 +279,20 @@
*
* @see SecurityManager#checkListen
* @since 1.4
*/
public DatagramSocket(SocketAddress bindaddr) throws SocketException {
+ // Special case initialization for the DatagramChannel socket adaptor.
+ if (this instanceof sun.nio.ch.DatagramSocketAdaptor) {
+ this.impl = null; // no DatagramSocketImpl
+ return;
+ }
+
// create a datagram socket.
- createImpl();
+ boolean multicast = (this instanceof MulticastSocket);
+ this.impl = createImpl(multicast);
+ checkOldImpl();
if (bindaddr != null) {
try {
bind(bindaddr);
} finally {
if (!isBound())
@@ -366,39 +373,36 @@
}
}
static Class<?> implClass = null;
- void createImpl() throws SocketException {
- if (impl == null) {
+ /**
+ * Creates a DatagramSocketImpl.
+ * @param multicast true if the DatagramSocketImpl is for a MulticastSocket
+ */
+ private static DatagramSocketImpl createImpl(boolean multicast) throws SocketException {
+ DatagramSocketImpl impl;
+ DatagramSocketImplFactory factory = DatagramSocket.factory;
if (factory != null) {
impl = factory.createDatagramSocketImpl();
- checkOldImpl();
} else {
- boolean isMulticast = (this instanceof MulticastSocket) ? true : false;
- impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(isMulticast);
-
- checkOldImpl();
- }
+ impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(multicast);
}
// creates a udp socket
impl.create();
- created = true;
+ return impl;
}
/**
- * Get the {@code DatagramSocketImpl} attached to this socket,
- * creating it if necessary.
+ * Return the {@code DatagramSocketImpl} attached to this socket.
*
* @return the {@code DatagramSocketImpl} attached to that
* DatagramSocket
- * @throws SocketException if creation fails.
+ * @throws SocketException never thrown
* @since 1.4
*/
DatagramSocketImpl getImpl() throws SocketException {
- if (!created)
- createImpl();
return impl;
}
/**
* Binds this DatagramSocket to a specific address and port.
@@ -1319,11 +1323,11 @@
}
/**
* User defined factory for all datagram sockets.
*/
- static DatagramSocketImplFactory factory;
+ private static volatile DatagramSocketImplFactory factory;
/**
* Sets the datagram socket implementation factory for the
* application. The factory can be specified only once.
* <p>
< prev index next >