J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.nio.ch; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.lang.reflect.*; |
| 30 | import java.net.*; |
| 31 | import java.nio.channels.*; |
| 32 | |
| 33 | |
| 34 | class Net { // package-private |
| 35 | |
| 36 | private Net() { } |
| 37 | |
| 38 | |
| 39 | // -- Miscellaneous utilities -- |
| 40 | |
| 41 | static InetSocketAddress checkAddress(SocketAddress sa) { |
| 42 | if (sa == null) |
| 43 | throw new IllegalArgumentException(); |
| 44 | if (!(sa instanceof InetSocketAddress)) |
| 45 | throw new UnsupportedAddressTypeException(); // ## needs arg |
| 46 | InetSocketAddress isa = (InetSocketAddress)sa; |
| 47 | if (isa.isUnresolved()) |
| 48 | throw new UnresolvedAddressException(); // ## needs arg |
| 49 | return isa; |
| 50 | } |
| 51 | |
| 52 | static InetSocketAddress asInetSocketAddress(SocketAddress sa) { |
| 53 | if (!(sa instanceof InetSocketAddress)) |
| 54 | throw new UnsupportedAddressTypeException(); |
| 55 | return (InetSocketAddress)sa; |
| 56 | } |
| 57 | |
| 58 | static void translateToSocketException(Exception x) |
| 59 | throws SocketException |
| 60 | { |
| 61 | if (x instanceof SocketException) |
| 62 | throw (SocketException)x; |
| 63 | Exception nx = x; |
| 64 | if (x instanceof ClosedChannelException) |
| 65 | nx = new SocketException("Socket is closed"); |
| 66 | else if (x instanceof AlreadyBoundException) |
| 67 | nx = new SocketException("Already bound"); |
| 68 | else if (x instanceof NotYetBoundException) |
| 69 | nx = new SocketException("Socket is not bound yet"); |
| 70 | else if (x instanceof UnsupportedAddressTypeException) |
| 71 | nx = new SocketException("Unsupported address type"); |
| 72 | else if (x instanceof UnresolvedAddressException) { |
| 73 | nx = new SocketException("Unresolved address"); |
| 74 | } |
| 75 | if (nx != x) |
| 76 | nx.initCause(x); |
| 77 | |
| 78 | if (nx instanceof SocketException) |
| 79 | throw (SocketException)nx; |
| 80 | else if (nx instanceof RuntimeException) |
| 81 | throw (RuntimeException)nx; |
| 82 | else |
| 83 | throw new Error("Untranslated exception", nx); |
| 84 | } |
| 85 | |
| 86 | static void translateException(Exception x, |
| 87 | boolean unknownHostForUnresolved) |
| 88 | throws IOException |
| 89 | { |
| 90 | if (x instanceof IOException) |
| 91 | throw (IOException)x; |
| 92 | // Throw UnknownHostException from here since it cannot |
| 93 | // be thrown as a SocketException |
| 94 | if (unknownHostForUnresolved && |
| 95 | (x instanceof UnresolvedAddressException)) |
| 96 | { |
| 97 | throw new UnknownHostException(); |
| 98 | } |
| 99 | translateToSocketException(x); |
| 100 | } |
| 101 | |
| 102 | static void translateException(Exception x) |
| 103 | throws IOException |
| 104 | { |
| 105 | translateException(x, false); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // -- Socket operations -- |
| 110 | |
| 111 | static FileDescriptor socket(boolean stream) { |
| 112 | return IOUtil.newFD(socket0(stream, false)); |
| 113 | } |
| 114 | |
| 115 | static FileDescriptor serverSocket(boolean stream) { |
| 116 | return IOUtil.newFD(socket0(stream, true)); |
| 117 | } |
| 118 | |
| 119 | // Due to oddities SO_REUSEADDR on windows reuse is ignored |
| 120 | private static native int socket0(boolean stream, boolean reuse); |
| 121 | |
| 122 | static native void bind(FileDescriptor fd, InetAddress addr, int port) |
| 123 | throws IOException; |
| 124 | |
| 125 | static native int connect(FileDescriptor fd, |
| 126 | InetAddress remote, |
| 127 | int remotePort, |
| 128 | int trafficClass) |
| 129 | throws IOException; |
| 130 | |
| 131 | |
| 132 | private static native int localPort(FileDescriptor fd) |
| 133 | throws IOException; |
| 134 | |
| 135 | private static native InetAddress localInetAddress(FileDescriptor fd) |
| 136 | throws IOException; |
| 137 | |
| 138 | static InetSocketAddress localAddress(FileDescriptor fd) { |
| 139 | try { |
| 140 | return new InetSocketAddress(localInetAddress(fd), |
| 141 | localPort(fd)); |
| 142 | } catch (IOException x) { |
| 143 | throw new Error(x); // Can't happen |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static int localPortNumber(FileDescriptor fd) { |
| 148 | try { |
| 149 | return localPort(fd); |
| 150 | } catch (IOException x) { |
| 151 | throw new Error(x); // Can't happen |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private static native int getIntOption0(FileDescriptor fd, int opt) |
| 156 | throws IOException; |
| 157 | |
| 158 | static int getIntOption(FileDescriptor fd, int opt) |
| 159 | throws IOException |
| 160 | { |
| 161 | return getIntOption0(fd, opt); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | private static native void setIntOption0(FileDescriptor fd, |
| 166 | int opt, int arg) |
| 167 | throws IOException; |
| 168 | |
| 169 | static void setIntOption(FileDescriptor fd, int opt, int arg) |
| 170 | throws IOException |
| 171 | { |
| 172 | setIntOption0(fd, opt, arg); |
| 173 | } |
| 174 | |
| 175 | private static native void initIDs(); |
| 176 | |
| 177 | static { |
| 178 | Util.load(); |
| 179 | initIDs(); |
| 180 | } |
| 181 | |
| 182 | } |