Path: blob/master/src/java.base/share/classes/java/nio/channels/NetworkChannel.java
41159 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.net.SocketOption;28import java.net.SocketAddress;29import java.util.Set;30import java.io.IOException;3132/**33* A channel to a network socket.34*35* <p> A channel that implements this interface is a channel to a network36* socket. The {@link #bind(SocketAddress) bind} method is used to bind the37* socket to a local {@link SocketAddress address}, the {@link #getLocalAddress()38* getLocalAddress} method returns the address that the socket is bound to, and39* the {@link #setOption(SocketOption,Object) setOption} and {@link40* #getOption(SocketOption) getOption} methods are used to set and query socket41* options. An implementation of this interface should specify the socket options42* that it supports.43*44* <p> The {@link #bind bind} and {@link #setOption setOption} methods that do45* not otherwise have a value to return are specified to return the network46* channel upon which they are invoked. This allows method invocations to be47* chained. Implementations of this interface should specialize the return type48* so that method invocations on the implementation class can be chained.49*50* @since 1.751*/5253public interface NetworkChannel54extends Channel55{56/**57* Binds the channel's socket to a local address.58*59* <p> This method is used to establish an association between the socket and60* a local address. Once an association is established then the socket remains61* bound until the channel is closed. If the {@code local} parameter has the62* value {@code null} then the socket will be bound to an address that is63* assigned automatically.64*65* @param local66* The address to bind the socket, or {@code null} to bind the socket67* to an automatically assigned socket address68*69* @return This channel70*71* @throws AlreadyBoundException72* If the socket is already bound73* @throws UnsupportedAddressTypeException74* If the type of the given address is not supported75* @throws ClosedChannelException76* If the channel is closed77* @throws IOException78* If some other I/O error occurs79* @throws SecurityException80* If a security manager is installed and it denies an unspecified81* permission. An implementation of this interface should specify82* any required permissions.83*84* @see #getLocalAddress85*/86NetworkChannel bind(SocketAddress local) throws IOException;8788/**89* Returns the socket address that this channel's socket is bound to.90*91* <p> Where the channel is {@link #bind bound} to an Internet Protocol92* socket address then the return value from this method is of type {@link93* java.net.InetSocketAddress}.94*95* @return The socket address that the socket is bound to, or {@code null}96* if the channel's socket is not bound97*98* @throws ClosedChannelException99* If the channel is closed100* @throws IOException101* If an I/O error occurs102*/103SocketAddress getLocalAddress() throws IOException;104105/**106* Sets the value of a socket option.107*108* @param <T>109* The type of the socket option value110* @param name111* The socket option112* @param value113* The value of the socket option. A value of {@code null} may be114* a valid value for some socket options.115*116* @return This channel117*118* @throws UnsupportedOperationException119* If the socket option is not supported by this channel120* @throws IllegalArgumentException121* If the value is not a valid value for this socket option122* @throws ClosedChannelException123* If this channel is closed124* @throws IOException125* If an I/O error occurs126*127* @see java.net.StandardSocketOptions128*/129<T> NetworkChannel setOption(SocketOption<T> name, T value) throws IOException;130131/**132* Returns the value of a socket option.133*134* @param <T>135* The type of the socket option value136* @param name137* The socket option138*139* @return The value of the socket option. A value of {@code null} may be140* a valid value for some socket options.141*142* @throws UnsupportedOperationException143* If the socket option is not supported by this channel144* @throws ClosedChannelException145* If this channel is closed146* @throws IOException147* If an I/O error occurs148*149* @see java.net.StandardSocketOptions150*/151<T> T getOption(SocketOption<T> name) throws IOException;152153/**154* Returns a set of the socket options supported by this channel.155*156* <p> This method will continue to return the set of options even after the157* channel has been closed.158*159* @return A set of the socket options supported by this channel160*/161Set<SocketOption<?>> supportedOptions();162}163164165