JAVA NIO Scatter/Gather(矢量IO)
2016-10-25 12:06
363 查看
矢量IO=Scatter/Gather:
在多个缓冲区上实现一个简单的IO操作。减少或避免了缓冲区拷贝和系统调用(IO)
write:Gather
数据从几个缓冲区顺序抽取并沿着通道发送,就好比全部缓冲区全部连接起来放入一个大的缓冲区进行发送,缓冲区本身不具备gather能力。
read:Scatter
从通道读取的数据会按顺序散布到多个缓冲区,直到缓冲区被填满或者通道数据读完。
Gather:
Scatter:
示例代码:
带offset、length参数重载read write方法,指明从那个buffer开始,共使用多少个buffer。
在多个缓冲区上实现一个简单的IO操作。减少或避免了缓冲区拷贝和系统调用(IO)
write:Gather
数据从几个缓冲区顺序抽取并沿着通道发送,就好比全部缓冲区全部连接起来放入一个大的缓冲区进行发送,缓冲区本身不具备gather能力。
read:Scatter
从通道读取的数据会按顺序散布到多个缓冲区,直到缓冲区被填满或者通道数据读完。
Gather:
Scatter:
示例代码:
/** * channel Gather/Scatter */ public static void channelGatherScatter(){ ByteBuffer head = ByteBuffer.allocate(4); ByteBuffer body = ByteBuffer.allocate(100); RandomAccessFile afile = null; RandomAccessFile bfile = null; ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); try { afile = new RandomAccessFile("hello.txt", "r"); bfile = new RandomAccessFile("hehe.txt", "rw"); readWriteLock.readLock().lock(); FileChannel fileChannel = afile.getChannel(); ByteBuffer[] buffers = {head, body}; while (fileChannel.read(buffers) != -1){ } head.flip(); body.flip(); System.out.println(new String(head.array())); System.out.println(new String(body.array())); readWriteLock.readLock().unlock(); fileChannel.close(); afile.close(); readWriteLock.writeLock().lock(); FileChannel bfileChannel = bfile.getChannel(); while (bfileChannel.write(buffers) > 0){ } readWriteLock.writeLock().unlock(); bfileChannel.close(); bfile.close(); }catch (Exception e){ e.printStackTrace(); } }
带offset、length参数重载read write方法,指明从那个buffer开始,共使用多少个buffer。
相关文章推荐
- JAVA NIO Scatter/Gather(矢量IO)
- Java NIO Scatter / Gather
- Java基础知识强化之IO流笔记75:NIO之 Scatter / Gather
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather
- 【JAVA】【NIO】5、Java NIO Scatter / Gather
- 05. Java NIO Scatter / Gather
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather
- Java NIO (三)-分散(Scatter)/聚集(Gather)
- Java-NIO(六):Channel聚集(gather)写入与分散(scatter)读取
- Java NIO Scatter/Gather
- java的nio之:java的nio系列教程之Scatter/Gather
- Java NIO Scatter / Gather
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO Scatter / Gather
- Java NIO 之 Scatter 和gather
- Java-NIO(六):Channel聚集(gather)写入与分散(scatter)读取
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather