`

NIO - Scatter/Gather

 
阅读更多

转自: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

  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. }  
  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

  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. }  
  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);  

分享到:
评论

相关推荐

    Ni-Zn/NiO-ZnO复合电极制备及其电化学电容性能研究

    Ni-Zn/NiO-ZnO复合电极制备及其电化学电容性能研究 ,金丹春,全敏慧,采用电化学沉积的方法得到Ni-Zn合金,再通过恒电流氧化法制备得到Ni-Zn/NiO-ZnO复合电极材料。用X射线衍射(XRD)和扫描电子显微镜(SEM)对制�

    水热沉淀法制备NiO-CaO/Al2O3复合催化剂及其在ReSER制氢中的应用 (2014年)

    制备了一组高催化活性的NiO-CaO/Al2O3复合催化剂并将其应用于反应吸附强化甲烷水蒸气重整制氢(ReSER)过程中。采用水热沉淀法制备了具有层状复合金属氢氧化物结构(NiAl-LDHs)的复合催化剂前驱体,高温焙烧后得到...

    httpcore-nio-4.4.6-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.6.jar 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar 赠送源代码:httpcore-nio-4.4.6-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.6-javadoc-API文档-中文(简体)版.zip ...

    xnio-nio-3.8.0.Final-API文档-中文版.zip

    赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...

    httpcore-nio-4.4.15-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)版....

    httpcore-nio-4.4.10-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.10-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.15-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)-...

    xnio-nio-3.8.4.Final-API文档-中英对照版.zip

    赠送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...

    xnio-nio-3.8.0.Final-API文档-中英对照版.zip

    赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...

    httpcore-nio-4.4.5-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.5.jar; 赠送原API文档:httpcore-nio-4.4.5-javadoc.jar; 赠送源代码:httpcore-nio-4.4.5-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.5.pom; 包含翻译后的API文档:...

    nio-multipart-parser-1.1.0.jar

    java运行依赖jar包

    httpcore-nio-4.4.14-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.14.jar; 赠送原API文档:httpcore-nio-4.4.14-javadoc.jar; 赠送源代码:httpcore-nio-4.4.14-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.14.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.6-API文档-中英对照版.zip

    赠送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文档:...

    httpcore-nio-4.4.12-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.12.jar; 赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档...

    xnio-nio-3.8.4.Final-API文档-中文版.zip

    赠送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...

    httpcore-nio-4.3.jar包

    用Java实现非阻塞通信 ,用ServerSocket和Socket来编写服务器程序和客户程序,是Java网络编程的最基本的方式。 httpcore-nio-4.3.jar包

    httpcore-nio-4.4.4-API文档-中文版.zip

    赠送jar包:httpcore-nio-4.4.4.jar; 赠送原API文档:httpcore-nio-4.4.4-javadoc.jar; 赠送源代码:httpcore-nio-4.4.4-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.4.pom; 包含翻译后的API文档:...

    httpcore-nio-4.4.12-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.12.jar; 赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档...

    httpcore-nio-4.4.4-API文档-中英对照版.zip

    赠送jar包:httpcore-nio-4.4.4.jar; 赠送原API文档:httpcore-nio-4.4.4-javadoc.jar; 赠送源代码:httpcore-nio-4.4.4-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.4.pom; 包含翻译后的API文档:...

Global site tag (gtag.js) - Google Analytics