Common methods of Mina Iobuffer

Source: Internet
Author: User

Limit (int)

If position>limit, Position = limit, if mark>limit, reset mark

Mark ()

Take the snapshot of the current position mark

Reset ()

Restore position to a previously marked mark

Clear ()

Limit=capacity, position=0, resetting mark, but not emptying the data, in order to prepare for the start of the put, is to clear the data, because you put the original data is overwritten

Rewind ()

position=0, reset mark, after a series of write operations, in order to prepare for getting from scratch, and clear () for the purpose of the difference, he is mostly used to read from the beginning, and clear is mostly used to start the filling, is to clean up the meaning of

Flip ()

Limit=position, position=0, reset mask, in order to prepare buf write-up, is generally the end of the buf operation, the BUF is written to the output stream when called, this must be called, otherwise very likely position!=limit, Causes no data behind the position, each time the data is written to the output stream, the position=limit must be ensured.

Remaining ()

Returns Limit-position, returning the remaining bytes in the buffer

Wrap (byte[])

Assemble to the new buffer,capacity=limit=byte[].length,position=0 reset mark

Slice ()

Split buffer, the remaining space is formed into a new buffer, the new position=0,limit=capacity=remaining, reset mark, and the main buffer content sharing, others are independent

Duplicate ()

buffers, content sharing, others are independent

Asreadonlybuffer ()

Like duplicate, just can't write

Compact ()

The byte between position and limit is moved to the front, position=limit-position, which is the meaning of compression here, which is generally the end of the BUF operation, which is called when the BUF is written to the output stream.

Position (int)

Position=newposition, if Position<mark, reset mark

Remaining ()

Returns the number of bytes between position and limit

Iobuffer

Iobuffer is a byte Buffer,mina used internally by Mina and does not directly use NiO's bytebuffer. But Iobuffer is a package for the Bytebuffer. Many of the methods in Iobuffer are direct inheritance of bytebuffer. Just added some more practical ways to Bytebuffer.

Basic usage

Since Iobuffer is a bytebuffer encapsulation of NIO, the basic concepts are the same, and the following is a brief introduction:

1.capacity: This property describes how many elements this buffer can buffer, and is the maximum number of buffer storage elements, which is specified at the time of buffer creation and cannot be modified.

2.limit: When writing data from buffer to channel, the limit variable indicates how much data remains to be read, and when the data is read from the channel into buffer, the limit variable indicates how much space is left to hold the data. Position under normal conditions is less than or equal to limit.

3,Position:buffer is actually an array. When you read data from the channel, you put the data read from the channel into the underlying array,position variable to track how much data has been written so far. More precisely, it indicates where the data should go into the array the next time the buffer is written. So if 3 bytes have been read from the channel, the buffer's position will be set to 3, pointing to the fourth position in the array.

4. Mark: A value that remembers the position position, resets the buffer's position to that index when the reset () method is called, but does not always need to define mark, but when defining mark, it cannot be defined as a negative number. and cannot make it larger than position, if mark is defined, the mark is discarded when the position or limit is adjusted to be less than the mark value.

Here's an example to illustrate:

I, in the initial state:

At this time position for 0,limit and capacity are set to 9;

II, reading 4 bytes of data from the channel into buffer, then position points to 4 (5th):

III. Before writing, we have to call the flip () method, which does two important things:
1. Set the limit to the current position.
2. Set position to 0.

IIII, after performing write operation;

IV, after the implementation of clear, position set to 0,limit set to Capition,mark is discarded;

Because Iobuffer is an abstract class that cannot be instantiated directly, all use calls to the Allocate method to allocate memory;

There are two definitions of allocate:

   1://Allocates a new buffer with a specific size, defining its type (direct or heap)
   2:public static Iobuffer allocate (int capacity, Boolean direct)
   
   4://Allocates a new buffer with a specific size
   5:public static Iobuffer allocate (int capacity)

Over here:

The size of the capacity:buffer;

Direct: If true, the direct buffer is obtained and if False, the heap buffer is obtained

differential analysis of direct buffer and heap buffer:

Direct buffer is not allocated on the heap, it is not managed directly by the GC (but the Java object of direct buffer is managed by GC, so long as the GC reclaims its Java object, the operating system releases the space requested by direct buffer). It seems to give the impression that "kernel buffers (buffer in kernel)". The heap buffer is allocated on the heap, or we can simply understand that heap buffer is a form of encapsulation of the byte[] array. When we write a heap buffer to the channel, the underlying implementation actually constructs a temporary direct buffer, and then copies the contents of the heap buffer to this temporary direct buffer, and then the direct Buffer to write it out. Therefore, it is faster to write a direct buffer to a channel than to write a heap buffer to a channel. But the cost of creating and destroying direct buffer is high, so use it where it's possible to reuse it.

public static Iobuffer allocate (int capacity) Usage:
   1://sets the default type of allocates assignment, which is set to heap buffer.
   2:  Iobuffer.setusedirectbuffer (false);
   3:  //Returns a new heap buffer.
   4:  Iobuffer buf = iobuffer.allocate (1024);

Iobuffer allows you to generate an auto-extended buffer (which is one of the reasons why you have not chosen to use NiO bytebuffer); By setting the AutoExpand property:

   1:iobuffer buffer = iobuffer.allocate (8);
   2:buffer.setautoexpand (TRUE);
   
   4:buffer.putstring ("12345678", encoder);
   5:        
   6://ADD more to this buffer
   7:buffer.put ((byte) 10);

Common methods of Mina Iobuffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.