--- old/src/java.base/share/classes/java/net/DatagramSocket.java 2020-01-10 14:28:22.000000000 +0000 +++ new/src/java.base/share/classes/java/net/DatagramSocket.java 2020-01-10 14:28:22.000000000 +0000 @@ -1,5 +1,5 @@ /* - * 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 @@ -115,7 +115,6 @@ /** * Various states of this socket. */ - private boolean created = false; private boolean bound = false; private boolean closed = false; private Object closeLock = new Object(); @@ -123,7 +122,7 @@ /* * The implementation of this DatagramSocket. */ - DatagramSocketImpl impl; + private final DatagramSocketImpl impl; /** * Are we using an older DatagramSocketImpl? @@ -282,8 +281,16 @@ * @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); @@ -368,35 +375,32 @@ static Class implClass = null; - void createImpl() throws SocketException { - if (impl == null) { - if (factory != null) { - impl = factory.createDatagramSocketImpl(); - checkOldImpl(); - } else { - boolean isMulticast = (this instanceof MulticastSocket) ? true : false; - impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(isMulticast); - - checkOldImpl(); - } + /** + * 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(); + } else { + 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; } @@ -1321,7 +1325,7 @@ /** * User defined factory for all datagram sockets. */ - static DatagramSocketImplFactory factory; + private static volatile DatagramSocketImplFactory factory; /** * Sets the datagram socket implementation factory for the