ByteChannel 通过源码发现每一个 file 或 socket 通道都实现ByteChannel。 ByteChannel public interface ByteChannel extends ReadableByteChannel, WritableByteChannel { } ReadableByteChannel 从 FileInputStream 对象的getChannel( )方法获取的 FileChannel 对象是只读的,不过从接口声明的角度来看却是双向的,因为 FileChannel 实现 ByteChannel
FilePacket p = FilePacket.parseByteBuffer(message); Map<String, Object> params; ByteChannel FilePacket.P_FILE_DATA: params = clients.get(conn); fileChannel = (ByteChannel case FilePacket.P_FILE_END: params = clients.get(conn); fileChannel = (ByteChannel java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.ByteChannel runnable = new Runnable() { public void run() { try { ByteChannel
DatagramChannel的定义: public abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel , ScatteringByteChannel, GatheringByteChannel, MulticastChannel ByteChannel表示它同时是ReadableByteChannel也是 public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel,
DatagramChannel的定义: public abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel , ScatteringByteChannel, GatheringByteChannel, MulticastChannel ByteChannel表示它同时是ReadableByteChannel也是 public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel,
ByteChannel 的read()和write()方法是用ByteBuffer作为参数,返回已传输的字节数,比缓冲区的字节数少或者可能为0,因为一次输出不完,缓冲去的位置会与已传输字节相同数量的前移
ByteChannel 接口引申出了 ReadableByteChannel 和WritableByteChannel 两个接口。 ByteChannel 接口本身并不定义新的 API 方法,它是一种用来聚集它自己以一个新名称继承的多个接口的便捷接口。 根据定义,实现 ByteChannel 接口的通道会同时实现 ReadableByteChannel 和 WritableByteChannel 两个接口,所以此类通道是双向的。 从 FileInputStream 对象的getChannel( )方法获取的 FileChannel 对象是只读的,不过从接口声明的角度来看却是双向的,因为FileChannel 实现 ByteChannel ByteChannel 的 read( ) 和 write( )方法使用 ByteBuffer 对象作为参数。两种方法均返回已传输的字节数,可能比缓冲区的字节数少甚至可能为零。
Buffer 看一下Buffer类的申明,实现了BufferedSource, BufferedSink, Cloneable, ByteChannel 四个接口。 public final class Buffer implements BufferedSource, BufferedSink, Cloneable, ByteChannel {...}
interface g extends z, WritableByteChannel { ...... } public final class f implements h, g, Cloneable, ByteChannel
需要提一下的是ByteChannel,它实现了ReadableByteChannel和 WritableByteChannel,也就是说它同时具备读和写的能力,这是有别于流的设计,因为大多数流得实现都只具备输入或输出中的一种能力 在NIO中操纵套接字的SocketChannel也实现了ByteChannel,所以我们可以直接使用它读写套接字。
前面我们已经简单的学习了channel,知道channel作为通道,可以在通道中进行读写操作,同时知道ByteChannel是双向的。
ScatteringByteChannel(interface)├──WritableByteChannel(interface)│└──GatheringByteChannel(interface)├──ByteChannel
它是使用最多的 socket 通道类: public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel
相关方法如下 public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel
其中ByteChannel可以进行读写操作,也即可以进行双向操作。 操作过程:首先创建流对象,有了流对象获取通道,然后准备好写入或者读入通道的bytebuffer信息,使用通道写入或者读入。
AbstractSelectableChannel */ public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel
FileChannel wFileChannel = new FileOutputStream(wFile).getChannel(); // 将Channel中的所有数据映射成ByteChannel
DatagramChannel的定义: public abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel , ScatteringByteChannel, GatheringByteChannel, MulticastChannel ByteChannel表示它同时是ReadableByteChannel也是 public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel,
TLS Channel - Implements a ByteChannel interface over SSLEngine, enabling easy-to-use (socket-like) TLS
ByteChannel接口提供基本的read和write功能。SeekableByteChannel是一个具有在通道中保持位置并更改该位置能力的ByteChannel。
AbstractSelectableChannel */ public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel