< 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.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodHandles.Lookup;
  31 import java.lang.invoke.VarHandle;
  32 import java.net.DatagramPacket;
  33 import java.net.DatagramSocket;
  34 import java.net.DatagramSocketImpl;
  35 import java.net.InetAddress;
  36 import java.net.InetSocketAddress;
  37 import java.net.NetworkInterface;
  38 import java.net.SocketAddress;
  39 import java.net.SocketException;
  40 import java.net.SocketOption;
  41 import java.net.StandardSocketOptions;
  42 import java.nio.ByteBuffer;
  43 import java.nio.channels.AlreadyConnectedException;
  44 import java.nio.channels.ClosedChannelException;
  45 import java.nio.channels.DatagramChannel;
  46 import java.security.AccessController;
  47 import java.security.PrivilegedAction;
  48 import java.util.Set;


  89     public void bind(SocketAddress local) throws SocketException {
  90         if (local != null) {
  91             local = Net.asInetSocketAddress(local);
  92         } else {
  93             local = new InetSocketAddress(0);
  94         }
  95         try {
  96             dc.bind(local);
  97         } catch (Exception x) {
  98             Net.translateToSocketException(x);
  99         }
 100     }
 101 
 102     @Override
 103     public void connect(InetAddress address, int port) {
 104         if (address == null)
 105             throw new IllegalArgumentException("Address can't be null");
 106         try {
 107             connectInternal(new InetSocketAddress(address, port));
 108         } catch (SocketException x) {
 109             throw new Error(x);
 110         }
 111     }
 112 
 113     @Override
 114     public void connect(SocketAddress remote) throws SocketException {
 115         if (remote == null)
 116             throw new IllegalArgumentException("Address can't be null");
 117         connectInternal(Net.asInetSocketAddress(remote));
 118     }
 119 
 120     @Override
 121     public void disconnect() {
 122         try {
 123             dc.disconnect();
 124         } catch (IOException x) {
 125             throw new Error(x);
 126         }
 127     }
 128 
 129     @Override
 130     public boolean isBound() {
 131         return dc.localAddress() != null;
 132     }
 133 
 134     @Override
 135     public boolean isConnected() {
 136         return dc.remoteAddress() != null;
 137     }
 138 
 139     @Override
 140     public InetAddress getInetAddress() {
 141         InetSocketAddress remote = dc.remoteAddress();
 142         return (remote != null) ? remote.getAddress() : null;
 143     }
 144 
 145     @Override




   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.MethodHandles;
  31 import java.lang.invoke.MethodHandles.Lookup;
  32 import java.lang.invoke.VarHandle;
  33 import java.net.DatagramPacket;
  34 import java.net.DatagramSocket;
  35 import java.net.DatagramSocketImpl;
  36 import java.net.InetAddress;
  37 import java.net.InetSocketAddress;
  38 import java.net.NetworkInterface;
  39 import java.net.SocketAddress;
  40 import java.net.SocketException;
  41 import java.net.SocketOption;
  42 import java.net.StandardSocketOptions;
  43 import java.nio.ByteBuffer;
  44 import java.nio.channels.AlreadyConnectedException;
  45 import java.nio.channels.ClosedChannelException;
  46 import java.nio.channels.DatagramChannel;
  47 import java.security.AccessController;
  48 import java.security.PrivilegedAction;
  49 import java.util.Set;


  90     public void bind(SocketAddress local) throws SocketException {
  91         if (local != null) {
  92             local = Net.asInetSocketAddress(local);
  93         } else {
  94             local = new InetSocketAddress(0);
  95         }
  96         try {
  97             dc.bind(local);
  98         } catch (Exception x) {
  99             Net.translateToSocketException(x);
 100         }
 101     }
 102 
 103     @Override
 104     public void connect(InetAddress address, int port) {
 105         if (address == null)
 106             throw new IllegalArgumentException("Address can't be null");
 107         try {
 108             connectInternal(new InetSocketAddress(address, port));
 109         } catch (SocketException x) {
 110             throw new UncheckedIOException(x);
 111         }
 112     }
 113 
 114     @Override
 115     public void connect(SocketAddress remote) throws SocketException {
 116         if (remote == null)
 117             throw new IllegalArgumentException("Address can't be null");
 118         connectInternal(Net.asInetSocketAddress(remote));
 119     }
 120 
 121     @Override
 122     public void disconnect() {
 123         try {
 124             dc.disconnect();
 125         } catch (IOException x) {
 126             throw new UncheckedIOException(x);
 127         }
 128     }
 129 
 130     @Override
 131     public boolean isBound() {
 132         return dc.localAddress() != null;
 133     }
 134 
 135     @Override
 136     public boolean isConnected() {
 137         return dc.remoteAddress() != null;
 138     }
 139 
 140     @Override
 141     public InetAddress getInetAddress() {
 142         InetSocketAddress remote = dc.remoteAddress();
 143         return (remote != null) ? remote.getAddress() : null;
 144     }
 145 
 146     @Override


< prev index next >