高并发IO底层原理浅析(二)

在Java NIO中,Channel是用于进行数据读写的基础组件,它是连接到能够执行I/O操作的实体(如文件、套接字)的对象。Channel类似于传统IO中的InputStream和OutputStream,但与之不同的是,Channel是双向的,可以同时用于读取和写出数据,并且通常配合Buffer对象使用以提高效率。

以下是一个关于Java NIO Channel的几个具体实现类及其用途的详细举例:

1.FileChannel

通过java.nio.channels.FileChannel类,可以高效地读写文件。例如:

java 复制代码
RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
FileChannel channel = raf.getChannel();

// 写入数据到文件
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("Hello, World!".getBytes());
buffer.flip(); // 准备从buffer读取数据
channel.write(buffer);

// 从文件读取数据
buffer.clear();
int bytesRead = channel.read(buffer);
buffer.flip();
while (buffer.hasRemaining()) {
    System.out.print((char) buffer.get());
}

2.SocketChannel:

  • java.nio.channels.SocketChannel用于网络通信,提供TCP/IP协议的客户端和服务端连接。例如创建一个TCP客户端连接:
java 复制代码
SocketChannel clientChannel = SocketChannel.open(new InetSocketAddress(host, port));
clientChannel.configureBlocking(false); // 设置为非阻塞模式

// 缓冲区准备发送数据
ByteBuffer outputBuffer = ByteBuffer.wrap(message.getBytes());

// 发送数据
while (outputBuffer.hasRemaining()) {
    clientChannel.write(outputBuffer);
}

// 接收服务器响应
ByteBuffer inputBuffer = ByteBuffer.allocate(1024);
while (clientChannel.read(inputBuffer) > 0) {
    inputBuffer.flip();
    byte[] data = new byte[inputBuffer.remaining()];
    inputBuffer.get(data);
    String response = new String(data, StandardCharsets.UTF_8);
    System.out.println("Server responded: " + response);
    inputBuffer.clear();
}

3.DatagramChannel:

  • java.nio.channels.DatagramChannel用于UDP通信,处理无连接的数据报包。
java 复制代码
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));

// 接收数据报
ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
SocketAddress sender = channel.receive(receiveBuffer);
receiveBuffer.flip();
byte[] data = new byte[receiveBuffer.remaining()];
receiveBuffer.get(data);
System.out.println("Received from: " + sender + ", Data: " + new String(data));

// 发送数据报
ByteBuffer sendBuffer = ByteBuffer.wrap(message.getBytes());
channel.send(sendBuffer, new InetSocketAddress(remoteHost, remotePort));

4.ServerSocketChannel:

  • java.nio.channels.ServerSocketChannel用于监听新的TCP连接请求,建立SocketChannel来服务每个新连接
java 复制代码
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.configureBlocking(false); // 非阻塞模式

while (true) {
    SocketChannel socketChannel = serverSocketChannel.accept();
    if (socketChannel != null) {
        // 创建线程或使用Selector处理新连接
        handleClientConnection(socketChannel);
    }
}

以上示例展示了如何创建并使用不同类型的Channel进行I/O操作,实际场景中往往还会结合Selector来进行高效的多路复用,从而在一个单线程中处理多个Channel上的事件。

相关推荐
铁蛋AI编程实战15 分钟前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
晚霞的不甘26 分钟前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
SunnyDays101127 分钟前
使用 Java 冻结 Excel 行和列:完整指南
java·冻结excel行和列
JoySSLLian33 分钟前
手把手教你安装免费SSL证书(附宝塔/Nginx/Apache配置教程)
网络·人工智能·网络协议·tcp/ip·nginx·apache·ssl
Zach_yuan38 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
摇滚侠39 分钟前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
云姜.44 分钟前
java多态
java·开发语言·c++
李堇1 小时前
android滚动列表VerticalRollingTextView
android·java
猫头虎1 小时前
如何解决 OpenClaw “Pairing required” 报错:两种官方解决方案详解
网络·windows·网络协议·macos·智能路由器·pip·scipy
泉-java1 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言