NIO基础篇(一)
1.NIO与传统IO的比较
Java的NIO(New IO)是不同于旧IO的,旧的IO是基于字节流和字符流的,是阻塞的IO。NIO是基于通道(Channel)和缓冲区(Buffer)的,是非阻塞的IO。
使用旧IO每次读取一行数据流的流程图如下,Thread必须等待,等待readline读到一行的数据并返回。

使用NIO,可以周期判断Buffer中是否有数据,没有数据时还可以去做其它的事情。基于缓存(Buffer)在一定程度上减少了读写速度不一致所带来的等待。
NIO可以使用单线程管理多个通道(网络连接或文件),但付出的代价是解析数据可能会比从一个阻塞流中读取数据更复杂。如果需要管理同时打开的成千上万个连接,这些连接每次只是发送少量的数据,例如聊天服务器,实现NIO的服务器可能是一个优势。

如果有少量的连接使用非常高的带宽,一次发送大量的数据,旧IO模型实现可能非常契合。

Java中几种IO的比较:
BIO:传统的IO,是同步阻塞IO
NIO:同步非阻塞IO,轮询缓冲区的状态。
AIO: jdk1.7开始支持,异步非阻塞IO
2.Channel、Buffer和Selector
NIO的核心组成部分:Channel、Buffer和Selector。
选择器(Selector)用于监听多通道中的事件。

Channel的主要实现:
- FileChannel : 从文件中读写数据
- DatagramChannel : 基于UDP读写网络中的数据
- SocketChannel : 基于TCP读写网络中的数据
- ServerSocketChannel : 监听新进来的TCP连接,像Web服务器那样。对每一个新进来的连接都会创建一个SocketChannel
Buffer的主要实现:
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
Buffer中还有一个特殊的类型:MappedByteBuffer,用于实现内存映射。

向Selector注册Channel,然后调用它的select()方法,这个方法会一直阻塞到通道中事件的发生,如新建连接、数据接收等。
3.Channel示例
我们将从文件中读取内容并打印,将使用Channel和Buffer。
文件中的内容如下:
if you try,you may have a chance!
if you try,you may have a chance2! 朋友!
朋友!
朋友!
朋友!
朋友!
朋友!
朋友!
读写的例子程序如下,其中还考虑了中文和读到半个中文字符的情况,只适用于文件编码格式为GBK的情形,这时一个汉字占2个字节。编码为UTF-8时,占用3个字节,需要重新考虑:
package nio; import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class nio { public static void main(String[] args) throws Exception {
nio n = new nio();
n.readDataToBuffer();
} private void readDataToBuffer() throws Exception {
RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");
FileChannel inChannel = aFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(48); byte [] bytes = new byte [48];
int index = 0;
buf.put("just for test ".getBytes(),3,6); //直接往buffer中放数据
int bytesRead = inChannel.read(buf); //数据通过chanel读到buffer中
int isChineseCharacterIndex = 0;
while (bytesRead != -1) { //缓冲区数据大小,-1表示没有数据
buf.flip(); //从写模式切换到读模式 while (buf.hasRemaining()) {
bytes[index] = buf.get();
if(bytes[index]<0){ //判断在最后读到半个汉字的情况,汉字占2个byte,这2个byte都是小于0的
isChineseCharacterIndex ++;
}
index++;
}
if(isChineseCharacterIndex%2 !=0){ //读到了半个汉字的情况,这半个汉字不显示,并将这半个汉字重新加到buffer中
System.out.println("Read " + (index-1));
System.out.println(new String(bytes,0,index-1));
buf.position(index-1);
buf.compact();
}else{
System.out.println("Read " + index);
System.out.println(new String(bytes,0,index));
buf.clear();
}
bytesRead = inChannel.read(buf);
index = 0;
isChineseCharacterIndex = 0;
//bytes = new byte [48]; read new data will override the old one
}
inChannel.position(inChannel.size()); //定位到文件末尾 //inChannel.write(ByteBuffer.wrap("\r\n朋友!".getBytes())); inChannel.close();
aFile.close();
}
}
输出结果为:
Read 48
t for if you try,you may have a chance!
if you
Read 47
try,you may have a chance2! 朋友!
朋友!
朋
Read 31
友!
朋友!
朋友!
朋友!
朋友!
分析一下上面的代码, ByteBuffer.allocate(48)定义了一个大小为48B的缓冲区,inChannel.read(buf)是通过管道向缓冲区中写入数据,bytesRead != -1是指通过管道向缓冲区中写入的数据流不为空,下面的代码是循环的从缓冲区中读出所写入的数据。
buf.flip() 是反转Buffer的读写状态,从之前的写状态变为读状态。
Channel从文件中读数据到Buffer中时,我们也可以直接向Buffer中写入数据。flip切换成写状态时,写入到Channel中的数据也将写入到文件中。
还需要注意的是:buf.position(index-1); buf.compact();作用是只保留最后一位字符(也就是半个中文字符)并移至Buffer顶端后清空Buffer。
读完缓冲区中的数据后调用clear方法将清空缓冲区的数据。
4.Buffer示例
理解Buffer,首先需要理解position、limit和capacity在读模式和写模式中的不同作用。

position在读模式表示当前写入数据的位置,从0开始,写模式下,position从0增加到capacity-1。读模式下position表示可以读取到的数据的位置,position将一直等于0。
limit在写模式下等于capacity,表示最多可以写入多少数据。limit在读模式下,表示Buffer中一共有多少个数据。
capacity在读模式和写模式下的作用相同,都表示最多可以写入多少数据。
理解position、limit和capacity在读模式和写模式中的不同作用后,还需要了解Buffer中一些常用方法的使用,如下。
clear(),只是把指针移到位置0,并没有真正清空数据。
flip(), limit = position,指针指向0,常在读模式和写模式切换时使用。
rewind(),指针指向0。
compact(),压缩数据。比如当前capacity是6,当前指针指向2(即0,1的数据已经写出了,没用了),那么compact方法将把2,3,4,5的数据挪到0,1,2,3的位置。
clear()和rewind()都是为新读入数据做好准备,区别在于clear会将capacity重置,而rewind不会重置capacity。
关于clear、flip、rewind、compact和clear方法的使用,可以参考下面的例子:
package nio; import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class nio2 {
public static void main(String[] args) throws Exception {
nio2 n = new nio2();
n.readDataToBuffer();
} private void readDataToBuffer() throws Exception {
RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");
FileChannel inChannel = aFile.getChannel(); //一般在把数据写入Buffer前调用clear,一般在从Buffer读出数据前调用flip,一般在把数据重写入Buffer前调用rewind
//Buffer and it's subclass is not thread safety
ByteBuffer buf = ByteBuffer.allocate(48); byte [] chars = new byte [48];
int index = 0;
int bytesRead = inChannel.read(buf);
System.out.println("============test rewind and clear method begin===============");
if (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip(); //change read mode to write mode while (buf.hasRemaining()) {
chars[index] = buf.get();
index++;
}
System.out.println(new String(chars,0,index)); //中文的情况
}
buf.rewind(); //rewind method just reset the pointer position,not clear buffer,and we can get the data again
index = 0;
System.out.println("Read " + bytesRead);
//buf.flip(); //change write mode to read mode,and when write mode change to read mode,buffer will clear
//System.out.println("position: " + buf.position()+ ",capacity: " + buf.capacity() + ",limit: " + buf.limit()); //buf.clear(); //clear method just reset the pointer position,not clear buffer,and we can get the data again
while (buf.hasRemaining()) {
chars[index] = buf.get();
index++;
}
System.out.println(new String(chars));
System.out.println("============test rewind and clear method end==============="); //test compact method,
System.out.println("============test compact method begin===============");
buf.position(35); //assume has read 35 byte,set the pointer to 35
buf.compact(); //will not clear the buffer,but copy date 35-47 to 0-12,and the pointer to 13
System.out.println("position: " + buf.position()+ ",capacity: " + buf.capacity() + ",limit: " + buf.limit());
index = 0;
for(int i=0;i<13;i++){
chars[index] = buf.get(i);
index++;
} System.out.println("data before the pointer: " + new String(chars,0,index));
index = 0;
while (buf.hasRemaining()) {
chars[index] = buf.get();
index++;
}
System.out.println("data after the pointer: " + new String(chars,0,index));
System.out.println("============test compact method end==============="); //test mark and reset method
System.out.println("============test mark and reset method begin===============");
index = 0;
buf.rewind();
while (buf.hasRemaining()) {
chars[index] = buf.get();
index++;
if(index == 13){
buf.mark();
}
}
buf.reset(); System.out.println("position after reset: " + buf.position());
index = 0;
while (buf.hasRemaining()) {
chars[index] = buf.get();
index++;
}
System.out.println("data after reset: " + new String(chars,0,index));
System.out.println("============test mark and reset method end==============="); inChannel.close();
aFile.close();
}
}
输出结果为:
============test rewind and clear method begin===============
Read 48
if you try,you may have a chance!
if you try,yo
Read 48
if you try,you may have a chance!
if you try,yo
============test rewind and clear method end===============
============test compact method begin===============
position: 13,capacity: 48,limit: 48
data before the pointer: if you try,yo
data after the pointer: u may have a chance!
if you try,yo
============test compact method end===============
============test mark and reset method begin===============
position after reset: 13
data after reset: u may have a chance!
if you try,yo
============test mark and reset method end===============
写数据到Buffer有两种方式:
从Channel写到Buffer。 int bytesRead = inChannel.read(buf);
通过Buffer的put()方法写到Buffer里。 buf.put(127);
从Buffer中读取数据有两种方式:
从Buffer读取数据到Channel。 int bytesWritten = inChannel.write(buf);
使用get()方法从Buffer中读取数据。 byte aByte = buf.get();
还可以直接将一个Channel中的数据直接传输到另一个Channel中,可以见下面的例子:
package nio; import java.io.RandomAccessFile;
import java.nio.channels.FileChannel; public class nio3 { public static void main(String[] args) throws Exception{
RandomAccessFile fromFile = new RandomAccessFile("test.txt", "rw");
FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel(); long position = 0;
long count = fromChannel.size(); toChannel.transferFrom(fromChannel,position, count);
//fromChannel.transferTo(position, count, toChannel); toChannel.close(); //channel关闭时会将数据写入
fromFile.close();
toFile.close();
} }
运行代码后,将把test.txt中的内容拷贝到toFile.txt。
NIO基础篇(一)的更多相关文章
- NIO基础篇(三)
NIO里对性能提升最显著的是内存映射(memory mapping),内存访问的速度往往比文件访问的速度快几个数量级. 在内存映射之前,还需要看NIO的一些其他的特性. 缓冲区分片 slice()方法 ...
- NIO基础篇(二)
Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 传统的 ...
- 撸基础篇系列,JAVA的NIO部分
前言:撸基础篇系列,避免每次都要从头开始看,写个自己的知识体系树 NIO 核心就是异步, 比如,复制文件,让操作系统去处理,等通知 BIO核心类 一,BIO NIO基本操作类 Bytebuffer 构 ...
- NIO相关基础篇一
转载请注明原创出处,谢谢! 说在前面 NIO相关知识是很多后续的一些基础知识,所以今天这篇文章仅仅是简单介绍,后续会继续有一到二篇相关NIO内容. 什么是NIO Java NIO( New IO) 是 ...
- NIO相关基础篇二
转载请注明原创出处,谢谢! 上篇NIO相关基础篇一,主要介绍了一些基本的概念以及缓冲区(Buffer)和通道(Channel),本篇继续NIO相关话题内容,主要就是文件锁.以及比较关键的Selecto ...
- NIO相关基础篇三
转载请注明原创出处,谢谢! 说在前面 上篇NIO相关基础篇二,主要介绍了文件锁.以及比较关键的Selector,本篇继续NIO相关话题内容,主要谈谈一些Linux 网络 I/O模型.零拷贝等一些内容, ...
- Jenkins: 基础篇(环境配置)
自动化领域比较有影响力的开源框架jenkins,确实比较强大,易用.很多公司将其用来做持续即成CI(continuous integration).为了拓展和强化自己的软件设计生态系统,也将很久前使用 ...
- 深入浅出微服务框架dubbo(一):基础篇
一.基础篇 1.1 开篇说明 dubbo是一个分布式服务框架,致力于提供高性能透明化RPC远程调用方案,提供SOA服务治理解决方案.本文旨在将对dubbo的使用和学习总结起来,深入源码探究原理,以备今 ...
- 小白—职场之Java基础篇
java基础篇 java基础 目录 1.java是一种什么语言,jdk,jre,jvm三者的区别 2.java 1.5之后的三大版本 3.java跨平台及其原理 4.java 语言的特点 5.什么是字 ...
随机推荐
- BZOJ3930: [CQOI2015]选数
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3930 容斥原理. 令l=(L-1)/k,r=R/k,这样找k的倍数就相当于找1的倍数. 设F[ ...
- 安装myeclipse后,打开时弹出:“该站点安全证书的吊销证书不可用”,怎样解决?
1.当弹出"该站点安全证书的吊销信息不可用.是否继续?"的对话框时,点击"查看证书",切换到"详细信息"TAB页,找到其"CRL分 ...
- [国嵌攻略][173][BOA嵌入式服务器移植]
1.解压boa嵌入式web服务 tar zxvf boa-0.94.13.tar.gz 2.进入src目录生成配置文件 ./configure 3.修改生成的Makefile CC=arm-linux ...
- React Native之Image组件
同 HTML 的 img 元素一样,React Native 提供的 Image 组件可以用来显示各种途径的图片,比如网络图片.本地图片.照相机图片等. 虽然效果是一样的.然而用法还是有区别的. 1. ...
- solrcloud(solr集群版)安装与配置
1 Solr集群 1.1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的 ...
- Linux中rpm命令用法听语音
rpm 是红帽(RedHat)软件包管理工具,实现类似于 Windows 中的添加/删除程序功能.下面,就来向大家介绍 rpm 命令的用法. 工具/原料 CentOS 一.rpm常用参数 1 rpm ...
- 开源三维地球GIS引擎Cesium常用功能的开发
Cesium是一个非常优秀的三维地球GIS引擎(开源且免费).能够加载各种符合标准的地图图层,瓦片图.矢量图等都支持.支持3DMax等建模软件生成的obj文件,支持通用的GIS计算:支持DEM高程图. ...
- git 签出(恢复)指定文件
在项目开发中,偶尔会因为误删文件或其他原因需要从git仓库中恢复某些文件.此篇文章将介绍如何通过git从历史提交记录.分支记录恢复指定文件. 1. git checkout 说明:使用git chec ...
- Linux网络设备驱动架构
Linux网络设备驱动程序体系结构分为四层:网络协议接口层.网络设备接口层.提供实际功能的设备驱动层以及网络设备与媒介层. (1)网络协议接口层向网络层协议提供统一的数据包收发接口,不论上层协议是AR ...
- duilib消息类型
//定义所有消息类型 ////////////////////////////////////////////////////////////////////////// #define DUI_MS ...