A, java programming foundation of nio

First, what is nio

1.NIO is a possible alternative to the standard Java IO API of IO API (starting from Java 1.4), NIO provides different standard IO IO work, NIO: Channels and Buffers (channel and buffer)

Two, nio contrasts with io

I

NIO

Stream-oriented

Buffer-oriented

Blocking IO

Non- blocking IO

no

Selector

 

Three, buffer data access

The 1.java NIO Buffer primarily for interacting with NIO channel, the data is read from the channel into the buffer, the buffer in the write channel, like a Buffer array can store a plurality of the same type of data. Depending on the type (boolean excluded), the following subclasses Buffer used:

ByteBuffer

CharBuffer

ShortBuffer

IntBuffer

LongBuffer

FloatBuffer

DoubleBuffer

2.buffer Overview

1) Capacity (capacity): Buffer indicates the maximum data capacity, buffer capacity can not be negative, and can not be established and modified.

2) limit (limit): a first index should not be read or written data, i.e., data can not be located after the write limit. Restrict the buffer can not be negative, and can not be greater than its capacity (capacity).

3) the location (position): The next read or write to the index data. Buffer location can not be negative, and can not be greater than its limit (limit).

4) marks (Mark) and reset (reset): it is a marker index () method to specify a specific position in the Buffer Buffer by the Mark, then can be restored by calling the reset () method to this position.

3. Code

/**
 * (缓冲区)buffer 用于NIO存储数据 支持多种不同的数据类型 <br>
 * 1.byteBuffer <br>
 * 2.charBuffer <br>
 * 3.shortBuffer<br>
 * 4.IntBuffer<br>
 * 5.LongBuffer<br>
 * 6.FloatBuffer <br>
 * 7.DubooBuffer <br>
 * 上述缓冲区管理的方式 几乎<br>
 * 通过allocate() 获取缓冲区 <br>
 * 二、缓冲区核心的方法 put 存入数据到缓冲区 get <br> 获取缓冲区数据 flip 开启读模式
 * 三、缓冲区四个核心属性<br>
 * capacity:缓冲区最大容量,一旦声明不能改变。 limit:界面(缓冲区可以操作的数据大小) limit后面的数据不能读写。
 * position:缓冲区正在操作的位置
 */
public class test001 {
    public static void main(String[] args) {
        //1.指定缓冲区的大小1024
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        System.out.println("-------------------");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        // 2.向缓冲区存放5个数据
        byteBuffer.put("abcd1".getBytes());
        System.out.println("--------------------");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        // 3.开启读模式
        byteBuffer.flip();
        System.out.println("----------开启读模式...----------");
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        byte[] bytes = new byte[byteBuffer.limit()];
        byteBuffer.get(bytes);
        System.out.println(new String(bytes, 0, bytes.length));
        System.out.println("----------重复读模式...----------");
        byteBuffer.rewind();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        byte[] bytes2 = new byte[byteBuffer.limit()];
        byteBuffer.get(bytes2);
        System.out.println(new String(bytes2, 0, bytes2.length));
        // 5.clean 清空缓冲区  数据依然存在,只不过数据被遗忘
        System.out.println("----------清空缓冲区...----------");
        byteBuffer.clear();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println((char)byteBuffer.get());
    }
}

4. Results

-------------------
0
1024
1024
--------------------
5
1024
1024
----------开启读模式...----------
0
5
1024
abcd1
----------重复读模式...----------
0
5
1024
abcd1
----------清空缓冲区...----------
0
1024
1024
a

5. Analysis

5.1. This step is actually when we first started this buffer array initialization time, this is the default start

 5.2. But when you go buffer array to begin writing a few bytes when it will become the following chart, position will move to the next position of the end of your data.

5. 3. This time you need to write the channel data buffer in the pipeline, so this time we need to use this byteBuffer.flip () method when you finish call this method, this time will become the following the map, so you can actually know that you have just written to the data buffer is in position ----> between limit (repeatable read result is the same).

5.4. In this case the underlying operating system can read these five bytes of data sent out from the buffer correctly. Before we write data in the next adjustment to the clear () method. The index buffer state has returned to its initial position.

Four, make use of the rest of

1. mark (Mark) and reset (reset): is a marker index () method to specify a specific position in the Buffer Buffer by the Mark, then can be restored by calling the reset () method to this position.

2. Code

public class Test002 {
    public static void main(String[] args) {
        ByteBuffer buf = ByteBuffer.allocate(1024);
        String str = "abcd1";
        buf.put(str.getBytes());
        // 开启读取模式
        buf.flip();
        byte[] dst = new byte[buf.limit()];
        buf.get(dst, 0, 2);
        buf.mark();
        System.out.println(new String(dst, 0, 2));
        System.out.println(buf.position());
        System.out.println("---------------------------");
        buf.get(dst, 2, 2);
        System.out.println(new String(dst, 2, 2));
        System.out.println(buf.position());
        buf.reset();
        System.out.println("重置恢复到mark位置..");
        System.out.println(buf.position());
    }
}

3. Results

ab
2
---------------------------
cd
4
重置恢复到mark位置..
2

Fifth, the end of the

1. continue next programming java nio explain the basis of the difference between security and direct and non-direct buffer buffer. Always keep the faith !!!

Published 122 original articles · won praise 64 · views 50000 +

Guess you like

Origin blog.csdn.net/chenmingxu438521/article/details/103967066