Core components of Java NiO:
Buffer
Channel
Selector
Buffer
is an array, but has an internal state. The following 4 indexes:
- Capacity: Total Capacity
- Position: The next element index to read/write
- Limit: The first index of an element that cannot be read/written
- Mark: Position tag, reset position
- By calling the Buffer.mark () method, you can mark a specific position in buffer. You can then revert to this position//by calling the Buffer.reset () method
- 0 <= Mark <= position <= limit <= capacity
Case:
Initial state:
Initially, adding data: Theput () method changes the value of the position, but the put (int,object) does not change
Buffer.put ((byte) H). Put ((byte) e). Put ((byte) l). Put ((bytes). ) o);
On the basis of the Flip() operation, the following state is entered:
Flip: Prepares the buffer for the data out state, i.e. limit=position,position=0
On the basis of the get operation, position will move back and know Position=limit, such as:
On the basis of the operation of Rewind (), position is 0,limit unchanged, for example, if you need to read buffer data multiple times, you can use rewind () between two reads.
Assume a new state such as:
Compact () operation in a new state, enter the following state
Clear () operation in the new state, return to the initial state, i.e. position=0,limit=capacity
Type of buffer:
- Bytebuffer
- Mappedbytebuffer
- Charbuffer
- DoubleBuffer
- Floatbuffer
- Intbuffer
- Longbuffer
- Shortbuffer
Allocation of buffer: Factory method
1, allocate
//申请48字节
ByteBuffer buf = ByteBuffer.allocate(48);
//申请1024字符
CharBuffer buf = CharBuffer.allocate(1024);
2. Wrap, wrap an existing array
char [] myArray = new char [100];
Charbuffer Charbuffer = Charbuffer.wrap (MyArray);
Note that the buffer created in this way will not create a new array on the heap, but instead directly use MyArray to do the backing store, which means that any modification to myarray or buffer will affect either buffer or myarray.
3. Copy buffer:"Shallow copy"
- A) The duplicate () method will return a newly created buffer, which shares data with the original buffer, as capacity, but has its own position, limit, and Mark properties.
- b) The buffer copied by the Asreadonlybuffer () method is similar to duplicate (), is read-only , and cannot be called put.
- c) Slice () method, the name Incredibles, similar to cutting a buffer out, similar to duplicate, but it will start from the current position of the original buffer, and capacity equals the original buffer number of remaining elements, that is ( limit-position).
向Buffer写数据
从channel写入到Buffer
intbytesRead = inChannel.read(buf);
通过Buffer的put方法写入
buf.put(127);
相对位置:在position之后写入数据,并改变position
put(byte b);
put(byte[] src);
put(byte[] src, int offset, int length);
put(ByteBuffer src);
绝对位置:提供写入的位置,并不改变position值。
put(int index, byte b);
Read data from Buffer
Read the data from the channel
intbytesWritten = inChannel.write(buf);
通过Buffer的get方法
相对位置
get()
get(byte[] dst);
get(byte[] dst, int offset, int length);
绝对位置
get(int index);
Java NiO Learning Buffer