9 * by Oracle 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 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.
24 */
25
26 package java.nio.channels;
27
28 import java.io.IOException;
29 import java.net.Socket;
30 import java.net.SocketOption;
31 import java.net.SocketAddress;
32 import java.nio.ByteBuffer;
33 import java.nio.channels.spi.AbstractSelectableChannel;
34 import java.nio.channels.spi.SelectorProvider;
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}
40 * methods of this class. It is not possible to create a channel for an arbitrary,
41 * pre-existing socket. A newly-created socket channel is open but not yet
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
134 }
135
136 /**
137 * Opens a socket channel.
138 *
139 * <p> The new channel is created by invoking the {@link
140 * java.nio.channels.spi.SelectorProvider#openSocketChannel
141 * openSocketChannel} method of the system-wide default {@link
142 * java.nio.channels.spi.SelectorProvider} object. </p>
143 *
144 * @return A new socket channel
145 *
146 * @throws IOException
147 * If an I/O error occurs
148 */
149 public static SocketChannel open() throws IOException {
150 return SelectorProvider.provider().openSocketChannel();
151 }
152
153 /**
154 * Opens a socket channel and connects it to a remote address.
155 *
156 * <p> This convenience method works as if by invoking the {@link #open()}
157 * method, invoking the {@link #connect(SocketAddress) connect} method upon
158 * the resulting socket channel, passing it {@code remote}, and then
159 * returning that channel. </p>
160 *
161 * @param remote
162 * The remote address to which the new channel is to be connected
163 *
164 * @return A new, and connected, socket channel
165 *
166 * @throws AsynchronousCloseException
167 * If another thread closes this channel
168 * while the connect operation is in progress
169 *
170 * @throws ClosedByInterruptException
171 * If another thread interrupts the current thread
172 * while the connect operation is in progress, thereby
173 * closing the channel and setting the current thread's
174 * interrupt status
175 *
176 * @throws UnresolvedAddressException
177 * If the given remote address is not fully resolved
178 *
179 * @throws UnsupportedAddressTypeException
180 * If the type of the given remote address is not supported
181 *
182 * @throws SecurityException
183 * If a security manager has been installed
184 * and it does not permit access to the given remote endpoint
185 *
186 * @throws IOException
187 * If some other I/O error occurs
188 */
189 public static SocketChannel open(SocketAddress remote)
190 throws IOException
191 {
192 SocketChannel sc = open();
193 try {
194 sc.connect(remote);
195 } catch (Throwable x) {
196 try {
197 sc.close();
198 } catch (Throwable suppressed) {
199 x.addSuppressed(suppressed);
200 }
201 throw x;
202 }
203 assert sc.isConnected();
204 return sc;
205 }
206
207 /**
208 * Returns an operation set identifying this channel's supported
209 * operations.
210 *
211 * <p> Socket channels support connecting, reading, and writing, so this
212 * method returns {@code (}{@link SelectionKey#OP_CONNECT}
|
9 * by Oracle 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 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.
24 */
25
26 package java.nio.channels;
27
28 import java.io.IOException;
29 import java.net.ProtocolFamily;
30 import java.net.Socket;
31 import java.net.SocketOption;
32 import java.net.SocketAddress;
33 import java.net.StandardProtocolFamily;
34 import java.net.UnixSocketAddress;
35 import java.nio.ByteBuffer;
36 import java.nio.channels.spi.AbstractSelectableChannel;
37 import java.nio.channels.spi.SelectorProvider;
38
39 /**
40 * A selectable channel for stream-oriented connecting sockets.
41 *
42 * <p> A socket channel is created by invoking one of the {@link #open open}
43 * methods of this class. It is not possible to create a channel for an arbitrary,
44 * pre-existing socket. A newly-created socket channel is open but not yet
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
137 }
138
139 /**
140 * Opens a socket channel.
141 *
142 * <p> The new channel is created by invoking the {@link
143 * java.nio.channels.spi.SelectorProvider#openSocketChannel
144 * openSocketChannel} method of the system-wide default {@link
145 * java.nio.channels.spi.SelectorProvider} object. </p>
146 *
147 * @return A new socket channel
148 *
149 * @throws IOException
150 * If an I/O error occurs
151 */
152 public static SocketChannel open() throws IOException {
153 return SelectorProvider.provider().openSocketChannel();
154 }
155
156 /**
157 * Opens a socket channel for the specific address family.
158 *
159 * @see #open()
160 * @param family The protocol family e.g. AF_UNIX
161 * @return A new socket channel
162 * @throws IOException If an I/O error occurs
163 */
164 public static SocketChannel open(ProtocolFamily family) throws IOException {
165 return SelectorProvider.provider().openSocketChannel(family);
166 }
167
168 /**
169 * Opens a socket channel and connects it to a remote address.
170 *
171 * <p> This convenience method works as if by invoking the {@link #open()}
172 * method, invoking the {@link #connect(SocketAddress) connect} method upon
173 * the resulting socket channel, passing it {@code remote}, and then
174 * returning that channel. </p>
175 *
176 * @param remote
177 * The remote address to which the new channel is to be connected
178 *
179 * @return A new, and connected, socket channel
180 *
181 * @throws AsynchronousCloseException
182 * If another thread closes this channel
183 * while the connect operation is in progress
184 *
185 * @throws ClosedByInterruptException
186 * If another thread interrupts the current thread
187 * while the connect operation is in progress, thereby
188 * closing the channel and setting the current thread's
189 * interrupt status
190 *
191 * @throws UnresolvedAddressException
192 * If the given remote address is not fully resolved
193 *
194 * @throws UnsupportedAddressTypeException
195 * If the type of the given remote address is not supported
196 *
197 * @throws SecurityException
198 * If a security manager has been installed
199 * and it does not permit access to the given remote endpoint
200 *
201 * @throws IOException
202 * If some other I/O error occurs
203 */
204 public static SocketChannel open(SocketAddress remote)
205 throws IOException
206 {
207 SocketChannel sc = remote instanceof UnixSocketAddress ? open(StandardProtocolFamily.UNIX) : open();
208 try {
209 sc.connect(remote);
210 } catch (Throwable x) {
211 try {
212 sc.close();
213 } catch (Throwable suppressed) {
214 x.addSuppressed(suppressed);
215 }
216 throw x;
217 }
218 assert sc.isConnected();
219 return sc;
220 }
221
222 /**
223 * Returns an operation set identifying this channel's supported
224 * operations.
225 *
226 * <p> Socket channels support connecting, reading, and writing, so this
227 * method returns {@code (}{@link SelectionKey#OP_CONNECT}
|