jdk/src/share/classes/sun/nio/ch/SocketAdaptor.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// Make a socket channel look like a socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// The only aspects of java.net.Socket-hood that we don't attempt to emulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
// here are the interrupted-I/O exceptions (which our Solaris implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
// attempt to support) and the sending of urgent data.  Otherwise an adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
// socket should look enough like a real java.net.Socket to fool most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
// developers most of the time, right down to the exception message strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
// The methods in this class are defined in exactly the same order as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
// java.net.Socket so as to simplify tracking future changes to that class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class SocketAdaptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    extends Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // The channel being adapted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final SocketChannelImpl sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Option adaptor object, created on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private volatile OptionAdaptor opts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Timeout "option" value for reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private volatile int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Traffic-class/Type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private volatile int trafficClass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // ## super will create a useless impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private SocketAdaptor(SocketChannelImpl sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.sc = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static Socket create(SocketChannelImpl sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new SocketAdaptor(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public SocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // Override this method just to protect against changes in the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void connect(SocketAddress remote) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        connect(remote, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void connect(SocketAddress remote, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (remote == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new IllegalArgumentException("connect: The address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IllegalArgumentException("connect: timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    if (sc.connect(remote))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    sel = Util.getTemporarySelector(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    sk = sc.register(sel, SelectionKey.OP_CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        if (ns > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            sk.isConnectable() && sc.finishConnect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        if (to <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                Net.translateException(x, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void bind(SocketAddress local) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (local == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                local = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            sc.bind(local);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return Net.asInetSocketAddress(sc.remoteAddress()).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (!sc.isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return new InetSocketAddress(0).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return Net.asInetSocketAddress(sc.localAddress()).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return Net.asInetSocketAddress(sc.remoteAddress()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!sc.isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return Net.asInetSocketAddress(sc.localAddress()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private class SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        extends ChannelInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        private SocketInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            super(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        protected int read(ByteBuffer bb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (timeout == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // Implement timeout with a selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                SelectionKey sk = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                Selector sel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    sel = Util.getTemporarySelector(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    sk = sc.register(sel, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    long to = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        long st = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        int ns = sel.select(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        if (ns > 0 && sk.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                            if ((n = sc.read(bb)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        sel.selectedKeys().remove(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        to -= System.currentTimeMillis() - st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        if (to <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            throw new SocketTimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (sk != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        sk.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    if (sel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        Util.releaseTemporarySelector(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private InputStream socketInputStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (!sc.isInputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new SocketException("Socket input is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (socketInputStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                socketInputStream = (InputStream)AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            return new SocketInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return socketInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (!sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (!sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!sc.isOutputOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new SocketException("Socket output is shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            os = (OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                AccessController.doPrivileged(new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    public Object run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        return Channels.newOutputStream(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw (IOException)e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private OptionAdaptor opts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (opts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            opts = new OptionAdaptor(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return opts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public void setTcpNoDelay(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        opts().setTcpNoDelay(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public boolean getTcpNoDelay() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return opts().getTcpNoDelay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public void setSoLinger(boolean on, int linger) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        opts().setSoLinger(on, linger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public int getSoLinger() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return opts().getSoLinger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void sendUrgentData(int data) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        throw new SocketException("Urgent data not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public void setOOBInline(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        opts().setOOBInline(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public boolean getOOBInline() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return opts().getOOBInline();
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 setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new IllegalArgumentException("timeout can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        this.timeout = timeout;
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 getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return timeout;
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 setSendBufferSize(int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        opts().setSendBufferSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return opts().getSendBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void setReceiveBufferSize(int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        opts().setReceiveBufferSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public int getReceiveBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return opts().getReceiveBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void setKeepAlive(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        opts().setKeepAlive(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public boolean getKeepAlive() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return opts().getKeepAlive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        opts().setTrafficClass(tc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        trafficClass = tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int tc = opts().getTrafficClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (tc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            tc = trafficClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        opts().setReuseAddress(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return opts().getReuseAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            Net.translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            sc.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            sc.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            Net.translateException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (sc.isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                ",port=" + getPort() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                ",localport=" + getLocalPort() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return "Socket[unconnected]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return sc.isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return sc.isBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return !sc.isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public boolean isInputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return !sc.isInputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public boolean isOutputShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return !sc.isOutputOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}