jdk/src/share/classes/sun/nio/ch/Net.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1152 29d6145d1097
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-2005 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.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
class Net {                                             // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private Net() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // -- Miscellaneous utilities --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static InetSocketAddress checkAddress(SocketAddress sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (sa == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (!(sa instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            throw new UnsupportedAddressTypeException(); // ## needs arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        InetSocketAddress isa = (InetSocketAddress)sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (isa.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            throw new UnresolvedAddressException(); // ## needs arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static InetSocketAddress asInetSocketAddress(SocketAddress sa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (!(sa instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new UnsupportedAddressTypeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return (InetSocketAddress)sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void translateToSocketException(Exception x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        throws SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (x instanceof SocketException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw (SocketException)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Exception nx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (x instanceof ClosedChannelException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            nx = new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        else if (x instanceof AlreadyBoundException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            nx = new SocketException("Already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        else if (x instanceof NotYetBoundException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            nx = new SocketException("Socket is not bound yet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else if (x instanceof UnsupportedAddressTypeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            nx = new SocketException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        else if (x instanceof UnresolvedAddressException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            nx = new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (nx != x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            nx.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (nx instanceof SocketException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw (SocketException)nx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        else if (nx instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw (RuntimeException)nx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new Error("Untranslated exception", nx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static void translateException(Exception x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                   boolean unknownHostForUnresolved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (x instanceof IOException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw (IOException)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // Throw UnknownHostException from here since it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // be thrown as a SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (unknownHostForUnresolved &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            (x instanceof UnresolvedAddressException))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             throw new UnknownHostException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        translateToSocketException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void translateException(Exception x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        translateException(x, false);
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
    // -- Socket operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static FileDescriptor socket(boolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return IOUtil.newFD(socket0(stream, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static FileDescriptor serverSocket(boolean stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return IOUtil.newFD(socket0(stream, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Due to oddities SO_REUSEADDR on windows reuse is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static native int socket0(boolean stream, boolean reuse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static native void bind(FileDescriptor fd, InetAddress addr, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static native int connect(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                              InetAddress remote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                              int remotePort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                              int trafficClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static native int localPort(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static native InetAddress localInetAddress(FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static InetSocketAddress localAddress(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return new InetSocketAddress(localInetAddress(fd),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                         localPort(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new Error(x);         // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static int localPortNumber(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return localPort(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new Error(x);         // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static native int getIntOption0(FileDescriptor fd, int opt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    static int getIntOption(FileDescriptor fd, int opt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return getIntOption0(fd, opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static native void setIntOption0(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                             int opt, int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static void setIntOption(FileDescriptor fd, int opt, int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        setIntOption0(fd, opt, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}