Vector io=scatter/gather:Implement a simple IO operation on multiple buffers. Reduced or avoided buffer copy and system call (IO) Write:gather data is extracted from several buffer sequences and sent along the channel, as if all buffers are connected together into a large buffer for sending, and the buffer itself does not have gather capability. Read:scatter data that is read from the channel is distributed sequentially to multiple buffers until the buffer is filled up or the channel data is read out.
Gather:scatter: Sample code:
1 /**2 * Channel Gather/scatter3 */4 Public Static voidChannelgatherscatter () {5Bytebuffer head = bytebuffer.allocate (4);6Bytebuffer BODY = bytebuffer.allocate (100);7Randomaccessfile Afile =NULL;8Randomaccessfile bfile =NULL;9Readwritelock Readwritelock =NewReentrantreadwritelock ();Ten Try { OneAfile =NewRandomaccessfile ("Hello.txt", "R"); Abfile =NewRandomaccessfile ("Hehe.txt", "RW"); - Readwritelock.readlock (). Lock (); -FileChannel FileChannel =Afile.getchannel (); thebytebuffer[] buffers ={head, body}; - while(Filechannel.read (buffers)! =-1){ - } - Head.flip (); + Body.flip (); -System.out.println (NewString (Head.array ())); +System.out.println (NewString (Body.array ())); A readwritelock.readlock (). Unlock (); at filechannel.close (); - afile.close (); - - Readwritelock.writelock (). Lock (); -FileChannel Bfilechannel =Bfile.getchannel (); - in while(Bfilechannel.write (buffers) > 0){ - } to + readwritelock.writelock (). Unlock (); - bfilechannel.close (); the bfile.close (); *}Catch(Exception e) { $ e.printstacktrace ();Panax Notoginseng } -}overloaded with offset, length parameter the read Write method, indicating how many buffer to use, starting with that buffer.
JAVA NIO scatter/gather (vector io)