J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2001 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 java.nio.channels; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.net.ServerSocket; |
| 30 | import java.net.SocketAddress; |
| 31 | import java.nio.channels.spi.*; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * A selectable channel for stream-oriented listening sockets. |
| 36 | * |
| 37 | * <p> Server-socket channels are not a complete abstraction of listening |
| 38 | * network sockets. Binding and the manipulation of socket options must be |
| 39 | * done through an associated {@link java.net.ServerSocket} object obtained by |
| 40 | * invoking the {@link #socket() socket} method. It is not possible to create |
| 41 | * a channel for an arbitrary, pre-existing server socket, nor is it possible |
| 42 | * to specify the {@link java.net.SocketImpl} object to be used by a server |
| 43 | * socket associated with a server-socket channel. |
| 44 | * |
| 45 | * <p> A server-socket channel is created by invoking the {@link #open() open} |
| 46 | * method of this class. A newly-created server-socket channel is open but not |
| 47 | * yet bound. An attempt to invoke the {@link #accept() accept} method of an |
| 48 | * unbound server-socket channel will cause a {@link NotYetBoundException} to |
| 49 | * be thrown. A server-socket channel can be bound by invoking one of the |
| 50 | * {@link java.net.ServerSocket#bind(java.net.SocketAddress,int) bind} methods |
| 51 | * of an associated server socket. |
| 52 | * |
| 53 | * <p> Server-socket channels are safe for use by multiple concurrent threads. |
| 54 | * </p> |
| 55 | * |
| 56 | * |
| 57 | * @author Mark Reinhold |
| 58 | * @author JSR-51 Expert Group |
| 59 | * @since 1.4 |
| 60 | */ |
| 61 | |
| 62 | public abstract class ServerSocketChannel |
| 63 | extends AbstractSelectableChannel |
| 64 | { |
| 65 | |
| 66 | /** |
| 67 | * Initializes a new instance of this class. |
| 68 | */ |
| 69 | protected ServerSocketChannel(SelectorProvider provider) { |
| 70 | super(provider); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Opens a server-socket channel. |
| 75 | * |
| 76 | * <p> The new channel is created by invoking the {@link |
| 77 | * java.nio.channels.spi.SelectorProvider#openServerSocketChannel |
| 78 | * openServerSocketChannel} method of the system-wide default {@link |
| 79 | * java.nio.channels.spi.SelectorProvider} object. |
| 80 | * |
| 81 | * <p> The new channel's socket is initially unbound; it must be bound to a |
| 82 | * specific address via one of its socket's {@link |
| 83 | * java.net.ServerSocket#bind(SocketAddress) bind} methods before |
| 84 | * connections can be accepted. </p> |
| 85 | * |
| 86 | * @return A new socket channel |
| 87 | * |
| 88 | * @throws IOException |
| 89 | * If an I/O error occurs |
| 90 | */ |
| 91 | public static ServerSocketChannel open() throws IOException { |
| 92 | return SelectorProvider.provider().openServerSocketChannel(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns an operation set identifying this channel's supported |
| 97 | * operations. |
| 98 | * |
| 99 | * <p> Server-socket channels only support the accepting of new |
| 100 | * connections, so this method returns {@link SelectionKey#OP_ACCEPT}. |
| 101 | * </p> |
| 102 | * |
| 103 | * @return The valid-operation set |
| 104 | */ |
| 105 | public final int validOps() { |
| 106 | return SelectionKey.OP_ACCEPT; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | // -- ServerSocket-specific operations -- |
| 111 | |
| 112 | /** |
| 113 | * Retrieves a server socket associated with this channel. |
| 114 | * |
| 115 | * <p> The returned object will not declare any public methods that are not |
| 116 | * declared in the {@link java.net.ServerSocket} class. </p> |
| 117 | * |
| 118 | * @return A server socket associated with this channel |
| 119 | */ |
| 120 | public abstract ServerSocket socket(); |
| 121 | |
| 122 | /** |
| 123 | * Accepts a connection made to this channel's socket. |
| 124 | * |
| 125 | * <p> If this channel is in non-blocking mode then this method will |
| 126 | * immediately return <tt>null</tt> if there are no pending connections. |
| 127 | * Otherwise it will block indefinitely until a new connection is available |
| 128 | * or an I/O error occurs. |
| 129 | * |
| 130 | * <p> The socket channel returned by this method, if any, will be in |
| 131 | * blocking mode regardless of the blocking mode of this channel. |
| 132 | * |
| 133 | * <p> This method performs exactly the same security checks as the {@link |
| 134 | * java.net.ServerSocket#accept accept} method of the {@link |
| 135 | * java.net.ServerSocket} class. That is, if a security manager has been |
| 136 | * installed then for each new connection this method verifies that the |
| 137 | * address and port number of the connection's remote endpoint are |
| 138 | * permitted by the security manager's {@link |
| 139 | * java.lang.SecurityManager#checkAccept checkAccept} method. </p> |
| 140 | * |
| 141 | * @return The socket channel for the new connection, |
| 142 | * or <tt>null</tt> if this channel is in non-blocking mode |
| 143 | * and no connection is available to be accepted |
| 144 | * |
| 145 | * @throws ClosedChannelException |
| 146 | * If this channel is closed |
| 147 | * |
| 148 | * @throws AsynchronousCloseException |
| 149 | * If another thread closes this channel |
| 150 | * while the accept operation is in progress |
| 151 | * |
| 152 | * @throws ClosedByInterruptException |
| 153 | * If another thread interrupts the current thread |
| 154 | * while the accept operation is in progress, thereby |
| 155 | * closing the channel and setting the current thread's |
| 156 | * interrupt status |
| 157 | * |
| 158 | * @throws NotYetBoundException |
| 159 | * If this channel's socket has not yet been bound |
| 160 | * |
| 161 | * @throws SecurityException |
| 162 | * If a security manager has been installed |
| 163 | * and it does not permit access to the remote endpoint |
| 164 | * of the new connection |
| 165 | * |
| 166 | * @throws IOException |
| 167 | * If some other I/O error occurs |
| 168 | */ |
| 169 | public abstract SocketChannel accept() throws IOException; |
| 170 | |
| 171 | } |