author | michaelm |
Thu, 14 Nov 2019 12:38:31 +0000 | |
branch | unixdomainchannels |
changeset 59078 | 4e648a2d8480 |
parent 58981 | 5c79956cc7d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.nio.channels; |
|
27 |
||
28 |
import java.io.IOException; |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
29 |
import java.net.InetSocketAddress; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
30 |
import java.net.ProtocolFamily; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
31 |
import java.net.StandardProtocolFamily; |
2 | 32 |
import java.net.Socket; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
33 |
import java.net.SocketOption; |
2 | 34 |
import java.net.SocketAddress; |
35 |
import java.nio.ByteBuffer; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
36 |
import java.nio.channels.spi.AbstractSelectableChannel; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
37 |
import java.nio.channels.spi.SelectorProvider; |
2 | 38 |
|
39 |
/** |
|
40 |
* A selectable channel for stream-oriented connecting sockets. |
|
41 |
* |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
42 |
* <p> A socket channel is created by invoking one of the open methods of this class. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
43 |
* It is not possible to create a channel for an arbitrary, |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
44 |
* pre-existing socket. A newly-created socket channel is open but not yet |
2 | 45 |
* connected. An attempt to invoke an I/O operation upon an unconnected |
46 |
* channel will cause a {@link NotYetConnectedException} to be thrown. A |
|
47 |
* socket channel can be connected by invoking its {@link #connect connect} |
|
48 |
* method; once connected, a socket channel remains connected until it is |
|
49 |
* closed. Whether or not a socket channel is connected may be determined by |
|
50 |
* invoking its {@link #isConnected isConnected} method. |
|
51 |
* |
|
52 |
* <p> Socket channels support <i>non-blocking connection:</i> A socket |
|
53 |
* channel may be created and the process of establishing the link to the |
|
54 |
* remote socket may be initiated via the {@link #connect connect} method for |
|
55 |
* later completion by the {@link #finishConnect finishConnect} method. |
|
56 |
* Whether or not a connection operation is in progress may be determined by |
|
57 |
* invoking the {@link #isConnectionPending isConnectionPending} method. |
|
58 |
* |
|
59 |
* <p> Socket channels support <i>asynchronous shutdown,</i> which is similar |
|
60 |
* to the asynchronous close operation specified in the {@link Channel} class. |
|
61 |
* If the input side of a socket is shut down by one thread while another |
|
62 |
* thread is blocked in a read operation on the socket's channel, then the read |
|
63 |
* operation in the blocked thread will complete without reading any bytes and |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
64 |
* will return {@code -1}. If the output side of a socket is shut down by one |
2 | 65 |
* thread while another thread is blocked in a write operation on the socket's |
66 |
* channel, then the blocked thread will receive an {@link |
|
67 |
* AsynchronousCloseException}. |
|
68 |
* |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
69 |
* <p>Two kinds of socket channel are supported: <i>IP</i> (Internet Protocol) and |
58981 | 70 |
* <i>Unix Domain</i> depending on which open method is used to create them and which |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
71 |
* subtype of {@link SocketAddress} that they support for local and remote addresses. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
72 |
* <i>IP</i> channels are created using {@link #open()}. They use {@link |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
73 |
* InetSocketAddress} addresses and support both IPv4 and IPv6 TCP/IP. |
58981 | 74 |
* <i>Unix Domain</i> channels are created using {@link #open(ProtocolFamily)} |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
75 |
* with the family parameter set to {@link StandardProtocolFamily#UNIX}. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
76 |
* They use {@link UnixDomainSocketAddress}es and also |
58981 | 77 |
* do not support the {@link #socket()} method. Note, it is also possible to |
78 |
* create channels that support either IPv4 or IPv6 only through the |
|
79 |
* {@link #open(ProtocolFamily)} method. |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
80 |
* |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
81 |
* <p> Socket options are configured using the {@link #setOption(SocketOption,Object) |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
82 |
* setOption} method. <i>IP</i> socket channels support the following options: |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
83 |
* <blockquote> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
84 |
* <table class="striped"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
85 |
* <caption style="display:none">Socket options</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
86 |
* <thead> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
87 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
88 |
* <th scope="col">Option Name</th> |
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
89 |
* <th scope="col">Description</th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
90 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
91 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
92 |
* <tbody> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
93 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
94 |
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_SNDBUF SO_SNDBUF} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
95 |
* <td> The size of the socket send buffer </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
96 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
97 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
98 |
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
99 |
* <td> The size of the socket receive buffer </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
100 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
101 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
102 |
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
103 |
* <td> Keep connection alive </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
104 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
105 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
106 |
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
107 |
* <td> Re-use address </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
108 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
109 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
110 |
* <th scope="row"> {@link java.net.StandardSocketOptions#SO_LINGER SO_LINGER} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
111 |
* <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
|
112 |
* only) </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
113 |
* </tr> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
114 |
* <tr> |
45881
aaec0fbe17ae
8184208: update class="striped" tables for accessibility
jjg
parents:
45124
diff
changeset
|
115 |
* <th scope="row"> {@link java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY} </th> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
* <td> Disable the Nagle algorithm </td> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
117 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
32143
diff
changeset
|
118 |
* </tbody> |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
119 |
* </table> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
120 |
* </blockquote> |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
121 |
* Additional (implementation specific) options may also be supported. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
122 |
* |
2 | 123 |
* <p> Socket channels are safe for use by multiple concurrent threads. They |
124 |
* support concurrent reading and writing, though at most one thread may be |
|
125 |
* reading and at most one thread may be writing at any given time. The {@link |
|
126 |
* #connect connect} and {@link #finishConnect finishConnect} methods are |
|
127 |
* mutually synchronized against each other, and an attempt to initiate a read |
|
128 |
* or write operation while an invocation of one of these methods is in |
|
129 |
* progress will block until that invocation is complete. </p> |
|
130 |
* |
|
58981 | 131 |
* <p><i>Unix Domain</i> channels support a subset of the options listed above. |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
132 |
* |
2 | 133 |
* @author Mark Reinhold |
134 |
* @author JSR-51 Expert Group |
|
135 |
* @since 1.4 |
|
136 |
*/ |
|
137 |
||
138 |
public abstract class SocketChannel |
|
139 |
extends AbstractSelectableChannel |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
140 |
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel |
2 | 141 |
{ |
142 |
||
143 |
/** |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
144 |
* Initializes a new <i>IP</i> instance of this class. |
18574
4aeaeb541678
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents:
18274
diff
changeset
|
145 |
* |
4aeaeb541678
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents:
18274
diff
changeset
|
146 |
* @param provider |
4aeaeb541678
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents:
18274
diff
changeset
|
147 |
* The provider that created this channel |
2 | 148 |
*/ |
149 |
protected SocketChannel(SelectorProvider provider) { |
|
150 |
super(provider); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
154 |
* Opens an <i>IP</i> socket channel. |
2 | 155 |
* |
156 |
* <p> The new channel is created by invoking the {@link |
|
157 |
* java.nio.channels.spi.SelectorProvider#openSocketChannel |
|
158 |
* openSocketChannel} method of the system-wide default {@link |
|
159 |
* java.nio.channels.spi.SelectorProvider} object. </p> |
|
160 |
* |
|
161 |
* @return A new socket channel |
|
162 |
* |
|
163 |
* @throws IOException |
|
164 |
* If an I/O error occurs |
|
165 |
*/ |
|
166 |
public static SocketChannel open() throws IOException { |
|
167 |
return SelectorProvider.provider().openSocketChannel(); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
171 |
* Returns a {@link SocketChannel} of the given {@link ProtocolFamily}. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
172 |
* |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
173 |
* @param family the protocol family |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
174 |
* |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
175 |
* @return a SocketChannel |
2 | 176 |
* |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
177 |
* @throws IOException if an I/O error occurs |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
178 |
* @throws UnsupportedAddressTypeException if the protocol family is not supported |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
179 |
* @since 14 |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
180 |
*/ |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
181 |
public static SocketChannel open(ProtocolFamily family) throws IOException { |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
182 |
return SelectorProvider.provider().openSocketChannel(family); |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
183 |
} |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
184 |
|
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
185 |
/** |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
186 |
* Opens a socket channel and connects it to a remote address. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
187 |
* Depending on the type of {@link SocketAddress} supplied, the returned object |
58981 | 188 |
* is an <i>IP</i> or <i>Unix Domain</i> channel. |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
189 |
* |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
190 |
* <p> For {@link InetSocketAddress}es this convenience method works as if by invoking the {@link #open()} |
2 | 191 |
* method, invoking the {@link #connect(SocketAddress) connect} method upon |
58981 | 192 |
* the resulting socket channel, passing it {@code remote}, which must be an |
193 |
* {@link InetSocketAddress} and then returning that channel. </p> |
|
2 | 194 |
* |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
195 |
* <p> For {@link UnixDomainSocketAddress}es it works as if by invoking the {@link |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
196 |
* #open(ProtocolFamily)} method with {@link StandardProtocolFamily#UNIX} as parameter, |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
197 |
* invoking the {@link #connect(SocketAddress) connect} method upon |
58981 | 198 |
* the resulting socket channel, passing it {@code remote}, which must be a |
199 |
* {@link UnixDomainSocketAddress} and then returning that channel. </p> |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
200 |
* |
2 | 201 |
* @param remote |
202 |
* The remote address to which the new channel is to be connected |
|
203 |
* |
|
18574
4aeaeb541678
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents:
18274
diff
changeset
|
204 |
* @return A new, and connected, socket channel |
4aeaeb541678
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents:
18274
diff
changeset
|
205 |
* |
2 | 206 |
* @throws AsynchronousCloseException |
207 |
* If another thread closes this channel |
|
208 |
* while the connect operation is in progress |
|
209 |
* |
|
210 |
* @throws ClosedByInterruptException |
|
211 |
* If another thread interrupts the current thread |
|
212 |
* while the connect operation is in progress, thereby |
|
213 |
* closing the channel and setting the current thread's |
|
214 |
* interrupt status |
|
215 |
* |
|
216 |
* @throws UnresolvedAddressException |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
217 |
* If the given remote is an InetSocketAddress that is not fully resolved |
2 | 218 |
* |
219 |
* @throws UnsupportedAddressTypeException |
|
220 |
* If the type of the given remote address is not supported |
|
221 |
* |
|
222 |
* @throws SecurityException |
|
223 |
* If a security manager has been installed |
|
224 |
* and it does not permit access to the given remote endpoint |
|
225 |
* |
|
226 |
* @throws IOException |
|
227 |
* If some other I/O error occurs |
|
228 |
*/ |
|
229 |
public static SocketChannel open(SocketAddress remote) |
|
230 |
throws IOException |
|
231 |
{ |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
232 |
SocketChannel sc; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
233 |
if (remote instanceof InetSocketAddress) |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
234 |
sc = open(); |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
235 |
else if (remote instanceof UnixDomainSocketAddress) |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
236 |
sc = open(StandardProtocolFamily.UNIX); |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
237 |
else |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
238 |
throw new UnsupportedAddressTypeException(); |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
239 |
|
2 | 240 |
try { |
241 |
sc.connect(remote); |
|
8539
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
242 |
} catch (Throwable x) { |
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
243 |
try { |
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
244 |
sc.close(); |
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
245 |
} catch (Throwable suppressed) { |
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
246 |
x.addSuppressed(suppressed); |
2 | 247 |
} |
8539
eeb9fc5a68c1
7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents:
5506
diff
changeset
|
248 |
throw x; |
2 | 249 |
} |
250 |
assert sc.isConnected(); |
|
251 |
return sc; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns an operation set identifying this channel's supported |
|
256 |
* operations. |
|
257 |
* |
|
258 |
* <p> Socket channels support connecting, reading, and writing, so this |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
259 |
* method returns {@code (}{@link SelectionKey#OP_CONNECT} |
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
260 |
* {@code |} {@link SelectionKey#OP_READ} {@code |} {@link |
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
261 |
* SelectionKey#OP_WRITE}{@code )}. |
2 | 262 |
* |
263 |
* @return The valid-operation set |
|
264 |
*/ |
|
265 |
public final int validOps() { |
|
266 |
return (SelectionKey.OP_READ |
|
267 |
| SelectionKey.OP_WRITE |
|
268 |
| SelectionKey.OP_CONNECT); |
|
269 |
} |
|
270 |
||
271 |
||
272 |
// -- Socket-specific operations -- |
|
273 |
||
274 |
/** |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
275 |
* {@inheritDoc} |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
276 |
* |
58981 | 277 |
* <p> Note, for <i>Unix Domain</i> channels, a file is created in the file-system |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
278 |
* with the same name as this channel's bound address. This file persists after |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
279 |
* the channel is closed, and must be removed before another channel can bind |
59078 | 280 |
* to the same name. However, automatically assigned addresses have an empty |
281 |
* pathname, and no corresponding file in the file-system. |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
282 |
* |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
283 |
* @throws ConnectionPendingException |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
284 |
* 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
|
285 |
* this channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
286 |
* @throws AlreadyBoundException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
287 |
* @throws UnsupportedAddressTypeException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
* @throws ClosedChannelException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
289 |
* @throws IOException {@inheritDoc} |
22339 | 290 |
* @throws SecurityException |
291 |
* If a security manager has been installed and its |
|
292 |
* {@link SecurityManager#checkListen checkListen} method denies |
|
58981 | 293 |
* the operation for <i>IP</i> channels; or for <i>Unix Domain</i> |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
294 |
* channels, if the security manager denies "read" or "write" |
58847 | 295 |
* {@link java.io.FilePermission} for the local path. |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
296 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
297 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
298 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
299 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
300 |
public abstract SocketChannel bind(SocketAddress local) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
301 |
throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
302 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
303 |
/** |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
304 |
* @throws UnsupportedOperationException {@inheritDoc} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
305 |
* @throws IllegalArgumentException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
306 |
* @throws ClosedChannelException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
307 |
* @throws IOException {@inheritDoc} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
308 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
309 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
310 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
311 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
312 |
public abstract <T> SocketChannel setOption(SocketOption<T> name, T value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
313 |
throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
314 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
315 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
316 |
* Shutdown the connection for reading without closing the channel. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
317 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
318 |
* <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
|
319 |
* 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
|
320 |
* 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
|
321 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
322 |
* @return The channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
323 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
324 |
* @throws NotYetConnectedException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
325 |
* If this channel is not yet connected |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
326 |
* @throws ClosedChannelException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
327 |
* If this channel is closed |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
328 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
329 |
* If some other I/O error occurs |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
330 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
331 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
332 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
333 |
public abstract SocketChannel shutdownInput() throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
334 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
335 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
336 |
* Shutdown the connection for writing without closing the channel. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
337 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
338 |
* <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
|
339 |
* channel will throw {@link ClosedChannelException}. If the output side of |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
340 |
* 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
|
341 |
* effect. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
342 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
343 |
* @return The channel |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
344 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
345 |
* @throws NotYetConnectedException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
346 |
* If this channel is not yet connected |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
347 |
* @throws ClosedChannelException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
348 |
* If this channel is closed |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
349 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
350 |
* If some other I/O error occurs |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
351 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
352 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
353 |
*/ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
354 |
public abstract SocketChannel shutdownOutput() throws IOException; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
355 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
356 |
/** |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
357 |
* Retrieves a socket associated with this channel if it is an <i>IP</i> |
58981 | 358 |
* channel. The operation is not supported for <i>Unix Domain</i> channels. |
2 | 359 |
* |
360 |
* <p> The returned object will not declare any public methods that are not |
|
361 |
* declared in the {@link java.net.Socket} class. </p> |
|
362 |
* |
|
363 |
* @return A socket associated with this channel |
|
58981 | 364 |
* |
365 |
* @throws UnsupportedOperationException is this is a <i>Unix Domain</i> channel |
|
2 | 366 |
*/ |
367 |
public abstract Socket socket(); |
|
368 |
||
369 |
/** |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
370 |
* Tells whether or not this channel's network socket is connected. |
2 | 371 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
372 |
* @return {@code true} if, and only if, this channel's network socket |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
373 |
* is {@link #isOpen open} and connected |
2 | 374 |
*/ |
375 |
public abstract boolean isConnected(); |
|
376 |
||
377 |
/** |
|
378 |
* Tells whether or not a connection operation is in progress on this |
|
18164 | 379 |
* channel. |
2 | 380 |
* |
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
381 |
* @return {@code true} if, and only if, a connection operation has been |
2 | 382 |
* initiated on this channel but not yet completed by invoking the |
383 |
* {@link #finishConnect finishConnect} method |
|
384 |
*/ |
|
385 |
public abstract boolean isConnectionPending(); |
|
386 |
||
387 |
/** |
|
388 |
* Connects this channel's socket. |
|
389 |
* |
|
390 |
* <p> If this channel is in non-blocking mode then an invocation of this |
|
391 |
* method initiates a non-blocking connection operation. If the connection |
|
392 |
* is established immediately, as can happen with a local connection, then |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
393 |
* this method returns {@code true}. Otherwise this method returns |
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
394 |
* {@code false} and the connection operation must later be completed by |
2 | 395 |
* invoking the {@link #finishConnect finishConnect} method. |
396 |
* |
|
397 |
* <p> If this channel is in blocking mode then an invocation of this |
|
398 |
* method will block until the connection is established or an I/O error |
|
399 |
* occurs. |
|
400 |
* |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
401 |
* <p> For <i>IP</i> channels, this method performs exactly the same security checks |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
402 |
* as the {@link java.net.Socket} class. That is, if a security manager has been |
2 | 403 |
* installed then this method verifies that its {@link |
404 |
* java.lang.SecurityManager#checkConnect checkConnect} method permits |
|
405 |
* connecting to the address and port number of the given remote endpoint. |
|
406 |
* |
|
58981 | 407 |
* <p> For <i>Unix Domain</i> channels, this method performs a security |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
408 |
* manager {@link SecurityManager#checkPermission(Permission)} using |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
409 |
* a {@link java.io.FilePermission} constructed with the path from the |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
410 |
* destination address and "read,write" as the actions. |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
411 |
* |
2 | 412 |
* <p> This method may be invoked at any time. If a read or write |
413 |
* operation upon this channel is invoked while an invocation of this |
|
414 |
* method is in progress then that operation will first block until this |
|
415 |
* invocation is complete. If a connection attempt is initiated but fails, |
|
416 |
* that is, if an invocation of this method throws a checked exception, |
|
417 |
* then the channel will be closed. </p> |
|
418 |
* |
|
419 |
* @param remote |
|
420 |
* The remote address to which this channel is to be connected |
|
421 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
422 |
* @return {@code true} if a connection was established, |
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
423 |
* {@code false} if this channel is in non-blocking mode |
2 | 424 |
* and the connection operation is in progress |
425 |
* |
|
426 |
* @throws AlreadyConnectedException |
|
427 |
* If this channel is already connected |
|
428 |
* |
|
429 |
* @throws ConnectionPendingException |
|
430 |
* If a non-blocking connection operation is already in progress |
|
431 |
* on this channel |
|
432 |
* |
|
433 |
* @throws ClosedChannelException |
|
434 |
* If this channel is closed |
|
435 |
* |
|
436 |
* @throws AsynchronousCloseException |
|
437 |
* If another thread closes this channel |
|
438 |
* while the connect operation is in progress |
|
439 |
* |
|
440 |
* @throws ClosedByInterruptException |
|
441 |
* If another thread interrupts the current thread |
|
442 |
* while the connect operation is in progress, thereby |
|
443 |
* closing the channel and setting the current thread's |
|
444 |
* interrupt status |
|
445 |
* |
|
446 |
* @throws UnresolvedAddressException |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
447 |
* If the given remote address is an InetSocketAddress which is not fully resolved |
2 | 448 |
* |
449 |
* @throws UnsupportedAddressTypeException |
|
450 |
* If the type of the given remote address is not supported |
|
451 |
* |
|
452 |
* @throws SecurityException |
|
453 |
* If a security manager has been installed |
|
454 |
* and it does not permit access to the given remote endpoint |
|
455 |
* |
|
456 |
* @throws IOException |
|
457 |
* If some other I/O error occurs |
|
458 |
*/ |
|
459 |
public abstract boolean connect(SocketAddress remote) throws IOException; |
|
460 |
||
461 |
/** |
|
462 |
* Finishes the process of connecting a socket channel. |
|
463 |
* |
|
464 |
* <p> A non-blocking connection operation is initiated by placing a socket |
|
465 |
* channel in non-blocking mode and then invoking its {@link #connect |
|
466 |
* connect} method. Once the connection is established, or the attempt has |
|
467 |
* failed, the socket channel will become connectable and this method may |
|
468 |
* be invoked to complete the connection sequence. If the connection |
|
469 |
* operation failed then invoking this method will cause an appropriate |
|
470 |
* {@link java.io.IOException} to be thrown. |
|
471 |
* |
|
472 |
* <p> If this channel is already connected then this method will not block |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
473 |
* and will immediately return {@code true}. If this channel is in |
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
474 |
* non-blocking mode then this method will return {@code false} if the |
2 | 475 |
* connection process is not yet complete. If this channel is in blocking |
476 |
* mode then this method will block until the connection either completes |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
477 |
* or fails, and will always either return {@code true} or throw a checked |
2 | 478 |
* exception describing the failure. |
479 |
* |
|
480 |
* <p> This method may be invoked at any time. If a read or write |
|
481 |
* operation upon this channel is invoked while an invocation of this |
|
482 |
* method is in progress then that operation will first block until this |
|
483 |
* invocation is complete. If a connection attempt fails, that is, if an |
|
484 |
* invocation of this method throws a checked exception, then the channel |
|
485 |
* will be closed. </p> |
|
486 |
* |
|
32143
394ab6a6658d
8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents:
25859
diff
changeset
|
487 |
* @return {@code true} if, and only if, this channel's socket is now |
2 | 488 |
* connected |
489 |
* |
|
490 |
* @throws NoConnectionPendingException |
|
491 |
* If this channel is not connected and a connection operation |
|
492 |
* has not been initiated |
|
493 |
* |
|
494 |
* @throws ClosedChannelException |
|
495 |
* If this channel is closed |
|
496 |
* |
|
497 |
* @throws AsynchronousCloseException |
|
498 |
* If another thread closes this channel |
|
499 |
* while the connect operation is in progress |
|
500 |
* |
|
501 |
* @throws ClosedByInterruptException |
|
502 |
* If another thread interrupts the current thread |
|
503 |
* while the connect operation is in progress, thereby |
|
504 |
* closing the channel and setting the current thread's |
|
505 |
* interrupt status |
|
506 |
* |
|
507 |
* @throws IOException |
|
508 |
* If some other I/O error occurs |
|
509 |
*/ |
|
510 |
public abstract boolean finishConnect() throws IOException; |
|
511 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
512 |
/** |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
513 |
* 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
|
514 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
515 |
* <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
|
516 |
* 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
|
517 |
* java.net.InetSocketAddress}. |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
518 |
* |
58981 | 519 |
* <p> Where the channel is bound and connected to a <i>Unix Domain</i> |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
520 |
* address, the returned address is a {@link UnixDomainSocketAddress} |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
521 |
* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
522 |
* @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
|
523 |
* connected |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
524 |
* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
525 |
* @throws ClosedChannelException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
526 |
* If the channel is closed |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
527 |
* @throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
528 |
* If an I/O error occurs |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
529 |
* |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
530 |
* @since 1.7 |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
531 |
*/ |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
532 |
public abstract SocketAddress getRemoteAddress() throws IOException; |
2 | 533 |
|
534 |
// -- ByteChannel operations -- |
|
535 |
||
536 |
/** |
|
537 |
* @throws NotYetConnectedException |
|
538 |
* If this channel is not yet connected |
|
539 |
*/ |
|
540 |
public abstract int read(ByteBuffer dst) throws IOException; |
|
541 |
||
542 |
/** |
|
543 |
* @throws NotYetConnectedException |
|
544 |
* If this channel is not yet connected |
|
545 |
*/ |
|
546 |
public abstract long read(ByteBuffer[] dsts, int offset, int length) |
|
547 |
throws IOException; |
|
548 |
||
549 |
/** |
|
550 |
* @throws NotYetConnectedException |
|
551 |
* If this channel is not yet connected |
|
552 |
*/ |
|
553 |
public final long read(ByteBuffer[] dsts) throws IOException { |
|
554 |
return read(dsts, 0, dsts.length); |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* @throws NotYetConnectedException |
|
559 |
* If this channel is not yet connected |
|
560 |
*/ |
|
561 |
public abstract int write(ByteBuffer src) throws IOException; |
|
562 |
||
563 |
/** |
|
564 |
* @throws NotYetConnectedException |
|
565 |
* If this channel is not yet connected |
|
566 |
*/ |
|
567 |
public abstract long write(ByteBuffer[] srcs, int offset, int length) |
|
568 |
throws IOException; |
|
569 |
||
570 |
/** |
|
571 |
* @throws NotYetConnectedException |
|
572 |
* If this channel is not yet connected |
|
573 |
*/ |
|
574 |
public final long write(ByteBuffer[] srcs) throws IOException { |
|
575 |
return write(srcs, 0, srcs.length); |
|
576 |
} |
|
577 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
578 |
/** |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
579 |
* {@inheritDoc} |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
580 |
* <p> |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
581 |
* If there is a security manager set and this is an <i>IP</i> channel, |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
582 |
* {@code checkConnect} method is |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
583 |
* called with the local address and {@code -1} as its arguments to see |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
584 |
* if the operation is allowed. If the operation is not allowed, |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
585 |
* a {@code SocketAddress} representing the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
586 |
* {@link java.net.InetAddress#getLoopbackAddress loopback} address and the |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
587 |
* local port of the channel's socket is returned. |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
47216
diff
changeset
|
588 |
* <p> |
58981 | 589 |
* If there is a security manager set and this is a <i>Unix Domain</i> channel, |
590 |
* {@code checkPermission} is called with a {@link FilePermission} whose {@code |
|
591 |
* path} is the path of the bound address and actions is {@code "read"} |
|
592 |
* and if the operation is allowed a {@link UnixDomainSocketAddress} |
|
593 |
* corresponding to the bound address is returned. If not, then an address with |
|
594 |
* an empty path is returned. |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
595 |
* |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
596 |
* @return The {@code SocketAddress} that the socket is bound to, or the |
58981 | 597 |
* {@code SocketAddress} representing the loopback address or an |
598 |
* empty pathname, if denied by the security manager, or {@code null} if the |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
599 |
* channel's socket is not bound |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
600 |
* |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
601 |
* @throws ClosedChannelException {@inheritDoc} |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
602 |
* @throws IOException {@inheritDoc} |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
603 |
*/ |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
604 |
@Override |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
9679
diff
changeset
|
605 |
public abstract SocketAddress getLocalAddress() throws IOException; |
2 | 606 |
} |