NIO three core components ---- Buffer (buffer)

Buffer:

The buffer is essentially a block of memory can read and write data, will be understood to be a container object (including arrays), the object provides a set of methods that can more easily use the memory block, a number of mechanisms built buffer object, to track and record the state transition of the buffer zone, channel provides read data from a file, network channels,But the reading or writing of data must be made through Buffer

Buffer classes and subclasses

1.Buffer is a top-level parent class, it is an abstract class, there are seven sub-categories, in addition to boolean type, other basic data types are subclasses of buffer, the most common is ByteBuffer
2.buffer four attributes to provide information on it contains information data elements
capacity : capacity, when the buffer is created and set immutable
limit : indicates the end of the current buffer
position : the position of the next read or write to the index
mark : mark

Buffer type commonly used methods

Here Insert Picture Description
The most commonly used method bytebuffer: The
Here Insert Picture Description
following list is read into a Buffer examples:

package com.jym.nio;

import java.nio.IntBuffer;

/**
 * @program: JymNetty
 * @description: 举例说明Buffer的使用
 * @author: jym
 * @create: 2020/01/31
 */
public class JymBuffer {
    public static void main(String[] args) {
        // 创建一个Buffer,大小为5,即可以存放5个int
        IntBuffer intBuffer = IntBuffer.allocate(5);
        // 向Buffer存放数据, intBuffer.capacity()为buffer的容量,put方法为存放
        for(int i = 0; i<intBuffer.capacity();i++){
            intBuffer.put(i+10);
        }
        // 如何取出数据
        // 将buffer转换,读写切换,intBuffer.hasRemaining() 判断buffer里是否有数据
        intBuffer.flip();
        while (intBuffer.hasRemaining()){
            System.out.println(intBuffer.get());
        }
    }
}

It should be noted that: when reading and writing, and must call switching method flip ()

Each time you add or read the time, position 1 will increase, up until the limit

Buffer Notes:

1.ByteBuffer support type of put and get, put into what type, get should be used to remove the corresponding data type, or it may have abnormal BufferUnderflowException
2 can be converted into an ordinary Buffer Buffer read-only, if only to reading Buffer add elements, will produceReadOnlyBufferException
Code Description:

package com.jym.nio;

import java.nio.ByteBuffer;

/**
 * @program: JymNetty
 * @description: Buffer注意事项
 * @author: jym
 * @create: 2020/02/01
 */
public class JymFileChannle4 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        // 读取的时候也一定要按照顺序读取,比如要指定类型,否则可能会出错
        byteBuffer.putChar('A');
        byteBuffer.putInt(3);
        byteBuffer.putShort((short) 4);
        byteBuffer.putLong(10086);


        // 转换成只读Buffer
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        // 添加元素会报错ReadOnlyBufferException
        readOnlyBuffer.put((byte) 1);
    }
}

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104138593