Java NiO Study Notes-bytebuffer (previous notes)

Source: Internet
Author: User

(Refer to "getting started with IBM NiO ")

In the NIO database, all data is processed using a buffer. When reading data, it is directly read into the buffer. When writing data, it is written to the buffer. When you access the data in NIO, it is put in the buffer zone. The buffer is essentially an array. It is usually a byte array, but other types of arrays can also be used. But a buffer does notOnlyIs an array. The buffer zone provides structured access to data, and supports tracking system read/write processes.

Buffer is actually a beautifying array.

Status variable

Tracks the status of data so that the buffer can manage data resources by itself

Position: Actually refers to the location where the next element of the buffer is read or written from the buffer. For example, if three buffer elements have been written, the position point is to the 4th position, that is, the position is set to 3 (the array starts from 0 ). Limit: How much data needs to be retrieved from the buffer, or how much space can be put into it. Postition always <= limit. Capacity: The capacity of the buffer's underlying array. Limit must not> capacity. Filp (): two things are done:1. Point the limit to the current position. 2. Set the position to 0 (Limit = position; position = 0)This process changes the status of data written by the buffer to "ready to read ". Because the data previously written to the buffer is between the position and the limit-1 (the limit points to the last position of the last data ). Clear ():Two things are also done: 1. The limit = capacity 2. Position = 0 process can change the status changed when the buffer reads data to "empty and prepare to write ". The following uses bytebuffer as an example to get (): the first three get methods are relatively read. It reads data relative to the location status and changes the position status. Byte get (); bytebuffer get (byte DST []); // read data written to bytebuffer into DST [] bytebuffer get (byte DST [], int offset, int length ); the read data is absolutely read (a byte), and the limit and position values are ignored. The buffer State statistics method is completely bypassed. That is to say, it will not change the position status inside the buffer. Byte get (INT index); Put (); similar to get, the first four put methods are relatively read. It is affected by position and limit, and changes position. Bytebuffer put (byte B); bytebuffer put (byte SRC []); // write bytebuffer put (byte SRC [], int offset, int length) from SRC ); bytebuffer put (bytebuffer SRC); absolute write will not affect the position status. Bytebuffer put (INT index, byte B); in addition to reading and writing bytes, there are other types of reading and writing methods. And they all exist. Relative and absolute. Typical operations: While (true) {<br/> buffer. clear (); // prepare to write data to the buffer <br/> int r = Fcin. read (buffer); // The channel reads data from the external system and writes the data to the buffer. <br/> If (r =-1) {<br/> break; <br/>}< br/> buffer. flip (); // prepare to read data into the buffer <br/> fcout. write (buffer); // The channel reads the buffer data and writes it to the corresponding external system. <br/>}The bytebuffer. Allocate (INT) method can be used to allocate (create) a byte buffer. Bytebuffer. Wrap (byte []); The method can wrap an existing byte array into a new bytebuffer object. The latter method requires careful processing of the original byte array. Because it can be accessed directly. The sharding of the buffer zone is to create a "sub-buffer ". The sub-buffer shares part of the underlying array location of the parent buffer. In a sense, the sub-buffer is like a window in the original buffer. In this way, when the content of the sub-buffer is changed, the corresponding location of the parent buffer is also changed. The split operation is determined based on the current position and the value of limit. Buffer. position (3); buffer. limit (7); bytebuffer slice = buffer. slice (); the read-only buffer asreadonlybuffer () method can return an object the same as the original buffer object, but the new buffer object is read-only. Direct BufferSun's definition: given a direct byte buffer, the Java Virtual Machine will do its best to directly perform local I/O operations on it. That is to say, it will be before (or after) each time it calls the local I/O operations of the underlying operating system ), try to avoid copying the buffer content to an intermediate buffer (or copying data from an intermediate buffer ). The directbuffer creation method is to use Bytebuffer. allocatedirect (INT); Method to replace bytebuffer. Allocate (INT); Memory shot file I/O it reads and writes much faster than other IO. It causes a part of the file or file to be shot by memory. However, only the data operated on this part of the location can be read and written in memory, rather than reading the entire file into the memory. (And it is the underlying mechanism of an OS. Synchronization of memory and data on physical disks is completed asynchronously at the underlying operating system level.) The map ing file can be obtained through the map method of the filechannel object. For example, the following method shadows the first 1024 bytes of a file into the memory and creates a mappedbytebuffer object to return it. Mappedbytebuffer is a subclass of bytebuffer. Mappedbytebuffer MBB = FC. Map (filechannel. mapmode. read_write, start, size );

 

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.