Java中的NIO基础知识

上一篇介绍了五种NIO模型,本篇将介绍Java中的NIO类库,为学习netty做好铺垫

Java NIO 由3个核心组成,分别是Channels,Buffers,Selectors。本文主要介绍着三个部分。

Channel

所有的I/O都从一个Channel开始。通道与流不同,通道是双向的,流是单向的。

即可以从通道中读取数据,也可以写数据到通道里 。

读的话,是从通道读取数据到缓冲区,写的话是从缓冲区写入数据到通道。

四种通道:

  • FileChannel.从文件中读写数据
  • DatagramChannel.通过UDP协议,读写网络中的数据
  • SocketChannel,能通过TCP协议来读写网络中数据,常用于客户端
  • ServerSocketChannel。监听TCP连接,对每个新进来的连接会创建一个SocketChannel。

Buffer

Java NIO中的Buffer用于NIO通道进行交互。

缓冲区本质上一块可以写入数据,也可以从中读取数据的内存。也就是堆外内存,也叫直接内存。

当向Buffer写入数据时,Buffer会记录下写了多少数据,一旦要读取数据,需要通过flip()方法将Buffer从写模式切换到度模式。

在读模式下,可以读取之前写入到Buffer的所有数据。

一旦读完了所有数据,就需要情况缓存区,让它可以再次被写入。有两种方式能清空缓冲区,调用clear()或者compact()方法。

clear()方法会清空整个缓冲区。compact()方法只会清除已经读过的数据。任何未读的数据都被移到缓冲区的起始处,新写入的数据将放到缓冲区未读数据的后面。

任何未读的数据将被移到缓冲区的起始处,新写入的数据将放大缓冲区未读数据的后面。

Buffer的capacity,position和limit

capacity

capacity作为一个内存块,buffer有一个固定的大小值,也叫capacity,只能向内存中写入byte,long,char等类型。一旦Buffer满了,需要将其清空。

position

当写数据到Buffer中是,position表示当前的位置。初始的position值为0,当一个byte,long等数据写到buffer后,position会向前移动到下一个可插入数据的单元。positon最大可谓capacity-1.

当读取数据时,也是从特定位置读。将Buffer从写模式切换到读模式,positon会被重置0,当从Buffer的position处读取数据时,position向前移动到想一个可以读的位置。

limit

在写模式下,Buffer的limit表示你最多能往Buffer里写多少数据。写模式下,limit等于buffer的capacity

Buffer的分配

要想获得一个Buffer对象首先要进行分配。 每一个Buffer类都有一个allocate方法。下面是一个分配48字节capacity的ByteBuffer的例子。

ByteBuffer buf = ByteBuffer.allocate(48);

Selector

Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够检测到通道是否为读写事件准备好的的组件。所以Selector可以单个线程处理多个Channel。

为什么使用Selector

Selector能够使用一个线程来处理所有通道。但是对于如今的操作系统和CPU来说,多线程已经较过去效率高了很多。

Selector的创建

1.通过调用Selector.open()方法创建一个Selector

2.将Channel注册到Selector上配合使用,可使用Channel.register方法来实现,如下

            servChannel.configureBlocking(false);
            servChannel.register(selector, SelectionKey.OP_ACCEPT);

与Selector一起使用时,Channel必须处于非阻塞模式下。这意味着FileChannel与Selector不能一起使用,因为FileChannel不能切换到非阻塞模式。

3.通道触发意味着该事件已经就绪。Java中有如下常量对应着通道事件。

  • SelectionKey.OP_CONNECT(连接就绪):Channel成功连接到另一个服务器
  • SelectionKey.OP_ACCEPT(接收就绪):Channel准备好接收进入新的连接
  • SelectionKey.OP_READ(读就绪):Channel有数据可以读
  • SelectionKey.OP_WRITE(写就绪):Chanel有数据可以写

4.SelectionKey

当向Selector注册Channel时,register()方法会返回一个SelectionKey对象。这个对象包含interest集合,ready集合,Channel,Selector,附加的对象(可选)。

interest集合是你所选择的感兴趣的事件集合。可以通过SelectionKey读写interest集合。

ready 集合是通道已经准备就绪的操作的集合。在一次选择(Selection)之后,你会首先访问这个ready set。

用NIO创建的客户端与服务端:

服务端:

package com.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class MutipleexerTimeServer implements Runnable{

    private Selector selector;

    private ServerSocketChannel servChannel;

    private volatile boolean stop = false;

    /**
     * 创建多路复用器,绑定NIO端口
     *
     * @param port
     */
    public MutipleexerTimeServer(int port){
        try{
            selector = Selector.open();
            servChannel = ServerSocketChannel.open();
            servChannel.configureBlocking(false);
            servChannel.register(selector, SelectionKey.OP_ACCEPT);
            servChannel.socket().bind(new InetSocketAddress(port),1024);
            System.out.println("the time server start at port: "+port );
        }catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void stop(){
        this.stop = stop;
    }

    @Override
    public void run() {
        while (!stop){
            try {
                // selector每隔一秒唤醒一次
                selector.select(1000);
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> it = selectionKeys.iterator();
                SelectionKey key = null;
                while (it.hasNext()){
                    key = it.next();
                    it.remove();
                    try {
                        handlerInput(key);
                    }catch (Exception e){
                        if (key !=null){
                            key.cancel();
                            if (key.channel() !=null){
                                key.channel().close();
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        // 多路复用器关闭后,所有注册到上面的channel和pipe等资源都不被自动去注册并关闭,所有不需要重复释放资源
        if (selector!=null){
            try {
                selector.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void handlerInput(SelectionKey key) throws Exception{
        if (key.isValid()){
            // 处理新接入的请求消息
            if (key.isAcceptable()){
                // Accept the new Connection
                ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                // 已完成TCP三次握手
                SocketChannel sc = ssc.accept();
                sc.configureBlocking(false);
                // Add the new connection to the selector
                sc.register(selector,SelectionKey.OP_READ);
            }

            if (key.isReadable()){
                // Read the data
                SocketChannel sc = (SocketChannel) key.channel();
                ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                int readBytes = sc.read(readBuffer);
                if (readBytes>0){
                    readBuffer.flip();
                    byte[] bytes = new byte[readBuffer.remaining()];
                    readBuffer.get(bytes);
                    String body = new String(bytes,"UTF-8");
                    System.out.println("server receive order: "+body);
                    String correntTime = "QUERY".equalsIgnoreCase(body)?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
                    doWrite(sc,correntTime);
                }else if (readBytes<0){
                     // 对端链路关闭
                    key.cancel();
                    sc.close();
                }else {
                    ;
                }
            }
        }
    }

    private void doWrite(SocketChannel channel,String response) throws Exception{
        if (response!=null && response.trim().length()>0){
            byte[] bytes = response.getBytes();
            ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
            writeBuffer.put(bytes);
            writeBuffer.flip();
            channel.write(writeBuffer);
        }
    }
}

package com.nio;
/**
 * 启动类
 */
public class TimeServer {

    public static void main(String args[]){
        int port = 9816;
        MutipleexerTimeServer timeServer = new MutipleexerTimeServer(port);
        new Thread(timeServer,"nit").start();
    }
}

客户端:

package com.nio.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @author tangj
 * @date 2018/6/14 23:13
 */
public class TimeClientHandle implements Runnable{

    private String host;

    private int port;

    private Selector selector;

    private SocketChannel socketChannel;

    private volatile boolean stop;

    public TimeClientHandle(String host,int port){
        this.host = host;
        this.port = port;
        try{
            selector = Selector.open();
            socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
        }catch (Exception e){
            e.printStackTrace();
            System.exit(-1);
        }
    }

    @Override
    public void run() {
        try{
            doConnect();
        }catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        while (!stop){
            try{
                selector.select(1000);
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> it = selectionKeys.iterator();
                SelectionKey key = null;
                while (it.hasNext()){
                    key = it.next();
                    it.remove();
                    try {
                        handlerInput(key);
                    }catch (Exception e){
                        if (key!=null){
                            key.cancel();
                            if (key.channel() != null){
                                key.channel().close();
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                System.exit(1);
            }
        }

        // 多路复用器关闭后,所有注册到上面的channel和pipe等资源都不被自动去注册并关闭,所有不需要重复释放资源
        if (selector!=null){
            try {
                selector.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void handlerInput(SelectionKey key) throws Exception{
        if (key.isValid()){
            // 判断是否连接成功
            SocketChannel sc = (SocketChannel) key.channel();
            if (key.isConnectable()){
                if (sc.finishConnect()) {
                    sc.register(selector, SelectionKey.OP_READ);
                    doWrite(sc);
                }else {
                    // 连接失败
                    System.exit(1);
                }

            }
            if (key.isReadable()){
                ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                int readBytes = sc.read(readBuffer);
                if (readBytes >0){
                    readBuffer.flip();
                    byte[] bytes = new byte[readBuffer.remaining()];
                    readBuffer.get(bytes);
                    String body = new String(bytes,"UTF-8");
                    System.out.println("NOW IS: "+body);
                    this.stop = true;
                }else if (readBytes < 0){
                    // 对端链路关闭
                    key.cancel();
                    sc.close();
                }else {
                    ; //读到0字节,忽略
                }
            }
        }
    }

    private void doConnect() throws Exception{
        // 如果直接连接成功,则注册到多路复用器上,发送请求信息,读应答
        if (socketChannel.connect(new InetSocketAddress(host,port))){
            socketChannel.register(selector, SelectionKey.OP_READ);
                doWrite(socketChannel);
        }else {
            socketChannel.register(selector,SelectionKey.OP_CONNECT);
        }
    }

    private void doWrite(SocketChannel sc) throws IOException{
        byte[] req = "QUERY".getBytes();
        ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
        writeBuffer.put(req);
        writeBuffer.flip();
        sc.write(writeBuffer);
        if (!writeBuffer.hasRemaining()){
            System.out.println("send order 2 server secceed");
        }
    }
}

package com.nio.client;

/**
 * @author tangj
 * @date 2018/6/14 22:56
 */
public class TimeClient {
    public static void main(String args[]){
        new Thread(new TimeClientHandle("127.0.0.1",9816)).start();
    }
}

参考:

并发编程网

《Netty权威指南》

代码地址:

github

原文地址:https://www.cnblogs.com/superfj/p/9207294.html

时间: 2024-08-27 14:59:58

Java中的NIO基础知识的相关文章

Java中String的基础知识

Java中String的基础知识 ==与equal的区别 基本数据类型,指的是java中的八种基本数据结构(byte,short,char,int,long,float,double,boolean),一般的比较是使用的 ==,比较的是他们的值. 复合数据类型(类) ==比较的是两个对象的引用,可以理解为在内存中的地址,除非是同一个new出来的对象,他们的 ==为true,否则,都为false. equal是object中的方法.object中的实现如下,内部还是使用==实现,也就是说,如果一个

Java中浮点数的基础知识

偶然查看Math.round的JDK 1 public static int round(float a) { 2 if (a != 0x1.fffffep-2f) // greatest float value less than 0.5 3 return (int)floor(a + 0.5f); 4 else 5 return 0; 6 } 注释说0x1.fffffep-2f是最接近0.5的float类型的小数,咦,科学计数法用e表示指数我是知道的,但是这个p是什么鬼.可能有的读者还会问,

java中的NIO基础

在jdk1.4中,加入了一个新的包,java.nio.*,这个包引入了新的javaI/O库,目的是为了提高速度,实际上,旧的I/O包也使用nio重新实现过. 相对于io,nio中的这个n代表什么呢?<java编程思想>直接把小标题取名为"新I/O",另一种说法是Non-blocking的首字母,不管怎样,nio也确实是一种新的处理非阻塞的IO. NIO的几个核心组成部分:Chennels(通道),Buffers(缓存),Selectors(选择器). 我们可以把通道(Cha

[基础] Java目录(摘自Java核心技术·卷1 基础知识)

Java核心技术·卷1 基础知识(原书第9版) 第1章 Java程序设计概述 1.1 Java程序设计平台 1.2 Java"白皮书"的关键术语 1.2.1 简单性 1.2.2 面向对象 1.2.3 网络技能 1.2.4 健壮性 1.2.5 安全性 1.2.6 体系结构中立 1.2.7 可移植性 1.2.8 解释型 1.2.9 高性能 1.2.10 多线程 1.2.11 动态性 1.3 Java applet与Internet 1.4 Java发展简史 1.5 关于Java的常见误解

Java多线程完整版基础知识

Java多线程完整版基础知识 (翟开顺由厚到薄系列) 1.前言 线程是现代操作系统中一个很重要的概念,多线程功能很强大,java语言对线程提供了很好的支持,我们可以使用java提供的thread类很容易的创建多个线程.线程很不难,我对之前学习过的基础,在这做了一个整理,本文主要参考的是Java研究组织出版的j2se进阶和张孝祥-java就业培训教材这两本书 2.概述 2.1线程是什么 主要是线程与进程的区别,这里不再阐述,自行网上搜索 为什么使用线程:操作系统切换多个线程要比调度进程在速度上快很

(001)springboot中测试的基础知识以及接口和Controller的测试

(一)springboot中测试的基础知识 (1)添加starter-test依赖,范围指定为test,只在执行测试时生效 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> 完整po

《java核心技术 卷1 基础知识》二

<Java核心技术 卷1 基础知识>第4-5章 在Java中没有类就无法做任何事情 new操作符的返回值是一个引用 在Java中,如果使用一个未初始化的指针,运行系统会产生一个运行时错误. Date 用来表示时间点 LocalDate 日历表示法 同时不推荐使用Date类来处理日历 推荐使用LocalDate来处理日历 每一个拥有名字的类都会被编译生成对应的class文件 所有的Java对象都是在堆中构造的,构造器总是随着new操作符一起使用 在Java中,所有的方法都必须在类的内部定义,但并

《Java核心技术 卷1 基础知识》三

<Java核心技术 卷1 基础知识> 第六章 接口和内部类 接口不是类,而是对类的的一组需求描述. 接口不能包含实例域--接口没有实例 可以将接口看作没有实例域的抽象类 要将类声明为实现某个接口,需要使用关键字implements. 类实现一个接口的具体步骤为: 1)使用implements关键字进行声明要实现的接口 2)对接口中的所有方法进行定义 接口中的所有方法默认为public 但在实现接口时需要声明为public 这里介绍了Comparable接口,该接口只有一个方法,compareT

《Java核心技术 卷1 基础知识》七

<Java核心技术 卷1 基础知识> 第10章 图形程序设计 在Java1.0刚出现时,就包含了一个基本GUI程序设计的类库,即抽象窗口工具箱(Abstract Window Toolkit,AWT) 基本AWT库采用将处理用户界面元素的任务委派给每个目标平台(如windows.Macintosh等)的本地GUI工具箱的方式, 由本地工具箱负责用户界面元素的创建和动作 但由于在不同的平台上,操作行为有一些微妙的差别 因此,AWT也由"一次编写,随处使用"变为"一次