Nio Scatter/Gather
参考资料:
Reille_java_NIO
http://hg.openjdk.java.net/jdk7/tl/jdk/rev/11ee8b471f9c OpenJDK improve Scatter/Gather implementation
http://www.javaperformancetuning.com/tips/nio.shtml Java Performance Tuning
Nio Scatter/Gather
Scatter/Gather模式能够将从一个Channel读取的信息分散到N个Buufer里面,[Scatter]. 同时也能够把N各Buffer里面的东西按照顺序写到一个Channel中. 尤其是在读取有一定格式的消息的时候,把消息头和消息体分别放到相应的Buffer里面进行解析是相当有用的.
下面给出一个Channel类结构的一部分:
上面的类结构中,ReadableByteChannel 负责读取而WriteByteBufferChannel用于写操作. 其中底层的ScatteringByteChannel和GatheringByteChannel是负责Scatter/Gather模式的接口. 实现了这两个接口就能够相应的实现Scatter/Gather模式. 从接口的结构上来看, ScatteringByteChannel是继承了ReadableByteChannel的,也就是负责"读",而GathringByteChannel是继承了WritableByteChannel的接口,负责"写". 经常使用文件读取的Channel是FIleChannel, 它继承了上述接口中的所有接口,可读/可写, 同时也实现了Scatter/Gather模式.
使用示例代码:
RandomAccessFile file = new RandomAccessFile("testFile.txt","rw");
FileChannel channel = file.getChannel();
Scatter:
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = { header, body };
channel.read(buffers);
Gather:
ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
//write data into buffers
ByteBuffer[] bufferArray = { header, body };
channel.write(buffers);
[友情提示: NIO是使用块的方式来处理文件,并不是流的方式. 如果要考取并行操作大文件的时候, 可以使用"生产者-消费者"的模型来对一个byteBuffer进行操作,而后考虑使用Gather的方式来进行数据的输出或者读取分析.当然,如果内存允许的情况下,是可以使用NIO 中的Memory-Mapped Files的方式进行文件操作的. 当然,也可以先映射一部分. 详细请参考map(...)方法. ]
to be continnue..
2021年9月24日 20:27
Maids will allow you to to handle these tasks. You will hire them for just this move-in types of service if that could be on-line. On and the second hand, you will hire them to go back after you will have moved in for long-term, regular care of the house. As a good busy pro, you wouldn't have the a chance to put into this action exclusively by yourself, but you could still get hold of help to your cleanup alot more quickly than you feel.
2023年12月06日 22:36
You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site