NIO - Scatter/Gather

2024-05-01 17:38
文章标签 nio gather scatter

本文主要是介绍NIO - Scatter/Gather,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ZZ:http://blog.csdn.net/java2000_wl/article/details/7619395

1.Scatter  从一个Channel读取的信息分散到N个缓冲区中(Buufer).

2.Gather  将N个Buffer里面内容按照顺序发送到一个Channel.  

    Scatter/Gather功能是通道(Channel)提供的  并不是Buffer,

Scatter/Gather相关接口 类图

    ReadableByteChannel WritableByteChannel     接口提供了通道的读写功能

    ScatteringByteChannel  GatheringByteChannel接口都新增了两个以缓冲区数组作为参数的相应方法

   以FileChannel为例

   *Scatter

[java]  view plain copy print ?
  1.        /** 
  2.  * Scatter 
  3.  * <br>------------------------------<br> 
  4.  * @param fileName 
  5.  * @throws IOException 
  6.  * @see FileChannel.read(java.nio.ByteBuffer[]) 
  7.  */  
  8. private static void scatter(final String fileName) throws IOException {  
  9.     RandomAccessFile accessFile = new RandomAccessFile(fileName, "r");  
  10.     //获取文件通道  
  11.     FileChannel channel = accessFile.getChannel();  
  12.     //创建两个缓冲区  
  13.     ByteBuffer headBuffer = ByteBuffer.allocate(2);  
  14.     ByteBuffer bodyBuffer = ByteBuffer.allocate(1024);  
  15.       
  16.     ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer};  
  17.     // headBuffer 前10个字节  
  18.     // bodyBuffer 剩下的   
  19.     long n = channel.read(allBuffers);  
  20.     System.out.println("共读到多少字节:" + n);  
  21.       
  22.     headBuffer.flip();  
  23.     //head缓冲区中的数据:qw  
  24.     System.out.println("head缓冲区中的数据:" + charset.decode(headBuffer));  
  25.       
  26.     bodyBuffer.flip();  
  27.     //body缓冲区中的数据:ertyuiop  
  28.     System.out.println("body缓冲区中的数据:" + charset.decode(bodyBuffer));  
  29.     accessFile.close();  
  30.     channel.close();  
  31. }  
  32.   
  33. /** 
  34.  * Scatter2 
  35.  * <br>------------------------------<br> 
  36.  * @param fileName 
  37.  * @throws IOException 
  38.  * @see FileChannel.read(java.nio.ByteBuffer[], int, int) 
  39.  */  
  40. private static void scatter2(final String fileName) throws IOException {  
  41.     RandomAccessFile accessFile = new RandomAccessFile(fileName, "r");  
  42.     //获取文件通道  
  43.     FileChannel channel = accessFile.getChannel();  
  44.     //创建五个缓冲区  
  45.     ByteBuffer headBuffer = ByteBuffer.allocate(2);  
  46.     ByteBuffer bodyBuffer1 = ByteBuffer.allocate(3);  
  47.     ByteBuffer bodyBuffer2 = ByteBuffer.allocate(2);  
  48.     ByteBuffer bodyBuffer3 = ByteBuffer.allocate(2);  
  49.     ByteBuffer bodyBuffer4 = ByteBuffer.allocate(1);  
  50.       
  51.     ByteBuffer[] allBuffers = new ByteBuffer[]{  
  52.             headBuffer,   
  53.             bodyBuffer1, bodyBuffer2,  
  54.             bodyBuffer3, bodyBuffer4,};  
  55.     //0从那个缓冲区开始被使用    使用3个缓冲区  
  56.     //会使用 headBuffer,bodyBuffer1,bodyBuffer2  
  57.     long n = channel.read(allBuffers, 03);  
  58.       
  59.     System.out.println("共读到多少字节:" + n);  
  60.       
  61.     headBuffer.flip();  
  62.     //head缓冲区中的数据:qw  
  63.     System.out.println("head缓冲区中的数据:" + charset.decode(headBuffer));  
  64.       
  65.     bodyBuffer1.flip();  
  66.     //body1缓冲区中的数据:ert  
  67.     System.out.println("body1缓冲区中的数据:" + charset.decode(bodyBuffer1));  
  68.       
  69.     bodyBuffer2.flip();  
  70.     //body2缓冲区中的数据:yu  
  71.     System.out.println("body2缓冲区中的数据:" + charset.decode(bodyBuffer2));  
  72.       
  73.     bodyBuffer3.flip();  
  74.     //body3,没有数据  
  75.     System.out.println("body3缓冲区中的数据:" + charset.decode(bodyBuffer3));  
  76.       
  77.     bodyBuffer4.flip();  
  78.     //body4没有数据  
  79.     System.out.println("body4缓冲区中的数据:" + charset.decode(bodyBuffer4));  
  80.       
  81.     accessFile.close();  
  82.     channel.close();  
  83. }  
  84.   
  85. /** 
  86.  * 
  87.  * <br>------------------------------<br> 
  88.  * @param fileName 
  89.  * @throws IOException 
  90.  */  
  91. private static void writeData(final String fileName, String data) throws IOException {  
  92.     RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");  
  93.     accessFile.writeBytes(data);  
  94.     accessFile.close();  
  95. }  
[java]  view plain copy print ?
  1. private static Charset charset = Charset.forName("GBK");  
  2.   
  3. public static void main(String[] args) throws IOException {  
  4.     final String fileName = "D:/test.log";  
  5.     //先写入10个字节数据 以便测试 scatter模式  
  6.     writeData(fileName, "qwertyuiop");  
  7.       
  8.     /**----------Scatter------------*/  
  9.     //read(java.nio.ByteBuffer[])  
  10.     scatter(fileName);  
  11.       
  12.     //read(java.nio.ByteBuffer[], int, int)  
  13.     scatter2(fileName);  
  14. }  

*Gather

[java]  view plain copy print ?
  1. /** 
  2.  * gather 
  3.  * <br>------------------------------<br> 
  4.  * @param fileName 
  5.  * @throws IOException  
  6.  * @see FileChannel#write(java.nio.ByteBuffer[]) 
  7.  */  
  8. private static void gather(String fileName) throws IOException {  
  9.     RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");  
  10.     //获取文件通道  
  11.     FileChannel channel = accessFile.getChannel();  
  12.     //创建两个缓冲区  
  13.     ByteBuffer headBuffer = ByteBuffer.allocate(3);  
  14.     headBuffer.put("abc".getBytes());  
  15.       
  16.     ByteBuffer bodyBuffer = ByteBuffer.allocate(1024);  
  17.     bodyBuffer.put("defg".getBytes());  
  18.       
  19.     ByteBuffer[] allBuffers = new ByteBuffer[]{headBuffer, bodyBuffer};  
  20.       
  21.     headBuffer.flip();  
  22.     bodyBuffer.flip();  
  23.       
  24.     //将按allBuffers顺序  写入abcdefg  
  25.     long n = channel.write(allBuffers);  
  26.       
  27.     System.out.println("共写入多少字节:" + n);  
  28.       
  29.     accessFile.close();  
  30.     channel.close();  
  31. }  
  32.   
  33. /** 
  34.  * gather2 
  35.  * <br>------------------------------<br> 
  36.  * @param fileName 
  37.  * @throws IOException  
  38.  * @see FileChannel#write(java.nio.ByteBuffer[], int, int) 
  39.  */  
  40. private static void gather2(String fileName) throws IOException {  
  41.     RandomAccessFile accessFile = new RandomAccessFile(fileName, "rw");  
  42.     //获取文件通道  
  43.     FileChannel channel = accessFile.getChannel();  
  44.     //创建两个缓冲区  
  45.     ByteBuffer headBuffer = ByteBuffer.allocate(3);  
  46.     ByteBuffer bodyBuffer1 = ByteBuffer.allocate(4);  
  47.     ByteBuffer bodyBuffer2 = ByteBuffer.allocate(20);  
  48.     ByteBuffer bodyBuffer3 = ByteBuffer.allocate(20);  
  49.     ByteBuffer bodyBuffer4 = ByteBuffer.allocate(20);  
  50.       
  51.     headBuffer.put("abc".getBytes());  
  52.     bodyBuffer1.put("defg".getBytes());  
  53.     bodyBuffer2.put("bnbnbnb".getBytes());  
  54.     bodyBuffer3.put("zzz444".getBytes());  
  55.       
  56.     ByteBuffer[] allBuffers = new ByteBuffer[]{  
  57.             headBuffer,   
  58.             bodyBuffer1, bodyBuffer2,  
  59.             bodyBuffer3, bodyBuffer4,};  
  60.       
  61.     headBuffer.flip();  
  62.     bodyBuffer1.flip();  
  63.     bodyBuffer2.flip();  
  64.     bodyBuffer3.flip();  
  65.     bodyBuffer4.flip();  
  66.       
  67.     //将按allBuffers数组顺序使用两个缓冲区  
  68.     //0从哪开始  
  69.     //2使用几个  
  70.     //当前使用headBuffer  bodyBuffer1  
  71.     //最终写入abcdefg  
  72.     long n = channel.write(allBuffers, 02);  
  73.       
  74.     //应该返回7个字节  
  75.     System.out.println("共写入多少字节:" + n);  
  76.       
  77.     accessFile.close();  
  78.     channel.close();  
  79. }  
[java]  view plain copy print ?
  1. private static Charset charset = Charset.forName("GBK");  
  2.   
  3. public static void main(String[] args) throws IOException {  
  4.     final String fileName = "D:/test.log";  
  5.     /**----------Gather------------*/  
  6.     //FileChannel#write(java.nio.ByteBuffer[])  
  7.     gather(fileName);  
  8.       
  9.     //FileChannel#write(java.nio.ByteBuffer[], int, int)  
  10.     gather2(fileName);  
  11. }  

这篇关于NIO - Scatter/Gather的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952285

相关文章

BD错误集锦5——java.nio.file.FileSystemException 客户端没有所需的特权

问题:在运行storm本地模式程序时,java.nio.file.FileSystemException  客户端没有所需的特权   解决方式:以管理员身份运行IDEA即可。

java NIO 缓存区之内核空间、用户空间和虚拟地址

IO是基于缓存区来做的,所谓的输入和输出就是从缓存区中移入和移出数据。以IO输入为例,首先是用户空间进程向内核请求某个磁盘空间数据,然后内核将磁盘数据读取到内核空间的buffer中,然后用户空间的进程再将内核空间buffer中的数据读取到自身的buffer中,然后进程就可以访问使用这些数据。     内核空间是指操作系统内核运行的空间,是为了保证操作系统内核的能够安全稳定地运行而为内核专

Java NIO 创建/复制缓冲区

创建缓冲区的方式 主要有以下两种方式创建缓冲区: 1、调用allocate方法 2、调用wrap方法 我们将以charBuffer为例,阐述各个方法的含义; allocate方法创建缓冲区 调用allocate方法实际上会返回new HeapCharBuffer(capacity, capacity)对象; 缓存空间存储在CharBuffer类的成员属性char[] h

python函数scatter使用

最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下: 1、scatter函数原型 2、其中散点的形状参数marker如下: 3、其中颜色参数c如下: 4、基本的使用方法如下: [python] view plain copy #导入必要的模块  import numpy as np  import matp

springcloud 编译报错:java.nio.charset.MalformedInputException: Input length = 1

目录 一、问题描述二、解决方法 一、问题描述 springcloud 编译报错:java.nio.charset.MalformedInputException: Input length = 1 10:22:52.979 [main] ERROR org.springframework.boot.SpringApplication - Application run fa

【Java】已解决java.nio.channels.OverlappingFileLockException异常

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决java.nio.channels.OverlappingFileLockException异常 在Java的NIO(New I/O)编程中,java.nio.channels.OverlappingFileLockException是一个特定的异常,它发生在尝试获取与已存在文件锁

【Java】已解决java.nio.channels.ClosedChannelException异常

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决java.nio.channels.ClosedChannelException异常 在Java的NIO(New I/O)编程中,java.nio.channels.ClosedChannelException是一个常见的异常,通常表示试图在一个已经关闭的通道(Channel)上进

最新版ChatGPT对话系统源码 Chat Nio系统源码

最新版ChatGPT对话系统源码 Chat Nio系统源码 支持 Vision 模型, 同时支持 直接上传图片 和 输入图片直链或 Base64 图片 功能 (如 GPT-4 Vision Preview, Gemini Pro Vision 等模型) 支持 DALL-E 模型绘图 支持 Midjourney / Niji 模型的 Imagine / Upscale / Variant /

JAVA NIO(二) Buffer和Channel

一,基本使用 1, 一个Socket连接使用一个Channel来表示,以前直接操作Socket文件描述符来对读写缓冲区操作,比如读数据到用户空间的一个byte数组,NIO中Channel对这个过程作了封装,其中用户空间的byte数组就类比Buffer。 2,Buffer用于和Channel进行交互。 Channel中的数据总是要先写入到Buffer,或从Buffer读取;

Java中IO与NIO的区别

一、概念           NIO即New IO,这个库是在JDK1.4中才引入的。NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多。在Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。 二、NIO和IO的主要区别 下表总结了Java IO和NIO之间的主要区别: IONIO面向流面向缓冲阻塞