src/java.base/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
author alanb
Thu, 08 Feb 2018 10:55:21 +0000
changeset 48761 74c1fa26435a
parent 47216 71c04702a3d5
child 49001 ce06058197a4
permissions -rw-r--r--
8196956: (ch) More channels cleanup Reviewed-by: rriggs, prappo, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    28
import java.io.IOException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    29
import java.net.DatagramPacket;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    30
import java.net.DatagramSocket;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    31
import java.net.DatagramSocketImpl;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    32
import java.net.InetAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    33
import java.net.InetSocketAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    34
import java.net.NetworkInterface;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    35
import java.net.SocketAddress;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    36
import java.net.SocketException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    37
import java.net.SocketOption;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    38
import java.net.SocketTimeoutException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    39
import java.net.StandardSocketOptions;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    40
import java.nio.ByteBuffer;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    41
import java.nio.channels.ClosedChannelException;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    42
import java.nio.channels.DatagramChannel;
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
    43
import java.nio.channels.IllegalBlockingModeException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// Make a datagram-socket channel look like a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// java.net.DatagramSocket so as to simplify tracking future changes to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
// class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class DatagramSocketAdaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    extends DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // The channel being adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final DatagramChannelImpl dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Timeout "option" value for receives
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 25859
diff changeset
    61
    private volatile int timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // ## super will create a useless impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // passing a dummy DatagramSocketImpl object to aovid any native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // resource allocation in super class and invoking our bind method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // before the dc field is initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(dummyDatagramSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.dc = dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static DatagramSocket create(DatagramChannelImpl dc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return new DatagramSocketAdaptor(dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private void connectInternal(SocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        throws SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        InetSocketAddress isa = Net.asInetSocketAddress(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int port = isa.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (port < 0 || port > 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IllegalArgumentException("connect: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new IllegalArgumentException("connect: null address");
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    90
        if (isClosed())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            dc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            Net.translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void bind(SocketAddress local) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (local == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                local = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            dc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Net.translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void connect(InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            connectInternal(new InetSocketAddress(address, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (SocketException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // Yes, j.n.DatagramSocket really does this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void connect(SocketAddress remote) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new IllegalArgumentException("Address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        connectInternal(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            dc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public boolean isBound() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   132
        return dc.localAddress() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean isConnected() {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   136
        return dc.remoteAddress() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return (isConnected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return (isConnected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                ? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void send(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (dc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (!dc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    ByteBuffer bb = ByteBuffer.wrap(p.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                                    p.getOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                                    p.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    if (dc.isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        if (p.getAddress() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            // Legacy DatagramSocket will send in this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            // and set address and port of the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            InetSocketAddress isa = (InetSocketAddress)
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   165
                                                    dc.remoteAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            p.setPort(isa.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            p.setAddress(isa.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            dc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            // Target address may not match connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            dc.send(bb, p.getSocketAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        // Not connected so address must be valid or throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        dc.send(bb, p.getSocketAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // Must hold dc.blockingLock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    //
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   186
    private SocketAddress receive(ByteBuffer bb) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (timeout == 0) {
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   188
            return dc.receive(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        dc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   193
            SocketAddress sender;
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   194
            if ((sender = dc.receive(bb)) != null)
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   195
                return sender;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (!dc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                     throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                long st = System.currentTimeMillis();
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 14342
diff changeset
   201
                int result = dc.poll(Net.POLLIN, to);
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   202
                if (result > 0 && ((result & Net.POLLIN) != 0)) {
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   203
                    if ((sender = dc.receive(bb)) != null)
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   204
                        return sender;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (to <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } finally {
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   211
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                dc.configureBlocking(true);
48761
74c1fa26435a 8196956: (ch) More channels cleanup
alanb
parents: 47216
diff changeset
   213
            } catch (ClosedChannelException e) { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void receive(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        synchronized (dc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (!dc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    ByteBuffer bb = ByteBuffer.wrap(p.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                    p.getOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                    p.getLength());
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   226
                    SocketAddress sender = receive(bb);
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
   227
                    p.setSocketAddress(sender);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    p.setLength(bb.position() - p.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return null;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
        SocketAddress local = dc.localAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
        if (local == null)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   241
            local = new InetSocketAddress(0);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   242
        InetAddress result = ((InetSocketAddress)local).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   243
        SecurityManager sm = System.getSecurityManager();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   244
        if (sm != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   245
            try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   246
                sm.checkConnect(result.getHostAddress(), -1);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   247
            } catch (SecurityException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   248
                return new InetSocketAddress(0).getAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   249
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   251
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   258
            SocketAddress local = dc.getLocalAddress();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   259
            if (local != null) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   260
                return ((InetSocketAddress)local).getPort();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   261
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   264
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        this.timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   275
    private void setBooleanOption(SocketOption<Boolean> name, boolean value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   276
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   277
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   278
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   279
            dc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   280
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   281
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   282
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   283
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   284
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   285
    private void setIntOption(SocketOption<Integer> name, int value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   286
        throws SocketException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   287
    {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   288
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   289
            dc.setOption(name, value);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   290
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   291
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   292
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   293
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   294
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   295
    private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   296
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   297
            return dc.getOption(name).booleanValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   298
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   299
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   300
            return false;       // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   301
        }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   302
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   303
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   304
    private int getIntOption(SocketOption<Integer> name) throws SocketException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   305
        try {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   306
            return dc.getOption(name).intValue();
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   307
        } catch (IOException x) {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   308
            Net.translateToSocketException(x);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   309
            return -1;          // keep compiler happy
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   310
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public void setSendBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   314
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   315
            throw new IllegalArgumentException("Invalid send size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   316
        setIntOption(StandardSocketOptions.SO_SNDBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public int getSendBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   320
        return getIntOption(StandardSocketOptions.SO_SNDBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void setReceiveBufferSize(int size) throws SocketException {
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   324
        if (size <= 0)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   325
            throw new IllegalArgumentException("Invalid receive size");
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   326
        setIntOption(StandardSocketOptions.SO_RCVBUF, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public int getReceiveBufferSize() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   330
        return getIntOption(StandardSocketOptions.SO_RCVBUF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void setReuseAddress(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   334
        setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public boolean getReuseAddress() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   338
        return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   339
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public void setBroadcast(boolean on) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   343
        setBooleanOption(StandardSocketOptions.SO_BROADCAST, on);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public boolean getBroadcast() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   347
        return getBooleanOption(StandardSocketOptions.SO_BROADCAST);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public void setTrafficClass(int tc) throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   351
        setIntOption(StandardSocketOptions.IP_TOS, tc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public int getTrafficClass() throws SocketException {
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   355
        return getIntOption(StandardSocketOptions.IP_TOS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return !dc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public DatagramChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    * A dummy implementation of DatagramSocketImpl that can be passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    * DatagramSocket constructor so that no native resources are allocated in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    * super class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
   private static final DatagramSocketImpl dummyDatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
       = new DatagramSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
       protected void create() throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
       protected void bind(int lport, InetAddress laddr) throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
       protected void send(DatagramPacket p) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
       protected int peek(InetAddress i) throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
       protected int peekData(DatagramPacket p) throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
       protected void receive(DatagramPacket p) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   394
       @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
       protected void setTTL(byte ttl) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   397
       @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
       protected byte getTTL() throws IOException { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
       protected void setTimeToLive(int ttl) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
       protected int getTimeToLive() throws IOException { return 0;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
       protected void join(InetAddress inetaddr) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
       protected void leave(InetAddress inetaddr) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
       protected void joinGroup(SocketAddress mcastaddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                 NetworkInterface netIf) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       protected void leaveGroup(SocketAddress mcastaddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                 NetworkInterface netIf) throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
       protected void close() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
       public Object getOption(int optID) throws SocketException { return null;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
       public void setOption(int optID, Object value) throws SocketException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}