Java nio Scatter/Gather (vector IO), nioscatter

Source: Internet
Author: User

Java nio Scatter/Gather (vector IO), nioscatter
Vector IO = Scatter/Gather:Implements a simple IO operation on multiple buffers. Reduce or avoid the buffer copy and System Call (IO) write: Gather data is extracted from several buffers sequentially and sent along the channel, just like all the buffers are connected and put into a large buffer for sending, the buffer itself does not have the gather capability. Read: The data read by Scatter from the channel is distributed to multiple buffers sequentially until the buffer is filled or the channel data is read.

 

Gather: Scatter: Sample Code:
 1 /** 2      * channel Gather/Scatter 3      */ 4     public static void channelGatherScatter(){ 5         ByteBuffer head = ByteBuffer.allocate(4); 6         ByteBuffer body = ByteBuffer.allocate(100); 7         RandomAccessFile afile = null; 8         RandomAccessFile bfile = null; 9         ReadWriteLock readWriteLock = new ReentrantReadWriteLock();10         try {11             afile = new RandomAccessFile("hello.txt", "r");12             bfile = new RandomAccessFile("hehe.txt", "rw");13             readWriteLock.readLock().lock();14             FileChannel fileChannel = afile.getChannel();15             ByteBuffer[] buffers = {head, body};16             while (fileChannel.read(buffers) != -1){17             }18             head.flip();19             body.flip();20             System.out.println(new String(head.array()));21             System.out.println(new String(body.array()));22             readWriteLock.readLock().unlock();23             fileChannel.close();24             afile.close();25 26             readWriteLock.writeLock().lock();27             FileChannel bfileChannel = bfile.getChannel();28 29             while (bfileChannel.write(buffers) > 0){30             }31 32             readWriteLock.writeLock().unlock();33             bfileChannel.close();34             bfile.close();35         }catch (Exception e){36             e.printStackTrace();37         }38     }
  The read write method with the offset and length parameters is overloaded to specify the number of buffers used starting from the buffer.

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.