This is the 25th day of my participation in Gwen Challenge

One, foreword

A Buffer can be used as a Buffer, or as a relay station, as shown in the following figure:

Disk data is passed through Buffer and then into memory.

Buffer: Reserving a specified amount of memory space for temporary storage of input/output (I/O) data. This part of memory space is called the Buffer.

A Buffer object is a container for a fixed amount of data.

Its attributes are:

  1. Capacity: The maximum number of data elements that a buffer can hold.
  2. Upper Limit: The first element of the buffer that cannot be read or written, used for counting.
  3. Position: The index of the next element to be read or written.
  4. Tag (Mark): a memo location; When callingreset()Method,positionWill come back tomarkPoint to.

In read/write mode, the value can be capacity, limit, or position




Second,APIuse

Basic steps for reading and writing data using Buffer:

  1. Write data toBuffer
  2. callflip()methods
  3. fromBufferRead data from
  4. callclear()Methods orcompact()methods
ByteBuffer buf = ByteBuffer.allocate(1024);

// Read from channel
int bytesRead = inChannel.read(buf);

while(bytesRead ! = -1) {
    buf.flip();
    
    while(buf.hasRemaining()) {
        System.out.println((char) buf.get());
    }
    
    buf.clear();
    bytesRead = inChannel.read(buf);
}
Copy the code

(1)BufferAssign, read, write

DirectBuffer is different from buffer, as shown in the figure below:

// Read data from disk file/network
ByteBuffer buf = ByteBuffer.allocate(1024);
ByteBuffer directBuf = ByteBuffer.allocateDirect(1024);
// If the direct mode is used, the overall performance is higher than that of normal mode.
Copy the code

The following demo shows how to allocate, read, and write buffer:

// 1. Allocate Buffer
ByteBuffer buffer = ByteBuffer.allocateDirect(100);

System.out.println("capacity : " + buffer.capacity());
System.out.println("position : " + buffer.position());
System.out.println("limit : " + buffer.limit());

byte[] src = new byte[] {1.2.3.4.5};

// 2. Place data, move position
buffer.put(src);

// position = 0 to 4, both filled with data
System.out.println("position : " + buffer.position());

// Position = 5 if you read the data directly, you cannot read the data.
Position = 0 and start reading data
buffer.position(0);

byte[] dst = new byte[5];

// 3. Write the data to the target array
buffer.get(dst);
System.out.println("position : " + buffer.position());
Copy the code

The output is as follows:

capacity : 100
position : 0
limit: 100 position: 5 position: 5 DST =[0,1,2,3,4]Copy the code

(2) usually calledBuffer APIOperation buffer

  1. clear()methods

The Buffer is cleared. The Buffer is not cleared, but the flags tell us where to start writing to the Buffer.

Position will be set back to 0 and limit will be set to the value of Capacity.

  1. flip()methods

Switch Buffer from write mode to read mode

Set position to 0 and limit to the previous position.

  1. rewind()methods

Set position to 0 and discard mark.

If you read the data once and then want to read it again, you can use rewind and the limit stays the same.