author | alanb |
Sun, 15 Feb 2009 12:25:54 +0000 | |
changeset 2057 | 3acf8e5e2ca0 |
parent 1247 | b4c26443dee5 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
2 |
* Copyright 2000-2009 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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.Socket; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
30 |
import java.net.SocketOption; |
2 | 31 |
import java.net.SocketAddress; |
32 |
import java.nio.ByteBuffer; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
33 |
import java.nio.channels.spi.AbstractSelectableChannel; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
34 |
import java.nio.channels.spi.SelectorProvider; |
2 | 35 |
|
36 |
/** |
|
37 |
* A selectable channel for stream-oriented connecting sockets. |
|
38 |
* |
|
39 |
* <p> A socket channel is created by invoking one of the {@link #open open} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
40 |
* methods of this class. It is not possible to create a channel for an arbitrary, |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
41 |
* pre-existing socket. A newly-created socket channel is open but not yet |
2 | 42 |
* connected. An attempt to invoke an I/O operation upon an unconnected |
43 |
* channel will cause a {@link NotYetConnectedException} to be thrown. A |
|
44 |
* socket channel can be connected by invoking its {@link #connect connect} |
|
45 |
* method; once connected, a socket channel remains connected until it is |
|
46 |
* closed. Whether or not a socket channel is connected may be determined by |
|
47 |
* invoking its {@link #isConnected isConnected} method. |
|
48 |
* |
|
49 |
* <p> Socket channels support <i>non-blocking connection:</i> A socket |
|
50 |
* channel may be created and the process of establishing the link to the |
|
51 |
* remote socket may be initiated via the {@link #connect connect} method for |
|
52 |
* later completion by the {@link #finishConnect finishConnect} method. |
|
53 |
* Whether or not a connection operation is in progress may be determined by |
|
54 |
* invoking the {@link #isConnectionPending isConnectionPending} method. |
|
55 |
* |
|
56 |
* <p> Socket channels support <i>asynchronous shutdown,</i> which is similar |
|
57 |
* to the asynchronous close operation specified in the {@link Channel} class. |
|
58 |
* If the input side of a socket is shut down by one thread while another |
|
59 |
* thread is blocked in a read operation on the socket's channel, then the read |
|
60 |
* operation in the blocked thread will complete without reading any bytes and |
|
61 |
* will return <tt>-1</tt>. If the output side of a socket is shut down by one |
|
62 |
* thread while another thread is blocked in a write operation on the socket's |
|
63 |
* channel, then the blocked thread will receive an {@link |
|
64 |
* AsynchronousCloseException}. |
|
65 |
* |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
66 |
* <p> Socket options are configured using the {@link #setOption(SocketOption,Object) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
67 |
* setOption} method. Socket channels support the following options: |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
68 |
* <blockquote> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
69 |
* <table border> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
70 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
71 |
* <th>Option Name</th> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
72 |
* <th>Description</th> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
73 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
74 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
75 |
* <td> {@link java.net.StandardSocketOption#SO_SNDBUF SO_SNDBUF} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
76 |
* <td> The size of the socket send buffer </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
77 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
78 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
79 |
* <td> {@link java.net.StandardSocketOption#SO_RCVBUF SO_RCVBUF} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
80 |
* <td> The size of the socket receive buffer </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
81 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
82 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
83 |
* <td> {@link java.net.StandardSocketOption#SO_KEEPALIVE SO_KEEPALIVE} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
84 |
* <td> Keep connection alive </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
85 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
86 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
87 |
* <td> {@link java.net.StandardSocketOption#SO_REUSEADDR SO_REUSEADDR} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
88 |
* <td> Re-use address </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
89 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
90 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
91 |
* <td> {@link java.net.StandardSocketOption#SO_LINGER SO_LINGER} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
92 |
* <td> Linger on close if data is present (when configured in blocking mode |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
93 |
* only) </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
94 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
95 |
* <tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
96 |
* <td> {@link java.net.StandardSocketOption#TCP_NODELAY TCP_NODELAY} </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
97 |
* <td> Disable the Nagle algorithm </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
98 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
99 |
* </table> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
100 |
* </blockquote> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
101 |
* Additional (implementation specific) options may also be supported. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
102 |
* |
2 | 103 |
* <p> Socket channels are safe for use by multiple concurrent threads. They |
104 |
* support concurrent reading and writing, though at most one thread may be |
|
105 |
* reading and at most one thread may be writing at any given time. The {@link |
|
106 |
* #connect connect} and {@link #finishConnect finishConnect} methods are |
|
107 |
* mutually synchronized against each other, and an attempt to initiate a read |
|
108 |
* or write operation while an invocation of one of these methods is in |
|
109 |
* progress will block until that invocation is complete. </p> |
|
110 |
* |
|
111 |
* @author Mark Reinhold |
|
112 |
* @author JSR-51 Expert Group |
|
113 |
* @since 1.4 |
|
114 |
*/ |
|
115 |
||
116 |
public abstract class SocketChannel |
|
117 |
extends AbstractSelectableChannel |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
118 |
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel |
2 | 119 |
{ |
120 |
||
121 |
/** |
|
122 |
* Initializes a new instance of this class. |
|
123 |
*/ |
|
124 |
protected SocketChannel(SelectorProvider provider) { |
|
125 |
super(provider); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Opens a socket channel. |
|
130 |
* |
|
131 |
* <p> The new channel is created by invoking the {@link |
|
132 |
* java.nio.channels.spi.SelectorProvider#openSocketChannel |
|
133 |
* openSocketChannel} method of the system-wide default {@link |
|
134 |
* java.nio.channels.spi.SelectorProvider} object. </p> |
|
135 |
* |
|
136 |
* @return A new socket channel |
|
137 |
* |
|
138 |
* @throws IOException |
|
139 |
* If an I/O error occurs |
|
140 |
*/ |
|
141 |
public static SocketChannel open() throws IOException { |
|
142 |
return SelectorProvider.provider().openSocketChannel(); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Opens a socket channel and connects it to a remote address. |
|
147 |
* |
|
148 |
* <p> This convenience method works as if by invoking the {@link #open()} |
|
149 |
* method, invoking the {@link #connect(SocketAddress) connect} method upon |
|
150 |
* the resulting socket channel, passing it <tt>remote</tt>, and then |
|
151 |
* returning that channel. </p> |
|
152 |
* |
|
153 |
* @param remote |
|
154 |
* The remote address to which the new channel is to be connected |
|
155 |
* |
|
156 |
* @throws AsynchronousCloseException |
|
157 |
* If another thread closes this channel |
|
158 |
* while the connect operation is in progress |
|
159 |
* |
|
160 |
* @throws ClosedByInterruptException |
|
161 |
* If another thread interrupts the current thread |
|
162 |
* while the connect operation is in progress, thereby |
|
163 |
* closing the channel and setting the current thread's |
|
164 |
* interrupt status |
|
165 |
* |
|
166 |
* @throws UnresolvedAddressException |
|
167 |
* If the given remote address is not fully resolved |
|
168 |
* |
|
169 |
* @throws UnsupportedAddressTypeException |
|
170 |
* If the type of the given remote address is not supported |
|
171 |
* |
|
172 |
* @throws SecurityException |
|
173 |
* If a security manager has been installed |
|
174 |
* and it does not permit access to the given remote endpoint |
|
175 |
* |
|
176 |
* @throws IOException |
|
177 |
* If some other I/O error occurs |
|
178 |
*/ |
|
179 |
public static SocketChannel open(SocketAddress remote) |
|
180 |
throws IOException |
|
181 |
{ |
|
182 |
SocketChannel sc = open(); |
|
183 |
try { |
|
184 |
sc.connect(remote); |
|
185 |
} finally { |
|
186 |
if (!sc.isConnected()) { |
|
187 |
try { sc.close(); } catch (IOException x) { } |
|
188 |
} |
|
189 |
} |
|
190 |
assert sc.isConnected(); |
|
191 |
return sc; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Returns an operation set identifying this channel's supported |
|
196 |
* operations. |
|
197 |
* |
|
198 |
* <p> Socket channels support connecting, reading, and writing, so this |
|
199 |
* method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT} |
|
200 |
* <tt>|</tt> {@link SelectionKey#OP_READ} <tt>|</tt> {@link |
|
201 |
* SelectionKey#OP_WRITE}<tt>)</tt>. </p> |
|
202 |
* |
|
203 |
* @return The valid-operation set |
|
204 |
*/ |
|
205 |
public final int validOps() { |
|
206 |
return (SelectionKey.OP_READ |
|
207 |
| SelectionKey.OP_WRITE |
|
208 |
| SelectionKey.OP_CONNECT); |
|
209 |
} |
|
210 |
||
211 |
||
212 |
// -- Socket-specific operations -- |
|
213 |
||
214 |
/** |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
* @throws ConnectionPendingException |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
216 |
* If a non-blocking connect operation is already in progress on |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
217 |
* this channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
218 |
* @throws AlreadyBoundException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
219 |
* @throws UnsupportedAddressTypeException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
* @throws ClosedChannelException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
* @throws IOException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
222 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
224 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
225 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
226 |
public abstract SocketChannel bind(SocketAddress local) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
227 |
throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
229 |
/** |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
230 |
* @throws UnsupportedOperationException {@inheritDoc} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
231 |
* @throws IllegalArgumentException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
232 |
* @throws ClosedChannelException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
233 |
* @throws IOException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
234 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
238 |
public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
240 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
* Shutdown the connection for reading without closing the channel. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
* <p> Once shutdown for reading then further reads on the channel will |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
* return {@code -1}, the end-of-stream indication. If the input side of the |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
246 |
* connection is already shutdown then invoking this method has no effect. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
248 |
* @return The channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
249 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
250 |
* @throws NotYetConnectedException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
* If this channel is not yet connected |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
252 |
* @throws ClosedChannelException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
253 |
* If this channel is closed |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
254 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
255 |
* If some other I/O error occurs |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
256 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
257 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
258 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
259 |
public abstract SocketChannel shutdownInput() throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
260 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
261 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
262 |
* Shutdown the connection for writing without closing the channel. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
263 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
264 |
* <p> Once shutdown for writing then further attempts to write to the |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
265 |
* channel will throw {@link ClosedChannelException}. If the output side of |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
266 |
* the connection is already shutdown then invoking this method has no |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
267 |
* effect. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
268 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
269 |
* @return The channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
270 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
271 |
* @throws NotYetConnectedException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
272 |
* If this channel is not yet connected |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
273 |
* @throws ClosedChannelException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
274 |
* If this channel is closed |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
275 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
276 |
* If some other I/O error occurs |
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 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
279 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
280 |
public abstract SocketChannel shutdownOutput() throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
281 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
282 |
/** |
2 | 283 |
* Retrieves a socket associated with this channel. |
284 |
* |
|
285 |
* <p> The returned object will not declare any public methods that are not |
|
286 |
* declared in the {@link java.net.Socket} class. </p> |
|
287 |
* |
|
288 |
* @return A socket associated with this channel |
|
289 |
*/ |
|
290 |
public abstract Socket socket(); |
|
291 |
||
292 |
/** |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
293 |
* Tells whether or not this channel's network socket is connected. |
2 | 294 |
* |
295 |
* @return <tt>true</tt> if, and only if, this channel's network socket |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
296 |
* is {@link #isOpen open} and connected |
2 | 297 |
*/ |
298 |
public abstract boolean isConnected(); |
|
299 |
||
300 |
/** |
|
301 |
* Tells whether or not a connection operation is in progress on this |
|
302 |
* channel. </p> |
|
303 |
* |
|
304 |
* @return <tt>true</tt> if, and only if, a connection operation has been |
|
305 |
* initiated on this channel but not yet completed by invoking the |
|
306 |
* {@link #finishConnect finishConnect} method |
|
307 |
*/ |
|
308 |
public abstract boolean isConnectionPending(); |
|
309 |
||
310 |
/** |
|
311 |
* Connects this channel's socket. |
|
312 |
* |
|
313 |
* <p> If this channel is in non-blocking mode then an invocation of this |
|
314 |
* method initiates a non-blocking connection operation. If the connection |
|
315 |
* is established immediately, as can happen with a local connection, then |
|
316 |
* this method returns <tt>true</tt>. Otherwise this method returns |
|
317 |
* <tt>false</tt> and the connection operation must later be completed by |
|
318 |
* invoking the {@link #finishConnect finishConnect} method. |
|
319 |
* |
|
320 |
* <p> If this channel is in blocking mode then an invocation of this |
|
321 |
* method will block until the connection is established or an I/O error |
|
322 |
* occurs. |
|
323 |
* |
|
324 |
* <p> This method performs exactly the same security checks as the {@link |
|
325 |
* java.net.Socket} class. That is, if a security manager has been |
|
326 |
* installed then this method verifies that its {@link |
|
327 |
* java.lang.SecurityManager#checkConnect checkConnect} method permits |
|
328 |
* connecting to the address and port number of the given remote endpoint. |
|
329 |
* |
|
330 |
* <p> This method may be invoked at any time. If a read or write |
|
331 |
* operation upon this channel is invoked while an invocation of this |
|
332 |
* method is in progress then that operation will first block until this |
|
333 |
* invocation is complete. If a connection attempt is initiated but fails, |
|
334 |
* that is, if an invocation of this method throws a checked exception, |
|
335 |
* then the channel will be closed. </p> |
|
336 |
* |
|
337 |
* @param remote |
|
338 |
* The remote address to which this channel is to be connected |
|
339 |
* |
|
340 |
* @return <tt>true</tt> if a connection was established, |
|
341 |
* <tt>false</tt> if this channel is in non-blocking mode |
|
342 |
* and the connection operation is in progress |
|
343 |
* |
|
344 |
* @throws AlreadyConnectedException |
|
345 |
* If this channel is already connected |
|
346 |
* |
|
347 |
* @throws ConnectionPendingException |
|
348 |
* If a non-blocking connection operation is already in progress |
|
349 |
* on this channel |
|
350 |
* |
|
351 |
* @throws ClosedChannelException |
|
352 |
* If this channel is closed |
|
353 |
* |
|
354 |
* @throws AsynchronousCloseException |
|
355 |
* If another thread closes this channel |
|
356 |
* while the connect operation is in progress |
|
357 |
* |
|
358 |
* @throws ClosedByInterruptException |
|
359 |
* If another thread interrupts the current thread |
|
360 |
* while the connect operation is in progress, thereby |
|
361 |
* closing the channel and setting the current thread's |
|
362 |
* interrupt status |
|
363 |
* |
|
364 |
* @throws UnresolvedAddressException |
|
365 |
* If the given remote address is not fully resolved |
|
366 |
* |
|
367 |
* @throws UnsupportedAddressTypeException |
|
368 |
* If the type of the given remote address is not supported |
|
369 |
* |
|
370 |
* @throws SecurityException |
|
371 |
* If a security manager has been installed |
|
372 |
* and it does not permit access to the given remote endpoint |
|
373 |
* |
|
374 |
* @throws IOException |
|
375 |
* If some other I/O error occurs |
|
376 |
*/ |
|
377 |
public abstract boolean connect(SocketAddress remote) throws IOException; |
|
378 |
||
379 |
/** |
|
380 |
* Finishes the process of connecting a socket channel. |
|
381 |
* |
|
382 |
* <p> A non-blocking connection operation is initiated by placing a socket |
|
383 |
* channel in non-blocking mode and then invoking its {@link #connect |
|
384 |
* connect} method. Once the connection is established, or the attempt has |
|
385 |
* failed, the socket channel will become connectable and this method may |
|
386 |
* be invoked to complete the connection sequence. If the connection |
|
387 |
* operation failed then invoking this method will cause an appropriate |
|
388 |
* {@link java.io.IOException} to be thrown. |
|
389 |
* |
|
390 |
* <p> If this channel is already connected then this method will not block |
|
391 |
* and will immediately return <tt>true</tt>. If this channel is in |
|
392 |
* non-blocking mode then this method will return <tt>false</tt> if the |
|
393 |
* connection process is not yet complete. If this channel is in blocking |
|
394 |
* mode then this method will block until the connection either completes |
|
395 |
* or fails, and will always either return <tt>true</tt> or throw a checked |
|
396 |
* exception describing the failure. |
|
397 |
* |
|
398 |
* <p> This method may be invoked at any time. If a read or write |
|
399 |
* operation upon this channel is invoked while an invocation of this |
|
400 |
* method is in progress then that operation will first block until this |
|
401 |
* invocation is complete. If a connection attempt fails, that is, if an |
|
402 |
* invocation of this method throws a checked exception, then the channel |
|
403 |
* will be closed. </p> |
|
404 |
* |
|
405 |
* @return <tt>true</tt> if, and only if, this channel's socket is now |
|
406 |
* connected |
|
407 |
* |
|
408 |
* @throws NoConnectionPendingException |
|
409 |
* If this channel is not connected and a connection operation |
|
410 |
* has not been initiated |
|
411 |
* |
|
412 |
* @throws ClosedChannelException |
|
413 |
* If this channel is closed |
|
414 |
* |
|
415 |
* @throws AsynchronousCloseException |
|
416 |
* If another thread closes this channel |
|
417 |
* while the connect operation is in progress |
|
418 |
* |
|
419 |
* @throws ClosedByInterruptException |
|
420 |
* If another thread interrupts the current thread |
|
421 |
* while the connect operation is in progress, thereby |
|
422 |
* closing the channel and setting the current thread's |
|
423 |
* interrupt status |
|
424 |
* |
|
425 |
* @throws IOException |
|
426 |
* If some other I/O error occurs |
|
427 |
*/ |
|
428 |
public abstract boolean finishConnect() throws IOException; |
|
429 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
430 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
431 |
* Returns the remote address to which this channel's socket is connected. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
432 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
433 |
* <p> Where the channel is bound and connected to an Internet Protocol |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
434 |
* socket address then the return value from this method is of type {@link |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
435 |
* java.net.InetSocketAddress}. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
436 |
* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
437 |
* @return The remote address; {@code null} if the channel's socket is not |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
438 |
* connected |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
439 |
* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
440 |
* @throws ClosedChannelException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
441 |
* If the channel is closed |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
442 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
443 |
* If an I/O error occurs |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
444 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
445 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
446 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
447 |
public abstract SocketAddress getRemoteAddress() throws IOException; |
2 | 448 |
|
449 |
// -- ByteChannel operations -- |
|
450 |
||
451 |
/** |
|
452 |
* @throws NotYetConnectedException |
|
453 |
* If this channel is not yet connected |
|
454 |
*/ |
|
455 |
public abstract int read(ByteBuffer dst) throws IOException; |
|
456 |
||
457 |
/** |
|
458 |
* @throws NotYetConnectedException |
|
459 |
* If this channel is not yet connected |
|
460 |
*/ |
|
461 |
public abstract long read(ByteBuffer[] dsts, int offset, int length) |
|
462 |
throws IOException; |
|
463 |
||
464 |
/** |
|
465 |
* @throws NotYetConnectedException |
|
466 |
* If this channel is not yet connected |
|
467 |
*/ |
|
468 |
public final long read(ByteBuffer[] dsts) throws IOException { |
|
469 |
return read(dsts, 0, dsts.length); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* @throws NotYetConnectedException |
|
474 |
* If this channel is not yet connected |
|
475 |
*/ |
|
476 |
public abstract int write(ByteBuffer src) throws IOException; |
|
477 |
||
478 |
/** |
|
479 |
* @throws NotYetConnectedException |
|
480 |
* If this channel is not yet connected |
|
481 |
*/ |
|
482 |
public abstract long write(ByteBuffer[] srcs, int offset, int length) |
|
483 |
throws IOException; |
|
484 |
||
485 |
/** |
|
486 |
* @throws NotYetConnectedException |
|
487 |
* If this channel is not yet connected |
|
488 |
*/ |
|
489 |
public final long write(ByteBuffer[] srcs) throws IOException { |
|
490 |
return write(srcs, 0, srcs.length); |
|
491 |
} |
|
492 |
||
493 |
} |