< prev index next >

src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java

Print this page

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.nio.ch;
 27 
 28 import java.io.IOException;

 29 import java.io.UncheckedIOException;
 30 import java.lang.invoke.MethodHandle;
 31 import java.lang.invoke.MethodHandles;
 32 import java.lang.invoke.MethodHandles.Lookup;
 33 import java.lang.invoke.MethodType;
 34 import java.lang.invoke.VarHandle;
 35 import java.net.DatagramPacket;
 36 import java.net.DatagramSocket;
 37 import java.net.InetAddress;
 38 import java.net.InetSocketAddress;
 39 import java.net.NetworkInterface;
 40 import java.net.MulticastSocket;
 41 import java.net.SocketAddress;
 42 import java.net.SocketException;
 43 import java.net.SocketOption;

 44 import java.net.StandardSocketOptions;
 45 import java.nio.ByteBuffer;
 46 import java.nio.channels.AlreadyConnectedException;
 47 import java.nio.channels.ClosedChannelException;
 48 import java.nio.channels.DatagramChannel;
 49 import java.nio.channels.MembershipKey;
 50 import java.security.AccessController;
 51 import java.security.PrivilegedAction;
 52 import java.security.PrivilegedExceptionAction;
 53 import java.util.Objects;
 54 import java.util.Set;
 55 import java.util.concurrent.locks.ReentrantLock;
 56 
 57 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 58 
 59 /**
 60  * A multicast datagram socket based on a datagram channel.
 61  *
 62  * This class overrides every public method defined by java.net.DatagramSocket
 63  * and java.net.MulticastSocket. The methods in this class are defined in exactly

231     }
232 
233     @Override
234     public void receive(DatagramPacket p) throws IOException {
235         // get temporary direct buffer with a capacity of p.bufLength
236         int bufLength = DatagramPackets.getBufLength(p);
237         ByteBuffer bb = Util.getTemporaryDirectBuffer(bufLength);
238         try {
239             long nanos = MILLISECONDS.toNanos(timeout);
240             SocketAddress sender = dc.blockingReceive(bb, nanos);
241             bb.flip();
242             synchronized (p) {
243                 // copy bytes to the DatagramPacket and set length
244                 int len = Math.min(bb.limit(), DatagramPackets.getBufLength(p));
245                 bb.get(p.getData(), p.getOffset(), len);
246                 DatagramPackets.setLength(p, len);
247 
248                 // sender address
249                 p.setSocketAddress(sender);
250             }









251         } catch (ClosedChannelException e) {
252             var exc = new SocketException("Socket closed");
253             exc.initCause(e);
254             throw exc;
255         } finally {
256             Util.offerFirstTemporaryDirectBuffer(bb);
257         }
258     }
259 
260     @Override
261     public InetAddress getLocalAddress() {
262         if (isClosed())
263             return null;
264         InetSocketAddress local = dc.localAddress();
265         if (local == null)
266             local = new InetSocketAddress(0);
267         InetAddress result = local.getAddress();
268         @SuppressWarnings("removal")
269         SecurityManager sm = System.getSecurityManager();
270         if (sm != null) {

761             try {
762                 return (NetworkInterface) GET_DEFAULT.invokeExact();
763             } catch (Throwable e) {
764                 throw new InternalError(e);
765             }
766         }
767 
768         /**
769          * Creates a NetworkInterface with the given name index and addresses.
770          */
771         static NetworkInterface newNetworkInterface(String name, int index, InetAddress[] addrs) {
772             try {
773                 return (NetworkInterface) CONSTRUCTOR.invoke(name, index, addrs);
774             } catch (Throwable e) {
775                 throw new InternalError(e);
776             }
777         }
778     }
779 
780     /**
781      * Provides access to the value of the private static DatagramSocket.NO_DELEGATE
782      */
783     private static class DatagramSockets {
784         private static final SocketAddress NO_DELEGATE;
785 
786         static {
787             try {
788                 PrivilegedExceptionAction<Lookup> pa = () ->
789                         MethodHandles.privateLookupIn(DatagramSocket.class, MethodHandles.lookup());
790                 @SuppressWarnings("removal")
791                 MethodHandles.Lookup l = AccessController.doPrivileged(pa);
792                 NO_DELEGATE = (SocketAddress)
793                         l.findStaticVarHandle(DatagramSocket.class, "NO_DELEGATE",
794                                 SocketAddress.class).get();
795             } catch (Exception e) {
796                 throw new ExceptionInInitializerError(e);
797             }
798         }
799     }
800 }

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.nio.ch;
 27 
 28 import java.io.IOException;
 29 import java.io.InterruptedIOException;
 30 import java.io.UncheckedIOException;
 31 import java.lang.invoke.MethodHandle;
 32 import java.lang.invoke.MethodHandles;
 33 import java.lang.invoke.MethodHandles.Lookup;
 34 import java.lang.invoke.MethodType;
 35 import java.lang.invoke.VarHandle;
 36 import java.net.DatagramPacket;
 37 import java.net.DatagramSocket;
 38 import java.net.InetAddress;
 39 import java.net.InetSocketAddress;
 40 import java.net.NetworkInterface;
 41 import java.net.MulticastSocket;
 42 import java.net.SocketAddress;
 43 import java.net.SocketException;
 44 import java.net.SocketOption;
 45 import java.net.SocketTimeoutException;
 46 import java.net.StandardSocketOptions;
 47 import java.nio.ByteBuffer;
 48 import java.nio.channels.AlreadyConnectedException;
 49 import java.nio.channels.ClosedChannelException;
 50 import java.nio.channels.DatagramChannel;
 51 import java.nio.channels.MembershipKey;
 52 import java.security.AccessController;
 53 import java.security.PrivilegedAction;
 54 import java.security.PrivilegedExceptionAction;
 55 import java.util.Objects;
 56 import java.util.Set;
 57 import java.util.concurrent.locks.ReentrantLock;
 58 
 59 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 60 
 61 /**
 62  * A multicast datagram socket based on a datagram channel.
 63  *
 64  * This class overrides every public method defined by java.net.DatagramSocket
 65  * and java.net.MulticastSocket. The methods in this class are defined in exactly

233     }
234 
235     @Override
236     public void receive(DatagramPacket p) throws IOException {
237         // get temporary direct buffer with a capacity of p.bufLength
238         int bufLength = DatagramPackets.getBufLength(p);
239         ByteBuffer bb = Util.getTemporaryDirectBuffer(bufLength);
240         try {
241             long nanos = MILLISECONDS.toNanos(timeout);
242             SocketAddress sender = dc.blockingReceive(bb, nanos);
243             bb.flip();
244             synchronized (p) {
245                 // copy bytes to the DatagramPacket and set length
246                 int len = Math.min(bb.limit(), DatagramPackets.getBufLength(p));
247                 bb.get(p.getData(), p.getOffset(), len);
248                 DatagramPackets.setLength(p, len);
249 
250                 // sender address
251                 p.setSocketAddress(sender);
252             }
253         } catch (SocketTimeoutException e) {
254             throw e;
255         } catch (InterruptedIOException e) {
256             Thread thread = Thread.currentThread();
257             if (thread.isVirtual() && thread.isInterrupted()) {
258                 close();
259                 throw new SocketException("Closed by interrupt");
260             }
261             throw e;
262         } catch (ClosedChannelException e) {
263             var exc = new SocketException("Socket closed");
264             exc.initCause(e);
265             throw exc;
266         } finally {
267             Util.offerFirstTemporaryDirectBuffer(bb);
268         }
269     }
270 
271     @Override
272     public InetAddress getLocalAddress() {
273         if (isClosed())
274             return null;
275         InetSocketAddress local = dc.localAddress();
276         if (local == null)
277             local = new InetSocketAddress(0);
278         InetAddress result = local.getAddress();
279         @SuppressWarnings("removal")
280         SecurityManager sm = System.getSecurityManager();
281         if (sm != null) {

772             try {
773                 return (NetworkInterface) GET_DEFAULT.invokeExact();
774             } catch (Throwable e) {
775                 throw new InternalError(e);
776             }
777         }
778 
779         /**
780          * Creates a NetworkInterface with the given name index and addresses.
781          */
782         static NetworkInterface newNetworkInterface(String name, int index, InetAddress[] addrs) {
783             try {
784                 return (NetworkInterface) CONSTRUCTOR.invoke(name, index, addrs);
785             } catch (Throwable e) {
786                 throw new InternalError(e);
787             }
788         }
789     }
790 
791     /**
792      * Provides access to non-public constants in DatagramSocket.
793      */
794     private static class DatagramSockets {
795         private static final SocketAddress NO_DELEGATE;

796         static {
797             try {
798                 PrivilegedExceptionAction<Lookup> pa = () ->
799                     MethodHandles.privateLookupIn(DatagramSocket.class, MethodHandles.lookup());
800                 @SuppressWarnings("removal")
801                 MethodHandles.Lookup l = AccessController.doPrivileged(pa);
802                 var handle = l.findStaticVarHandle(DatagramSocket.class, "NO_DELEGATE", SocketAddress.class);
803                 NO_DELEGATE = (SocketAddress) handle.get();

804             } catch (Exception e) {
805                 throw new ExceptionInInitializerError(e);
806             }
807         }
808     }
809 }
< prev index next >