JAVA NIO scatter/gather (vector io)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.