Java NIO中SocketChannel类源码

Java NIO中SocketChannel类源码


/**
 * A selectable channel for stream-oriented connecting sockets.
 * 
 * <p> Socket channels are not a complete abstraction of connecting network
 * sockets.  Binding, shutdown, and the manipulation of socket options must be
 * done through an associated {@link java.net.Socket} object obtained by
 * invoking the {@link #socket() socket} method.  It is not possible to create
 * a channel for an arbitrary, pre-existing socket, nor is it possible to
 * specify the {@link java.net.SocketImpl} object to be used by a socket
 * associated with a socket channel.
 *
 * <p> A socket channel is created by invoking one of the {@link #open open}
 * methods of this class.  A newly-created socket channel is open but not yet
 * connected.  An attempt to invoke an I/O operation upon an unconnected
 * channel will cause a {@link NotYetConnectedException} to be thrown.  A
 * socket channel can be connected by invoking its {@link #connect connect}
 * method; once connected, a socket channel remains connected until it is
 * closed.  Whether or not a socket channel is connected may be determined by
 * invoking its {@link #isConnected isConnected} method.
 *
 * <p> Socket channels support <i>non-blocking connection:</i> A socket
 * channel may be created and the process of establishing the link to the
 * remote socket may be initiated via the {@link #connect connect} method for
 * later completion by the {@link #finishConnect finishConnect} method.
 * Whether or not a connection operation is in progress may be determined by
 * invoking the {@link #isConnectionPending isConnectionPending} method.
 *
 * <p> The input and output sides of a socket channel may independently be
 * <i>shut down</i> without actually closing the channel.  Shutting down the
 * input side of a channel by invoking the {@link java.net.Socket#shutdownInput
 * shutdownInput} method of an associated socket object will cause further
 * reads on the channel to return <tt>-1</tt>, the end-of-stream indication.
 * Shutting down the output side of the channel by invoking the {@link
 * java.net.Socket#shutdownOutput shutdownOutput} method of an associated
 * socket object will cause further writes on the channel to throw a {@link
 * ClosedChannelException}.
 *
 * <p> Socket channels support <i>asynchronous shutdown,</i> which is similar
 * to the asynchronous close operation specified in the {@link Channel} class.
 * If the input side of a socket is shut down by one thread while another
 * thread is blocked in a read operation on the socket's channel, then the read
 * operation in the blocked thread will complete without reading any bytes and
 * will return <tt>-1</tt>.  If the output side of a socket is shut down by one
 * thread while another thread is blocked in a write operation on the socket's
 * channel, then the blocked thread will receive an {@link
 * AsynchronousCloseException}.
 *
 * <p> Socket channels are safe for use by multiple concurrent threads.  They
 * support concurrent reading and writing, though at most one thread may be
 * reading and at most one thread may be writing at any given time.  The {@link
 * #connect connect} and {@link #finishConnect finishConnect} methods are
 * mutually synchronized against each other, and an attempt to initiate a read
 * or write operation while an invocation of one of these methods is in
 * progress will block until that invocation is complete.  </p>
 */

public abstract class SocketChannel
    extends AbstractSelectableChannel
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
{

    protected SocketChannel(SelectorProvider provider) {
	super(provider);
    }

    public static SocketChannel open() throws IOException {
	return SelectorProvider.provider().openSocketChannel();
    }

    public static SocketChannel open(SocketAddress remote)
	throws IOException
    {
	SocketChannel sc = open();
	sc.connect(remote);
	return sc;
    }

    public final int validOps() {
	return (SelectionKey.OP_READ
		| SelectionKey.OP_WRITE
		| SelectionKey.OP_CONNECT);
    }


    // -- Socket-specific operations --

    /**
     * Retrieves a socket associated with this channel.
     */
    public abstract Socket socket();

    public abstract boolean isConnected();

    /**
     * Tells whether or not a connection operation is in progress on this
     * channel.  
     */
    public abstract boolean isConnectionPending();

    /**
     * Connects this channel's socket.
     */
    public abstract boolean connect(SocketAddress remote) throws IOException;

    /**
     * Finishes the process of connecting a socket channel.
     */
    public abstract boolean finishConnect() throws IOException;


    // -- ByteChannel operations --

    /**
     * @throws  NotYetConnectedException
     *          If this channel is not yet connected
     */
    public abstract int read(ByteBuffer dst) throws IOException;

    public abstract long read(ByteBuffer[] dsts, int offset, int length)
	throws IOException;

    public final long read(ByteBuffer[] dsts) throws IOException {
	return read(dsts, 0, dsts.length);
    }

    public abstract int write(ByteBuffer src) throws IOException;


    public abstract long write(ByteBuffer[] srcs, int offset, int length)
	throws IOException;

    public final long write(ByteBuffer[] srcs) throws IOException {
	return write(srcs, 0, srcs.length);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值