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
Large scientific facilities refer to major nation...
In the past two years, with the development of va...
Shrimp pre-cooking is troublesome, so fully proce...
Google held a press conference today to announce ...
Recently, a #woman in Huizhou, Guangdong used a m...
I believe that friends who have placed informatio...
According to a CNNIC report, the size of China...
Driven by multiple forces including users, platfo...
How should beauty short video KOL marketing be do...
Beijing Meteorological Observatory issued a blue ...
It has been more than three years since the launc...
Following nanomaterials, quantum materials have b...
In an information flow advertisement , the role o...
On July 24, the Wentian Experimental Module was s...
Netizens found blue-ringed octopus in hot pot res...