NIO - Scatter/Gather
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外面一般的冰淇淋车要多少钱?
- 下一篇: 什么个性签名吸引男人