Buffer的capacity,limit,position
limit,position的含义取决于是读模式还是写模式
capacity
buffer的大小
limit
write:和capacity一样,表示能写多少
read:表示最多可读多少
position
write:write index,最大值 capacity-1
read:read index,从写模式切换到读模式时,position会被重置为0
buffer的类型
- ByteBuffer
- MappedByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
public class FileTest { public static void main(String[] args) throws IOException { RandomAccessFile raFile = new RandomAccessFile("/Users/shifulong/monitor_contact.sql", "rw"); FileChannel fileChannel = raFile.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(64); while (fileChannel.read(byteBuffer) != -1) {//read in to buffer byteBuffer.flip(); while (byteBuffer.hasRemaining()) { System.out.print((char) byteBuffer.get()); } byteBuffer.clear(); } } }
典型的几个方法
ByteBuffer byteBuffer = ByteBuffer.allocate(64);//获取一个64大小的buffer byteBuffer.flip();//将buffer从写模式切换到读模式 byteBuffer.rewind();//将position设为0,可重读数据 byteBuffer.clear();//清除buffer position设为0,如果buffer中还有数据没有读,就丢失了 byteBuffer.compact();//清除buffer,并把没读的数据copy到起始处,position设到最后一个没读的数据后面,可以追加写 byteBuffer.mark();//标记一个特定postion byteBuffer.reset();//恢复标记,恢复到标记的position
分散&聚集
多个buffer写到channel中
从channel总读到多个buffer中
//分散 public void scatter() throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("", ""); FileChannel fileChannel = randomAccessFile.getChannel(); ByteBuffer head = ByteBuffer.allocate(16); ByteBuffer body = ByteBuffer.allocate(64); fileChannel.read(new ByteBuffer[]{head, body});//读到多个buffer中,不方便操作不定长的消息 } //聚集 public void gather() throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("", ""); FileChannel fileChannel = randomAccessFile.getChannel(); ByteBuffer buffer1 = ByteBuffer.allocate(16);//need put byte to buffer ByteBuffer buffer2 = ByteBuffer.allocate(64); fileChannel.write(new ByteBuffer[]{buffer1, buffer2});//多个buffer写到channel,可以操作不定长的消息 }
通道之间的数据传输
只要两个通道之间有一个是FileChannel,就可以将一个channel传输到另一个channel
public static void from() throws IOException { RandomAccessFile fromFile = new RandomAccessFile("/Users/shifulong/monitor_contact.sql", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("/Users/shifulong/monitor_contact_to.sql", "rw"); FileChannel toChannel = toFile.getChannel(); long position = 0; toChannel.transferFrom(fromChannel,position,fromChannel.size()); toChannel.close(); fromChannel.close(); } public static void to() throws IOException { RandomAccessFile fromFile = new RandomAccessFile("/Users/shifulong/monitor_contact.sql", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("/Users/shifulong/monitor_contact_to.sql", "rw"); FileChannel toChannel = toFile.getChannel(); long position = 0; fromChannel.transferTo(position,toChannel.size(),toChannel); fromChannel.close(); toChannel.close(); }
selector
Selector selector = Selector.open(); SelectableChannel channel = null;// TODO channel.configureBlocking(false);//非阻塞模式 //channel注册selector,第二个参数是intreast事件,多个intreast事件可以用“|” // int intreastSetP = SelectionKey.OP_ACCEPT | SelectionKey.OP_READ; SelectionKey selectionKey1 = channel.register(selector, SelectionKey.OP_READ); int intreastSet = selectionKey1.interestOps(); int readySet = selectionKey1.readyOps(); //添加附加对象 selectionKey1.attach(new Object()); //取出附加对象 selectionKey1.attachment(); //选取通道 selector.select();//block when at least a ready channel that you regist selector.select(1000);//timeout selector.selectNow();//return immediately Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey selectionKey = iterator.next(); if (selectionKey.isAcceptable()) { // 这两个含义不太清楚 } else if (selectionKey.isConnectable()) { // 这两个含义不太清楚 } else if (selectionKey.isReadable()) { // a channel is ready for reading } else if (selectionKey.isWritable()) { // a channel is ready for writing } iterator.remove(); SelectableChannel selectableChannel = selectionKey.channel(); }
相关推荐
主要介绍一些关于NIO 的基础知识,有浅到深
目录运行效果截图:源代码:Server端:Client端:使用时注意:(持续更新) 运行效果截图: 启动server端: 依次启动client1、client2、client3并输入相应昵称...import java.nio.channels.Channel; import java.nio.c
NULL 博文链接:https://zhangshixi.iteye.com/blog/683767
NULL 博文链接:https://zhangshixi.iteye.com/blog/679959
NULL 博文链接:https://zhangshixi.iteye.com/blog/685022
NULL 博文链接:https://zhangshixi.iteye.com/blog/684544
利用磁控溅射法,在ITO玻璃基底上沉积NiO薄膜和ZnO:Al(Al掺杂的ZnO或AZO)薄膜,制备具有半导体特性的NiO/ZnO:A1透明异质结二极管。使用UV-1700型分光光度计、KEITHLEY4200-SCS半导体测试仪、JSM-6490LV型扫描电子...
Java NIO系列教程 Java NIO Channel Buffer Selector SocketChannel
Java NIO系列教程(二) Channel Java NIO系列教程(三) Buffer Java NIO系列教程(四) Scatter/Gather Java NIO系列教程(五) 通道之间的数据传输 Java NIO系列教程(六) Selector Java NIO系列教程(七) ...
赠送jar包:httpcore-nio-4.4.6.jar; 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar; 赠送源代码:httpcore-nio-4.4.6-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.6.pom; 包含翻译后的API文档:...
Nio学习笔记
面向缓冲区(buffer,channel) 堵塞io 非堵塞io - 选择器 java nio主要的核心组件 缓冲区 buffer 通道 Channels 选择器 Selectors java nio缓冲区buffer 简介 Buffer是数据的容器,在nio中负责数据的存取,java为...
JAVA NIO学习资料JAVA NIO学习资料
赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...
Next, you'll learn about NIO's buffer, channel, selector, regular expression, charset, and formatter APIs. Finally, you'll discover NIO.2's offerings in terms of an improved file system interface, ...
java nio作者倾力所写的简单易懂的NIO学习指南,让你顺利拿下NIO开发,包括了NIO中的Buffer,Channel,Selector的介绍,理论&代码都有,是你学习NIO的不二法宝~
Java I/O学习笔记: 磁盘操作 字节操作 字符操作 对象操作 网络操作 NIO & AIO Java I/O Java是一种面向对象的编程语言,由Sun Microsystems于1995年推出。它是一种跨平台的语言,意味着可以在不同的操作系统上运行...
尚硅谷_NIO_缓冲区(Buffer)的数据存取 ·03. 尚硅谷_NIO_直接缓冲区与非直接缓冲区 ·04. 尚硅谷_NIO_通道(Channel)的原理与获取 ·05. 尚硅谷_NIO_通道的数据传输与内存映射文件 ·06. 尚硅谷_NIO_分散读取与聚集...
NIO是NewI/O的简称,具有以下特性:为所有的原始类型提供(Buffer)缓存支持;使用java.nio.charset.Charset作为字符集编码解码解决方案;增加通道(channel)对象,作为新的原始I/O抽象;支持锁和内存映射文件的...
NIO学习总结 主要内容: NIO简介: Java NIO 是 java 1.4, 之后新出的一套IO接口NIO中的N可以理解为Non-blocking,不单纯是New。 NIO的特性/NIO与IO区别: 1)IO是面向流的,NIO是面向缓冲区的; 2)IO流是阻塞的,NIO流...