Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/oracle/net/Sdp.java
38918 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.oracle.net;2627import java.net.Socket;28import java.net.ServerSocket;29import java.net.SocketImpl;30import java.net.SocketImplFactory;31import java.net.SocketException;32import java.nio.channels.SocketChannel;33import java.nio.channels.ServerSocketChannel;34import java.io.IOException;35import java.io.FileDescriptor;36import java.security.AccessController;37import java.security.PrivilegedAction;38import java.lang.reflect.Constructor;39import java.lang.reflect.AccessibleObject;40import java.lang.reflect.InvocationTargetException;4142import sun.net.sdp.SdpSupport;4344/**45* This class consists exclusively of static methods that Sockets or Channels to46* sockets that support the InfiniBand Sockets Direct Protocol (SDP).47*/4849public final class Sdp {50private Sdp() { }5152/**53* The package-privage ServerSocket(SocketImpl) constructor54*/55private static final Constructor<ServerSocket> serverSocketCtor;56static {57try {58serverSocketCtor = (Constructor<ServerSocket>)59ServerSocket.class.getDeclaredConstructor(SocketImpl.class);60setAccessible(serverSocketCtor);61} catch (NoSuchMethodException e) {62throw new AssertionError(e);63}64}6566/**67* The package-private SdpSocketImpl() constructor68*/69private static final Constructor<SocketImpl> socketImplCtor;70static {71try {72Class<?> cl = Class.forName("java.net.SdpSocketImpl", true, null);73socketImplCtor = (Constructor<SocketImpl>)cl.getDeclaredConstructor();74setAccessible(socketImplCtor);75} catch (ClassNotFoundException e) {76throw new AssertionError(e);77} catch (NoSuchMethodException e) {78throw new AssertionError(e);79}80}8182private static void setAccessible(final AccessibleObject o) {83AccessController.doPrivileged(new PrivilegedAction<Void>() {84public Void run() {85o.setAccessible(true);86return null;87}88});89}9091/**92* SDP enabled Socket.93*/94private static class SdpSocket extends Socket {95SdpSocket(SocketImpl impl) throws SocketException {96super(impl);97}98}99100/**101* Creates a SDP enabled SocketImpl102*/103private static SocketImpl createSocketImpl() {104try {105return socketImplCtor.newInstance();106} catch (InstantiationException x) {107throw new AssertionError(x);108} catch (IllegalAccessException x) {109throw new AssertionError(x);110} catch (InvocationTargetException x) {111throw new AssertionError(x);112}113}114115/**116* Creates an unconnected and unbound SDP socket. The {@code Socket} is117* associated with a {@link java.net.SocketImpl} of the system-default type.118*119* @return a new Socket120*121* @throws UnsupportedOperationException122* If SDP is not supported123* @throws IOException124* If an I/O error occurs125*/126public static Socket openSocket() throws IOException {127SocketImpl impl = createSocketImpl();128return new SdpSocket(impl);129}130131/**132* Creates an unbound SDP server socket. The {@code ServerSocket} is133* associated with a {@link java.net.SocketImpl} of the system-default type.134*135* @return a new ServerSocket136*137* @throws UnsupportedOperationException138* If SDP is not supported139* @throws IOException140* If an I/O error occurs141*/142public static ServerSocket openServerSocket() throws IOException {143// create ServerSocket via package-private constructor144SocketImpl impl = createSocketImpl();145try {146return serverSocketCtor.newInstance(impl);147} catch (IllegalAccessException x) {148throw new AssertionError(x);149} catch (InstantiationException x) {150throw new AssertionError(x);151} catch (InvocationTargetException x) {152Throwable cause = x.getCause();153if (cause instanceof IOException)154throw (IOException)cause;155if (cause instanceof RuntimeException)156throw (RuntimeException)cause;157throw new RuntimeException(x);158}159}160161/**162* Opens a socket channel to a SDP socket.163*164* <p> The channel will be associated with the system-wide default165* {@link java.nio.channels.spi.SelectorProvider SelectorProvider}.166*167* @return a new SocketChannel168*169* @throws UnsupportedOperationException170* If SDP is not supported or not supported by the default selector171* provider172* @throws IOException173* If an I/O error occurs.174*/175public static SocketChannel openSocketChannel() throws IOException {176FileDescriptor fd = SdpSupport.createSocket();177return sun.nio.ch.Secrets.newSocketChannel(fd);178}179180/**181* Opens a socket channel to a SDP socket.182*183* <p> The channel will be associated with the system-wide default184* {@link java.nio.channels.spi.SelectorProvider SelectorProvider}.185*186* @return a new ServerSocketChannel187*188* @throws UnsupportedOperationException189* If SDP is not supported or not supported by the default selector190* provider191* @throws IOException192* If an I/O error occurs193*/194public static ServerSocketChannel openServerSocketChannel()195throws IOException196{197FileDescriptor fd = SdpSupport.createSocket();198return sun.nio.ch.Secrets.newServerSocketChannel(fd);199}200}201202203