Buffer:
A container for a specific base data type. Defined by the Java.nio package, all buffers are subclasses of the buffer abstract class.
The buffer in Java NIO is primarily used to interact with the NIO channel, where the data is read from the channel into the buffer and written from the buffer to the channel.
Buffer is like an array that can hold multiple data of the same type. Depending on the data type (except for the Boolean), there are the following Buffer common subclasses:
Bytebuffer
Charbuffer
Shortbuffer
Intbuffer
Longbuffer
Floatbuffer
DoubleBuffer
These Buffer classes use similar methods for managing data, except that they manage different types of data. is to obtain a Buffer using the following method
Object:
static Xxxbuffer allocate (int capacity): Creates a Xxxbuffer object with a capacity of capacity
Two core methods for buffer Access data:
Put (): Deposit data into buffer
Get (): Gets the data in the buffer
Four core properties in the buffer:
Capacity: capacity, which represents the maximum amount of data stored in the buffer. Once the declaration cannot be changed.
Limit: Bounds that indicates the size of the data that can be manipulated in the buffer. (data cannot be read or written after limit)
Position: position that represents the position in the buffer where the data is being manipulated.
Mark: Mark, indicating the location of the current position. Can be restored to mark's location via Reset ()
0 <= Mark <= position <= limit <= capacity
Direct buffers vs. non-direct buffers:
Non-direct buffers: allocates buffers through the allocate () method, setting buffers in the JVM's memory
Direct buffer: Allocates a direct buffer through the Allocatedirect () method, establishing the buffer in physical memory. can improve efficiency
Java NIO--data access in buffer