A buffer is a piece of data that is about to be written to a channel or has just been read from a channel. It is an object that holds data and acts as an endpoint of a NIO channel. The buffer provides a formal mechanism for data access and the reading and writing process. It is a major difference between NIO and the old Java I/O. Previously data was read and written directly from streams, now data can be read and written from buffers. In NIO, channel is a synonym for stream. To learn more about NIO channels, please read the previous tutorial Java NIO channels.
NIO buffer characteristics
Buffer Type Each primitive type has a corresponding buffer type. All buffer classes implement the Buffer interface. The most commonly used buffer type is ByteBuffer. The following are the buffer types provided in the Java NIO package.
Buffer capacity The buffer has a fixed size. We can only store data less than the "fixed size". The fixed size value is called the buffer capacity. Once the buffer is full, it must be emptied before writing again. Once the capacity is set, it will not change during the life cycle of the buffer. Buffer Limits In write mode, the limit of the buffer is equal to the capacity. In read mode, the limit points to the bit after the last data bit in the buffer. When the buffer is written, the limit is incremented. The limit of the buffer is always greater than or equal to zero and less than or equal to the capacity, 0 <= limit <= capacity. Buffer location The position points to the current address of the buffer. When the buffer is created, the position is set to zero. During reading and writing, the position is incremented to the next index position. The position is always between zero and the limit. Buffer Marking Marking is similar to setting a bookmark to a buffer. When mark() is called, the current position is recorded, and when reset() is called, the marked position is restored. Buffer flip, clear, and rewind Buffer flip() The flip() method is used to prepare the buffer for a get operation or to prepare a new write sequence. flip() sets the limit to the current position and then sets the position to 0. Buffer clear() The clear() method is used to prepare the buffer for a put operation or to prepare a new read sequence. clear() sets the limit to the capacity and sets the position to 0. Buffer rewind() The rewind() method is used to read the data that has been obtained again. rewind() sets the buffer position to 0. How to read NIO buffer
How to write to NIO buffer
The above are two examples of read and write buffers. There are many types of buffers and many ways to read and write them. You can choose according to your usage requirements. Original link: javapapers Translation: ImportNew.com - lemeilleur |
<<: The most powerful Android virus outbreak in history, effective on 15,000 models
>>: If you want to create the popular animation effects, you must understand some physics
According to Bloomberg, Alphabet's self-drivin...
[[132219]] Yuri Milner is such a low-key and toug...
According to the latest news, Apple's upcomin...
Source of this issue: Hanmu Diaomeng, how do plan...
According to a recent comprehensive foreign news ...
I have been wanting to talk about the seed user o...
Where there is demand, there is a market. During ...
Thousands of sea turtles die every year from bein...
With only one month left until the end of 2020, d...
1. SAIC Group announced that its sales of new ene...
Recently, Huawei released its 2024 annual report....
Apple App Store today officially launched a new c...
The formula for making money on the Internet is: ...
Introduction to the video resources of the new ge...
According to foreign media reports recently, Toyo...