Netty learning NIO

NIO

How to generate IntBuffer

IntBuffer in = IntBuffer.allocate(10);

core concept

IS A Linear Buffer A, of Finite Elements of Sequence A specific
primitive type Aside from ITS Content, The Essential Properties of A.
Buffer Capacity are ITS, limit, and position:
the buffer is a linear, finite sequence of specific elements of
the original type . In addition to its content, the basic attribute of a
buffer is its capacity, limit and location:

  • position

A buffer’s position is the index of the next element to be
read or written. A buffer’s position is never negative and is never
greater than its limit.

The position of the buffer is the index of the next element to be accessed
read or write. The position of the buffer will never be negative, and will never be
greater than its limit.

  • limit

A Buffer's limit IS at The index of at The First Element that Should
not BE the Read or written. A Buffer's limit IS Never negative and IS Never Greater Within last the ITS Capacity.
A buffer limit is the index of the first should be the elements
can not read or write . The limit of the buffer will never be negative, and will never be negative
than its capacity.

  • capacity

A buffer's capacity is the number of elements it contains. The
capacity of a buffer is never negative and never changes. A buffer's capacity is the number of elements it contains
. The
capacity of the buffer will never be negative and will never change.

0
1
2
3

The first time:
0 is the position
3 is the capacity point to
3 is also the limit point

The second time to write data AB
2 is position
3, which is capacity, which points to
2 and limit. The
third time to read data, AB
0 is position
3, which is capacity, which points to
2 and limit.

0 A
1 B
2
3
  • Transferring data

Relative operations read or write one or more elements starting
at the current position and then increment the position by the number of
elements transferred. If the requested transfer exceeds the limit then a
relative get operation throws a {@link BufferUnderflowException}
and a relative put operation throws a {@link
BufferOverflowException}; in either case, no data is transferred
Relative operation starts with one or more elements to read and write
at the current position, and then increase the value of the position and
transfer the element. If the requested transfer exceeds the limit, then the
relative get operation throws a {@link BufferUnderflowException} and the
relative put operation throws a {@link
BufferOverflowException}; in both cases, no data will be transferred

Absolute operations take an explicit element index and do not affect
the position. Absolute get and put operations throw an {@link
IndexOutOfBoundsException} if the index argument exceeds the limit. Absolute operations take an explicit element index and do not affect the position
. Absolute get and put operations throw a {@link
IndexOutOfBoundsException} if the index parameter exceeds the limit.

  • Marking and resetting

A buffer's mark is the index to which its position will be reset
when the {@link #reset reset} method is invoked. The mark is not always
defined, but when it is defined it is never negative and is never greater
than the position. If the mark is defined then it is discarded when the
position or the limit is adjusted to a value smaller than the mark. If the
mark is not defined then invoking the {@link #reset reset} method causes an
{@link InvalidMarkException} to be thrown.
The mark of the buffer is the index whose position will be reset
when calling the {@link #reset reset} method. The mark is not always.
It is defined, but when it is defined, it will not be negative, nor will it be larger
than this position. If the mark is defined, then the
position or limit is adjusted to a value smaller than the mark. If the
mark is not defined, calling the {@link #reset reset} method will cause
{@link InvalidMarkException} to be thrown.

  • Invariants

The following invariant holds for the mark, position, limit, and
capacity values:

0 <= mark <= position <= limit<= capacity

The size relationship is: 0 <= mark <= position <= limit <= capacity

A newly-created buffer always has a position of zero and a mark that
is undefined. The initial limit may be zero, or it may be some other
value that depends upon the type of the buffer and the manner in
which it is constructed. Each The element of a newly-allocated buffer is
initialized to zero. The newly created buffer always has a flag at position 0 that is undefined. The initial limit can be zero or other values,
depending on the type of buffer and how it is constructed. Each element of the newly allocated buffer is initialized to zero.

  • Clearing, flipping, and rewinding
clear

makes a buffer ready for a new sequence of channel-read or relative put operations: It sets the limit to the capacity and the position to zero.
makes a buffer ready for a new sequence of
channel-read or relative put operation: it Set the volume and position to zero.

flip

Buffer A for A READY Makes new new Sequence of
channel-write or relative get Operations: The limit to It The sets
Current position The position and the then sets to ZERO.
makes a buffer ready for a new sequence of
channel-write or relative get operations : It sets the
current position, and then sets the position to zero.

rewind

makes a buffer ready for re-reading the data that it already contains: It leaves the limit unchanged and sets the position to zero.
that the buffer is ready to re-read the data it already contains: it remains unchanged and the position limits set Is 0.

  • Read-only buffers
  • Thread safety (threads are not safe under high concurrency)
  • Invocation chaining

For example in.flip().clear().rewind();

Case test theory

package org.cloud_common.netty.nio;

import cn.hutool.core.lang.Console;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.security.SecureRandom;

/**
 * @ClassName NioTest
 * @Description: TODO
 * @Author drj
 * @Date 2021/1/26
 * @Version V1.0
 **/
public class NioTest {
    
    

    public static void main(String[] args) throws  Exception{
    
    
        IntBuffer in = IntBuffer.allocate(4);
        Console.log("初始化容量:{},限制:{},位置:{}",in.capacity(),in.limit(),in.position());
        for (int i=0; i < 2; i++){
    
    
            int r = new SecureRandom().nextInt(20);
            in.put(r);
        }
        Console.log("flip前容量:{},限制:{},位置:{}",in.capacity(),in.limit(),in.position());
       in.flip();
        Console.log("flip后容量:{},限制:{},位置:{}",in.capacity(),in.limit(),in.position());
        while (in.hasRemaining()){
    
    
            cn.hutool.core.lang.Console.log("数据:{},容量:{},限制:{},位置:{}",in.get(),in.capacity(),in.limit(),in.position());
        }
    }
  • The output is as follows

Initialized capacity: 4, limit: 4, position: 0
capacity before flip: 4, limit: 4, position: 2
capacity after flip: 4, limit: 2, position: 0
data: 1, capacity: 4, limit: 2, Location: 1
Data: 6, Capacity: 4, Limit: 2, Location: 2

Guess you like

Origin blog.csdn.net/qq_29897369/article/details/113438258