java 套接字关联的通道_JavaNIO-通道01 Channel接口结构

Channel接口总体结构

12bcb015de20

image

1 Channel父接口

1.1 AutoCloseable 接口

AutoCloseable接口用来描述自动关闭,此方法主要与try-with-resources语句结合实现自动关闭任何资源关闭,不支持幂等操作,重复调用可能抛出Exception

public interface AutoCloseable {

//AutoCloseable接口用来描述自动关闭,此方法主要与try-with-resources语句结合实现自动关闭任何资源关闭,不支持幂等操作,重复调用可能抛出Exception

void close() throws Exception;

}

1.2 Closeable 接口

Closeable接口用来描述手动关闭,此方法主要用作手动关闭IO流,释放系统资源,支持幂等操作。可能抛出IOException异常

public interface Closeable extends AutoCloseable {

// Closeable接口用来描述手动关闭,此方法主要用作手动关闭IO流,释放系统资源,支持幂等操作。可能抛出IOException异常

public void close() throws IOException;

}

2 Channel接口

Channel接口被称为"通道",用来表示数据到硬件设备、文件、网络套接字的连接通路。

通道可处于打开或关闭这两种状态。一旦通道关闭,任何尝试调用I / O操作将导致抛出ClosedChannelException,通道是否打开可以通过调用其isOpen方法进行测试

public interface Channel extends Closeable {

/** 判断通道是否打开 **/

public boolean isOpen();

/** 关闭通道 **/

public void close() throws IOException;

}

3 Channel子接口

3.1 WritableByteChannel 接口

一个可以用来写入的通道。可写通道上在一个时间节点只能最多进行一个写操作。如果一个线程在通道上执行写操作 ,任何其他线程试图在该通道进行写操作都将被阻止,直到第一个写操作完成。

public interface WritableByteChannel

extends Channel

{

/**

* 从给定缓冲区读取数据写入到此通道中。(同步)

* @param src

* 写入通道字节缓冲区

*

* @return 写入的字节数

*

* @throws NonWritableChannelException

* 如果没有打开此通道进行读取

*

* @throws ClosedChannelException

* 如果该通道关闭

*

* @throws AsynchronousCloseException

* 如果另一个线程关闭此通道读取操作正在进行时

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程写入操作,正在此时关闭通道并设置当前线程的中断状态

*

* @throws IOException

* 如果发生其他I/O错误

*/

public int write(ByteBuffer src) throws IOException;

}

3.2 ReadableByteChannel 接口

一个可以用来读取的通道。通道上在一个时间节点只能最多进行一个读操作。如果一个线程在通道上执行读操作 ,任何其他线程试图在该通道进行读操作都将被阻止,直到第一个读操作完成。

public interface ReadableByteChannel extends Channel {

/**

* 将数据从通道读取写入到给定的缓冲区中。(同步)

*

* @param dst

* 从通道读取数据后,写入字节的缓冲区

*

* @return 读取的字节数

*

* @throws NonReadableChannelException

* 如果没有打开此通道进行读取

*

* @throws ClosedChannelException

* 如果该通道关闭

*

* @throws AsynchronousCloseException

* 如果另一个线程关闭此通道读取操作正在进行时

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程读取操作,正在此时关闭通道并设置当前线程的中断状态

*

* @throws IOException

* 如果发生其他I/O错误

*/

public int read(ByteBuffer dst) throws IOException;

}

3.3 ByteChannel 接口

可以读写字节的通道

/**

* 可以读写字节的通道。这个接口简单地统一了ReadableByteChannel,WritableByteChannel

*/

public interface ByteChannel

extends ReadableByteChannel, WritableByteChannel

{

}

3.4 ScatteringByteChannel 接口

将此通道中的数据列读取,写入到给定缓冲区数组中。

public interface ScatteringByteChannel

extends ReadableByteChannel

{

/**

* 将数据从此通道读取,写入到给定缓冲区数组子序列中。

*

* @param dsts

* 要写入的缓冲区数组

*

* @param offset

* 在缓冲区数组从哪个位置上缓冲区开始写入

*

* @param length

* 从offset开始,写入多少个缓冲区

*

* @return 从通道读取的字节数

*

* @throws IndexOutOfBoundsException

* 如果写入缓冲区字节大于字节数组总字节大小

*

* @throws NonReadableChannelException

* 如果通道未打开就开始读取

*

* @throws ClosedChannelException

* 如果通道关闭

*

* @throws AsynchronousCloseException

* 如果另一个线程关闭此通道读取操作正在进行时

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程读取操作,正在此时关闭通道并设置当前线程的中断状态

*

* @throws IOException

* 如果发生其他I/O错误

*/

public long read(ByteBuffer[] dsts, int offset, int length)

throws IOException;

/**

* 将数据从此通道读取,写入到给定缓冲区数组

*

*

* @param dsts

* 要写入的缓冲区数组

*

* @return 从通道读取的字节数

*

* @throws NonReadableChannelException

* 如果通道未打开就开始读取

*

* @throws ClosedChannelException

* 如果通道关闭

*

* @throws AsynchronousCloseException

* 如果另一个线程关闭此通道读取操作正在进行时

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程读操作,同时通道被关闭

*

* @throws IOException

* 如果发生其他I/O错误

*/

public long read(ByteBuffer[] dsts) throws IOException;

}

3.5 GatheringByteChannel 接口

从给定缓冲区数组向通道写入数据。

public interface GatheringByteChannel

extends WritableByteChannel

{

/**

* 从给定缓冲区数组的子序列向此通道写入数据。

*

* @param srcs

* 写入通道字节缓冲区数组

*

* @param offset

* 在缓冲区数组从哪个位置上缓冲区开始读取

*

* @param length

* 从offset开始,读取多少个缓冲区

*

* @return 通道写入的字节数

*

* @throws IndexOutOfBoundsException

* 如果读取缓冲区字节大于字节数组总字节大小

*

* @throws NonWritableChannelException

* 如果通道未打开就开始写入

*

* @throws ClosedChannelException

* 如果通道关闭

*

* @throws AsynchronousCloseException

* 写入操作正在进行时,另一个线程关闭此通道

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程写操作,同时通道被关闭

*

* @throws IOException

* 如果发生其他I/O错误

*/

public long write(ByteBuffer[] srcs, int offset, int length)

throws IOException;

/**

* 从给定缓冲区数组向此通道写入一个字节序列。

* @param srcs

* 写入通道字节缓冲区数组

*

* @return 通道写入的字节数

*

* @throws NonWritableChannelException

* 如果通道未打开就开始写入

*

* @throws ClosedChannelException

* 如果通道关闭

*

* @throws AsynchronousCloseException

* 写入操作正在进行时,另一个线程关闭此通道

*

* @throws ClosedByInterruptException

* 如果另一个线程中断当前线程写操作,同时通道被关闭

*

* @throws IOException

* 如果发生其他I/O错误

*/

public long write(ByteBuffer[] srcs) throws IOException;

}

3.6 SeekableByteChannel 接口

通道中支持position,可以用来指定通道的读取或写入位置,当对通道进行非指定位置的读取写入时,position会伴随写入读取大小偏移

public interface SeekableByteChannel

extends ByteChannel

{

/**

* 从该通道读取数据写入字节缓冲区,并更新通道中position

*/

@Override

int read(ByteBuffer dst) throws IOException;

/**

* 从给定缓冲区读取数据写入通道,并更新通道中position

*/

@Override

int write(ByteBuffer src) throws IOException;

/**

* 返回此通道的position。

*

* @return 返回此通道的position

*

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生IO异常

*/

long position() throws IOException;

/**

* 设置此通道的position。

*

*

* @param newPosition

* 新position,

*

* @return This channel

*

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IllegalArgumentException

* If the new position is negative

* @throws IOException

* 如果发生IO异常

*/

SeekableByteChannel position(long newPosition) throws IOException;

/**

* 此通道所连接的实体的当前大小

*

* @return 当前大小(以字节为单位)

*

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生IO异常

*/

long size() throws IOException;

/**

* 将此通道连接到的实体截断为给定大小

*

* @param size

* 新的大小,非负字节计数

*

* @return This channel

*

* @throws NonWritableChannelException

* If this channel was not opened for writing

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IllegalArgumentException

* 如果新尺寸为负

* @throws IOException

* 如果发生IO异常

*/

SeekableByteChannel truncate(long size) throws IOException;

}

3.7 AsynchronousChannel 接口

支持异步I/O操作的通道。

public interface AsynchronousChannel

extends Channel

{

/**

* 关闭通道

*

* 如果一个线程在AsynchronousChannel通道上执行操作尚未完成被另一个线程调用close(),执行线程会抛出AsynchronousCloseException异常。

*

* AsynchronousChannel通道在关闭后,对其进行I/O操作,会立即完成,并抛出 ClosedChannelException异常

*

* @throws IOException

* 如果发生I/O错误

*/

@Override

void close() throws IOException;

}

3.8 AsynchronousByteChannel 接口

异步读写数据通道操作。

public interface AsynchronousByteChannel

extends AsynchronousChannel

{

/**

* 将数据从通道读取写入给定的缓冲区中。(异步)

*

* @param

* 操作时附加到I/O操作的对象类型。

* @param dst

* 从通道读取写入的缓冲区

* @param attachment

* 要附加到I/O操作的对象;可以是@code null

* @param handler

* IO完成回调处理程序

*

* @throws IllegalArgumentException

* 如果缓冲区是只读的

* @throws ReadPendingException

* 若在上一个read()方法未完成之前,再次调用read()方法

* @throws ShutdownChannelGroupException

* 如果通过和一个已经终止的AsynchronousChannelGroup关联

*/

void read(ByteBuffer dst,

A attachment,

CompletionHandler handler);

/**

* 将数据从通道读取写入给定的缓冲区中。(异步)

*

* @param dst

* 从通道读取写入的缓冲区

*

* @return 表示操作结果的Future

*

* @throws IllegalArgumentException

* 如果缓冲区是只读的

* @throws ReadPendingException

* 若在上一个read()方法未完成之前,再次调用read()方法

*/

Future read(ByteBuffer dst);

/**

* 从给定缓冲区向此通道写入数据。

* @param

* 操作时附加到I/O操作的对象类型。

* @param src

* 写入通道字节缓冲区

* @param attachment

* 要附加到I/O操作的对象;可以是@code null

* @param handler

* IO完成回调处理程序

*

* @throws WritePendingException

* 若在上一个write()方法未完成之前,再次调用write()方法

* @throws ShutdownChannelGroupException

* 如果通过和一个已经终止的AsynchronousChannelGroup关联

*/

void write(ByteBuffer src,

A attachment,

CompletionHandler handler);

/**

* 从给定缓冲区向此通道写入数据。

*

* @param src

* 写入通道字节缓冲区

*

* @return 表示操作结果的Future

*

* @throws WritePendingException

* 若在上一个write()方法未完成之前,再次调用write()方法

*/

Future write(ByteBuffer src);

}

CompletionHandler

异步回调处理接口

public interface CompletionHandler {

/**

* 异步操作完成时调用。

*

* @param result

* I/O操作的结果.

* @param attachment

* 操作时附加到I/O操作的对象。

*/

void completed(V result, A attachment);

/**

* 异步操作失败时调用。

*

* @param exc

* I/O操作失败原因的异常

* @param attachment

* 操作时附加到I/O操作的对象。

*/

void failed(Throwable exc, A attachment);

}

3.9 InterruptibleChannel 接口

使通道能以异步的方式进行关闭与中断。

如果一个线程在同时实现asynchronously和InterruptibleChannel通道上出现了阻塞状态,那么当其他线程调用这个通道的close()方法时, 这个呈阻塞状态的线程将接收到AsynchronousCloseException 异常。

如果一个线程在同时实现asynchronously和InterruptibleChannel通道上出现了阻塞状态,那么当其他线程调用这个阻塞线程的

interrupt()方法后,通道将被关闭,这个阻塞的线程将接收到ClosedByinterruptException异常

public interface InterruptibleChannel

extends Channel

{

/**

* 关闭此通道

*

*

* @throws IOException 如果发生I/O错误

*/

public void close() throws IOException;

}

3.10 NetworkChannel 接口

public interface NetworkChannel

extends Channel

{

/**

* 将通道的套接字绑定到本地地址。

*

* @param local

* 绑定套接字的地址

*

* @return 当前Channel

*

* @throws AlreadyBoundException

* 如果套接字已绑定

* @throws UnsupportedAddressTypeException

* 如果不支持给定地址的类型

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生其他I/O错误

* @throws SecurityException

* 如果安装了安全管理器并且拒绝未指定的权限

*

* @see #getLocalAddress

*/

NetworkChannel bind(SocketAddress local) throws IOException;

/**

* 返回此通道套接字绑定到的套接字地址

*

* @return 套接字绑定到的套接字地址,或@code null 如果通道的套接字未绑定

*

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生IO异常

*/

SocketAddress getLocalAddress() throws IOException;

/**

* 设置socket选项的值。

*

* @param name

* 选项的名称

* @param value

* 选项的值

*

* @return This channel

*

* @throws UnsupportedOperationException

* 如果此通道不支持socket选项

* @throws IllegalArgumentException

* 如果该值不是此套接字选项的有效值

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生IO异常

*

* @see java.net.StandardSocketOptions

*/

NetworkChannel setOption(SocketOption name, T value) throws IOException;

/**

* 返回socket选项的值。

*

* @param name

* 选项的名称

*

* @return 选项的值,如果没有设置返回null

*

* @throws UnsupportedOperationException

* 如果此通道不支持socket选项

* @throws ClosedChannelException

* 如果通道被关闭

* @throws IOException

* 如果发生IO异常

*

* @see java.net.StandardSocketOptions

*/

T getOption(SocketOption name) throws IOException;

/**

* 返回此通道支持的一组套接字选项

*/

Set> supportedOptions();

}

3.11 MulticastChannel 接口

MulticastChannel 接口的主要作用是使通道支持Internet Protocol ( IP )多播。IP 多播就 是将多个主机地址进行打包,形成一个组( group ),然后将IP 报文向这个组进行发送,也就相当于同时向多个主机传输数据。

public interface MulticastChannel

extends NetworkChannel

{

/**

* 关闭此通道

*

* @throws IOException

* 如果发生I/O错误

*/

@Override void close() throws IOException;

/**

* 加入多播组以开始接收发送到该组的所有数据报,返回MembershipKey

*

* @param group

* 要加入的多播地址

* @param interf

* 要加入组的网络接口

* @return MembershipKey

*

* @throws IllegalArgumentException

* If the group parameter is not a {@link InetAddress#isMulticastAddress

* multicast} address, or the group parameter is an address type

* that is not supported by this channel

* @throws IllegalStateException

* 如果通道已经具有特定于源的界面上的组

* @throws UnsupportedOperationException

* 如果通道的套接字不是Internet协议套接字

* @throws ClosedChannelException

* 如果该通道关闭

* @throws IOException

* 如果发生I/O错误

* @throws SecurityException

* If a security manager is set, and its

* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}

* method denies access to the multiast group

*/

MembershipKey join(InetAddress group, NetworkInterface interf)

throws IOException;

/**

* 加入多播组以开始接收发送到该组的数据报来自给定的源地址。

*

*

* @param group

* 要加入的多播地址

* @param interf

* 要加入组的网络接口

* @param source

* 来源地址

*

* @return MembershipKey

*

* @throws IllegalStateException

* 如果通道当前是给定接口上接收所有数据报的组的成员

* @throws UnsupportedOperationException

* 如果通道的套接字不是Internet协议套接字或不支持源筛选

* @throws ClosedChannelException

* 如果该通道关闭

* @throws IOException

* 如果发生I/O错误

* @throws SecurityException

* If a security manager is set, and its

* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}

* method denies access to the multiast group

*/

MembershipKey join(InetAddress group, NetworkInterface interf, InetAddress source)

throws IOException;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值