深入理解NIO核心组件
IO
IO :Input/output 输入/输出
- 本地磁盘读写是IO
- 网络使用TCP/UDP协议传输,也是IO
BIO 同步阻塞式IO
以网络传输为例:
- 客户端向服务端发送一个Http请求
- 服务端给客户端开启一个线程,并且等待数据读写
- 此时客户端因网络原因或其他因素不进行读写,那么服务端线程将会持续等待。
- 请求数增多后线程数也增多,对服务器内存消耗较大,容易内存溢出。
NIO 同步非阻塞式IO
再以网络传输为例:
- 客户端向服务端发送一个http请求
- 服务端会有一个IO线程对请求连接进行监听
- 当客户端读取或写入数据时,IO线程 将读写操作分配给其他线程执行。
- 当服务端接收多个客户端请求,并且客户端没有进行读写操作。此时IO线程不会分配新线程给客户端,只监听事件。这个技术称为多路复用
JAVA NIO
数据传输流程
- 文件 =》 channel (双向管道,可读可写) =》 buffer 缓冲区 =》应用API
Buffer
在buffer内维护了一个数组,我们需要了解三个属性
- position:当前位置(读写位置)
- capacity:buffer容量
- limit:限制位,读或写数据不能超过该位置
- mark:标记位
Buffer 读写流程
- put / get 方法 从前向后读写,同时移动position指针,直至小于limit ,超过limit即会抛出异常。
- 注意每次读写后 position 都会在最后的位置,此时如果要再次读写,需要执行 flip() 或者 rewind() 方法来设置 position值等于0。
常用方法
flip()
写入buffer后进行读取,调用该方法
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
remaining()
获取当前buffer数据长度
public final int remaining() {
return limit - position;
}
hasRemaining()
判断是否读到了尽头
public final boolean hasRemaining() {
return position < limit;
}
rewind()
重置position和mark 便于重新写入buffer
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
rewind()
channel
channel有三类:
- fileChannel:对文件进行读写操作
- DatagramChannel:UDP协议传输
- ServerSocketChannel:TCP协议传输
// fileChannel
public class FileChannelTest {
String filePath = "F:\\work\\nettyStydyMaven\\test.txt";
@Test
public void test() throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
// FileChannel channel = fileInputStream.getChannel(); // 读管道
// 读写管道 rw
java.nio.channels.FileChannel channel = new RandomAccessFile(filePath, "rw").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 循环读取数据
while ((channel.read(buffer)) != -1) {
channel.read(buffer);
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
int i = 0;
while (buffer.hasRemaining()) {
bytes[i++] = buffer.get();
}
buffer.clear();
System.out.println(new String(bytes));
}
// 写入数据到文件
long position = channel.position();
System.out.println("position: " + position);
channel.write(ByteBuffer.wrap("哈哈哈".getBytes()));
}
}
// UDP协议数据传输
@Test
public void test() throws IOException {
DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress(8090));
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true){
buffer.clear();
channel.receive(buffer);
buffer.flip();
byte[]arr = new byte[buffer.remaining()];
buffer.get(arr);
System.out.println(new String(arr));
buffer.clear();
buffer.put("服务器收到了消息,并且给你回复".getBytes());
buffer.flip();
channel.send(buffer,new InetSocketAddress("127.0.0.1",9090));
}
}
@Test
public void test1() throws IOException {
DatagramChannel channel = DatagramChannel.open();
channel.bind(new InetSocketAddress(9090));
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("你好、、服务器".getBytes());
buffer.flip();
channel.send(buffer,new InetSocketAddress("127.0.0.1",8090));
buffer.clear();
channel.receive(buffer);
buffer.flip();
int i = 0;
byte[]arr = new byte[buffer.remaining()];
while (buffer.hasRemaining()){
arr[i++] = buffer.get();
}
System.out.println("收到服务器消息:" + new String(arr));
}
/**
TCP协议传输,BIO模型,(此时未使用selector)
* 服务端接收消息
*
* @throws IOException
*/
@Test
public void testServer() throws IOException {
ServerSocketChannel open = ServerSocketChannel.open();
open.bind(new InetSocketAddress(6688));
while (true){
handle(open.accept());
}
}
public void handle(final SocketChannel socketChannel) {
new Thread(new Runnable() {
@Override
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true) {
try {
buffer.clear();
socketChannel.read(buffer);
buffer.flip();
int i = 0;
byte[] arr = new byte[buffer.remaining()];
while (buffer.hasRemaining()) {
arr[i++] = buffer.get();
}
System.out.println(new String(arr));
buffer.rewind();
socketChannel.write(buffer);
} catch (Exception e) {
try {
socketChannel.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}).start();
}