NIO - Scatter/Gather
1.Scatter 从一个Channel读取的信息分散到N个缓冲区中(Buufer).
2.Gather 将N个Buffer里面内容按照顺序发送到一个Channel.
Scatter/Gather功能是通道(Channel)提供的 并不是Buffer,
Scatter/Gather相关接口 类图
ReadableByteChannel WritableByteChannel 接口提供了通道的读写功能
ScatteringByteChannel GatheringByteChannel接口都新增了两个以缓冲区数组作为参数的相应方法
以FileChannel为例
*Scatter
/*** Scatter* <br>------------------------------<br>* @param fileName* @throws IOException* @see FileChannel.read(java.nio.ByteBuffer[])*/private static void scatter(final String fileName) throws IOException {RandomAccessFile accessFile = new RandomAccessFile(fileName, "r");//获取文件通道FileChannel channel = accessFile.getChannel();//创建两个缓冲区ByteBuffer headBuffer = ByteBuffer.allocate(2);ByteBuffer bodyBuffer = ByteBuffer.allocate(1024);ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer};// headBuffer 前10个字节// bodyBuffer 剩下的 long n = channel.read(allBuffers);System.out.println("共读到多少字节:" + n);headBuffer.flip();//head缓冲区中的数据:qwSystem.out.println("head缓冲区中的数据:" + charset.decode(headBuffer));bodyBuffer.flip();//body缓冲区中的数据:ertyuiopSystem.out.println("body缓冲区中的数据:" + charset.decode(bodyBuffer));accessFile.close();channel.close();}/*** Scatter2* <br>------------------------------<br>* @param fileName* @throws IOException* @see FileChannel.read(java.nio.ByteBuffer[], int, int)*/private static void scatter2(final String fileName) throws IOException {RandomAccessFile accessFile = new RandomAccessFile(fileName, "r");//获取文件通道FileChannel channel = accessFile.getChannel();//创建五个缓冲区ByteBuffer headBuffer = ByteBuffer.allocate(2);ByteBuffer bodyBuffer1 = ByteBuffer.allocate(3);ByteBuffer bodyBuffer2 = ByteBuffer.allocate(2);ByteBuffer bodyBuffer3 = ByteBuffer.allocate(2);ByteBuffer bodyBuffer4 = ByteBuffer.allocate(1);ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer1, bodyBuffer2,bodyBuffer3, bodyBuffer4,};//0从那个缓冲区开始被使用 使用3个缓冲区//会使用 headBuffer,bodyBuffer1,bodyBuffer2long n = channel.read(allBuffers, 0, 3);System.out.println("共读到多少字节:" + n);headBuffer.flip();//head缓冲区中的数据:qwSystem.out.println("head缓冲区中的数据:" + charset.decode(headBuffer));bodyBuffer1.flip();//body1缓冲区中的数据:ertSystem.out.println("body1缓冲区中的数据:" + charset.decode(bodyBuffer1));bodyBuffer2.flip();//body2缓冲区中的数据:yuSystem.out.println("body2缓冲区中的数据:" + charset.decode(bodyBuffer2));bodyBuffer3.flip();//body3,没有数据System.out.println("body3缓冲区中的数据:" + charset.decode(bodyBuffer3));bodyBuffer4.flip();//body4没有数据System.out.println("body4缓冲区中的数据:" + charset.decode(bodyBuffer4));accessFile.close();channel.close();}/**** <br>------------------------------<br>* @param fileName* @throws IOException*/private static void writeData(final String fileName, String data) throws IOException {RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");accessFile.writeBytes(data);accessFile.close();}
private static Charset charset = Charset.forName("GBK");public static void main(String[] args) throws IOException {final String fileName = "D:/test.log";//先写入10个字节数据 以便测试 scatter模式writeData(fileName, "qwertyuiop");/**----------Scatter------------*///read(java.nio.ByteBuffer[])scatter(fileName);//read(java.nio.ByteBuffer[], int, int)scatter2(fileName);}
*Gather
/*** gather* <br>------------------------------<br>* @param fileName* @throws IOException * @see FileChannel#write(java.nio.ByteBuffer[])*/private static void gather(String fileName) throws IOException {RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");//获取文件通道FileChannel channel = accessFile.getChannel();//创建两个缓冲区ByteBuffer headBuffer = ByteBuffer.allocate(3);headBuffer.put("abc".getBytes());ByteBuffer bodyBuffer = ByteBuffer.allocate(1024);bodyBuffer.put("defg".getBytes());ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer};headBuffer.flip();bodyBuffer.flip();//将按allBuffers顺序 写入abcdefglong n = channel.write(allBuffers);System.out.println("共写入多少字节:" + n);accessFile.close();channel.close();}/*** gather2* <br>------------------------------<br>* @param fileName* @throws IOException * @see FileChannel#write(java.nio.ByteBuffer[], int, int)*/private static void gather2(String fileName) throws IOException {RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");//获取文件通道FileChannel channel = accessFile.getChannel();//创建两个缓冲区ByteBuffer headBuffer = ByteBuffer.allocate(3);ByteBuffer bodyBuffer1 = ByteBuffer.allocate(4);ByteBuffer bodyBuffer2 = ByteBuffer.allocate(20);ByteBuffer bodyBuffer3 = ByteBuffer.allocate(20);ByteBuffer bodyBuffer4 = ByteBuffer.allocate(20);headBuffer.put("abc".getBytes());bodyBuffer1.put("defg".getBytes());bodyBuffer2.put("bnbnbnb".getBytes());bodyBuffer3.put("zzz444".getBytes());ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer1, bodyBuffer2,bodyBuffer3, bodyBuffer4,};headBuffer.flip();bodyBuffer1.flip();bodyBuffer2.flip();bodyBuffer3.flip();bodyBuffer4.flip();//将按allBuffers数组顺序使用两个缓冲区//0从哪开始//2使用几个//当前使用headBuffer bodyBuffer1//最终写入abcdefglong n = channel.write(allBuffers, 0, 2);//应该返回7个字节System.out.println("共写入多少字节:" + n);accessFile.close();channel.close();}
private static Charset charset = Charset.forName("GBK");public static void main(String[] args) throws IOException {final String fileName = "D:/test.log";/**----------Gather------------*///FileChannel#write(java.nio.ByteBuffer[])gather(fileName);//FileChannel#write(java.nio.ByteBuffer[], int, int)gather2(fileName);}
转载于:https://www.cnblogs.com/yangjin-55/archive/2012/05/31/2786538.html
NIO - Scatter/Gather相关推荐
- 【Java NIO的深入研究6】JAVA NIO之Scatter/Gather
Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道)中读取或者写入到Channel的操作. 分散(s ...
- Java NIO系列教程(四) Scatter/Gather
转载自 Java NIO系列教程(四) Scatter/Gather 译文地址 作者:Jakob Jenkov 译者:郭蕾 Java NIO开始支持scatter/gather,scatte ...
- java nio 系列教程 四_Java NIO系列教程(四) Scatter/Gather
作者:Jakob Jenkov 译者:郭蕾 Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Channel在中文经常翻译为通道) ...
- RDMA技术详解(三):理解RDMA Scatter Gather List
1. 前言 在使用RDMA操作之前,我们需要了解一些RDMA API中的一些需要的值.其中在ibv_send_wr我们需要一个sg_list的数组,sg_list是用来存放ibv_sge元素,那么什么 ...
- JavaNIO - Scatter Gather
Scatter & Gather指在多个缓冲区上实现一个简单的 I/O 操作.减少或避免了Buffer间的拷贝和系统调用. Channel Write操作 Write操作 Channel Re ...
- 5 Java NIO Scatter 与Gather-翻译
Java NIO开始支持scatter与gatter.scatter与gatter用于实现从Channel读数据和向通道写数据. scatter从一个通道读取数据并写入到多个Buffer中.因此,sc ...
- Java IO/NIO教程
Java IO教程 http://tutorials.jenkov.com/java-io/index.html Java NIO教程 英文版: http://tutorials.jenkov.com ...
- Java NIO Introduction
2019独角兽企业重金招聘Python工程师标准>>> Java NIO (New IO) is an alternative IO API for Java (from Java ...
- 海纳百川而来的一篇相当全面的Java NIO教程
目录 零.NIO包 一.Java NIO Channel通道 Channel的实现(Channel Implementations) Channel的基础示例(Basic Channel Exampl ...
最新文章
- 有负权重边的图可以有拉普拉斯矩阵吗?
- C++ Primer 5th笔记(chap 19 特殊工具与技术)将成员函数用作可调用对象
- 汽车新零售转型之路怎么走?你需要知道这些
- adnroid开发环境的搭建
- java高级断言_Java之断言
- oracle 10 TNSLSNR.EXE 占用了8080端口怎么办
- html5播放器占用带宽情况,分享|用 bmon 查看网络带宽使用情况
- MongoDB Sharding 机制分析
- Echarts数据可视化legend图例,开发全解+完美注释
- Https之SSL原理
- js基础——function类型
- 精通innodb引擎_《MySQL技术内幕:InnoDB存储引擎》PDF 下载
- 十七世纪的常用对数表是怎么算出来的
- 20道嵌入式工程师面试题(附答案)
- 计算机电源输出定义,电脑电源接口定义图解
- 驻波在物理上的应用与魅力
- python_计算一张纸对折多少次超过珠峰
- Windows 内网渗透之攻击域控
- vue具名插槽的使用
- Flink Joining
热门文章
- mysql数据更新回退_Mysql的几个灵魂拷问(一)
- java 整合solr_SpringBoot整合Spring Data Solr
- python正则表达式_Python正则表达式简记和re库!
- ggplot2中显示坐标轴_R可视化11|ggplot2-图层图形语法 (3)
- 用计算机计算成品率计算公式,计算机专业英语+单词+部分习题.doc
- 视频监控系统供电方式及选择方法
- Corner Proposal Network 论文阅读(2020ECCV)
- 【机器视觉案例】(11) 眨眼计数器,人脸关键点检测,附python完整代码
- 学习java周期_Java第一作业周期总结
- 结构光测距相位差_ROHM确立新型VCSEL模块技术 有助于提高测距精度