For subsequent ProtocolFilter (
Codec) to write a better understanding, this article describes the Mina ByteBuffer
And Java Nio ByteBuffer. About Java Nio ByteBuffer
And the class diagrams of Mina ByteBuffer and its sub-classes are provided in the attachment. Because
The ByteBuffer of Mina is in the Mina 2.0
The above versions are all renamed as IoBuffer. In order
Mina ByteBuffer is collectively referred to as IoBuffer.
, Java Nio ByteBuffer collectively referred to as ByteBuffer
. About ByteBuffer in IoBuffer
Extensions and some important methods are marked with red boxes in the IoBuffer class diagram. For more information, see the attachment.
Before discussing IoBuffer, let's briefly describe ByteBuffer.
. IoBuffer is used for ByteBuffer
. Many methods in IoBuffer are for ByteBuffer
. Only some more practical methods are added for ByteBuffer.
(1) bytebuffer Introduction
Bytebuffer inherits from the buffer class,
Bytebuffer stores bytes. If you want to convert them into strings, you need to use charset
Charset is character encoding. It provides the ability to convert byte streams into strings (
Decoding) and converting a string into a word-based throttling (encoding)
. This is similar to the working principle of the Mina codec later. Pair
Bytebuffer can be accessed using read (),
Write () and other methods.
Bytebuffer has three important attributes:
1) Capacity: the amount of data that can be stored in the cache.
2) limit: indicates the location of the read/write cache. You cannot read or write data beyond the specified location.
Position: indicates the read/write unit of the next cache area. The location of each read/write cache area changes. The position is a non-negative integer.
These three properties of bytebuffer are equivalent to three tag bits to indicate the areas where the program can read and write:
It simply represents the capacity, limit, and position in the cache area. The limit value must be within the capacity, that is, the limit value cannot exceed the capacity. The same position is used to mark the start position of a program to perform read or write operations on the cache area. The program can only read and write within the limit, that is, the read and write operations cannot exceed the limit, so the size of the position value cannot exceed the limit. Capacity>
Limit> position> = 0.
As mentioned above, the three attributes of ByteBuffer are only the tag location of the cache area. How can we change the positions of these tags?
ByteBuffer provides three methods to change the preceding attribute values.
1) clear (): The limit is set to capacity, and the position is set to 0.
.
2) flip (): The limit is set to position, and the position is set to 0
.
3) rewind (): The limit is not changed, and the position is set to 0.
Java code
- // JDK does not provide the bytebuffer public constructor.
- // Method to create a cache area.
- Bytebuffer buffer = bytebuffer. Allocate (1024 );
- // ========== Test cache read/write a string ========== //
- String username = "chinaestone ";
- Char [] charArray = userName. toCharArray ();
- System. out. println ("this is a string stored in the cache ");
- For (int I = 0; I <charArray. length; I ++ ){
- System. out. println (charArray [I]);
- Buffer. putChar (charArray [I]);
- }
- Buffer. Limit (buffer. Position ());
- Buffer. Position (0 );
- System. Out. println ();
- System. Out. println ("this is the string retrieved from the cache ");
- While (buffer. hasremaining ()){
- System. Out. println (buffer. getchar ());
- }
[Java]
View plaincopyprint?
- // JDK does not provide the bytebuffer public constructor.
- // Method to create a cache area.
- Bytebuffer buffer = bytebuffer. Allocate (1024 );
- // ========== Test cache read/write a string ========== //
- String username = "chinaestone ";
- Char [] chararray = username. tochararray ();
- System. Out. println ("this is a string stored in the cache ");
- For (int I = 0; I <charArray. length; I ++ ){
- System. out. println (charArray [I]);
- Buffer. putChar (charArray [I]);
- }
- Buffer. limit (buffer. position ());
- Buffer. position (0 );
- System. out. println ();
- System. Out. println ("this is the string retrieved from the cache ");
- While (buffer. hasremaining ()){
- System. Out. println (buffer. getchar ());
- }
// JDK does not provide the ByteBuffer public constructor. You can only use this // method to create a cache zone. ByteBuffer buffer = ByteBuffer. allocate (1024); // ========= test the cache to read and write a String ==========/string userName = "chinaestone "; char [] charArray = userName. toCharArray (); System. out. println ("this is the string stored in the cache"); for (int I = 0; I <charArray. length; I ++) {System. out. println (charArray [I]); buffer. putChar (charArray [I]);} buffer. limit (buffer. position (); buffer. position (0); System. out. println (); System. out. println ("this is the string retrieved from the cache"); while (buffer. hasRemaining () {System. out. println (buffer. getChar ());}
The above is just a simple demo program. Its function is to read and write strings.
", Huh, huh. How to read and write strings to ByteBuffer
Detailed description in iobuffer.
(2) Introduction to iobuffer
Iobuffer is an extension of bytebuffer, not
Bytebuffer is irrelevant. For Mina or
For socket applications, the methods provided by bytebuffer are insufficient:
1) It does not provide enough available put and
Set method, such as fill and get/putstring
And get/putasciiint.
2) It is difficult to put variable-length data into bytebuffer.
Based on the preceding disadvantages, Mina provides iobuffer
To supplement the limitations of bytebuffer.
Let's drink code. Let's take a look at Mina's
How iobuffer reads and writes strings.
Java code
- // Obtain a 1024-bytebuffer
- Bytebuffer buffer = bytebuffer. Allocate (1024 );
- // Set the system character set to UTF-8
- Charset CH = charset. forname ("UTF-8 ");
- // Obtain the UTF-8 Encoder
- Charsetencoder encoder = CH. newencoder ();
- // Obtain the UTF-8 Decoder
- Charsetdecoder decoder = CH. newdecoder ();
- System. Out. println (buffer. Remaining ());
- // Encode the string
- String cs = "China's Yishi headers ";
- // Encode the string and put it into the cache
- Buffer. putstring (CS, encoder );
- System. Out. println (buffer. Remaining ());
- // Set the cache location to the location
- Buffer. Limit (buffer. Position ());
- // Set the cache location to 0
- Buffer. Position (0 );
- // Read the cached string
- String str = buffer. getString (decoder );
- // Print the information in the output Cache
- System. out. println (str );
- System. out. println (buffer. remaining ());
[Java]
View plaincopyprint?
- // Obtain a 1024-ByteBuffer
- ByteBuffer buffer = ByteBuffer. allocate (1024 );
- // Set the system character set to UTF-8
- Charset ch = Charset. forName ("UTF-8 ");
- // Obtain the UTF-8 Encoder
- Charsetencoder encoder = CH. newencoder ();
- // Obtain the UTF-8 Decoder
- Charsetdecoder decoder = CH. newdecoder ();
- System. Out. println (buffer. Remaining ());
- // Encode the string
- String cs = "China's Yishi headers ";
- // Encode the string and put it into the cache
- Buffer. putString (cs, encoder );
- System. out. println (buffer. remaining ());
- // Set the cache location to the location
- Buffer. limit (buffer. position ());
- // Set the cache location to 0
- Buffer. position (0 );
- // Read the cached string
- String STR = buffer. getstring (decoder );
- // Print the information in the output Cache
- System. Out. println (STR );
- System. Out. println (buffer. Remaining ());
// Obtain a 1024-bytebufferbytebuffer buffer = bytebuffer. allocate (1024); // set the system character set to utf-8Charset CH = charset. forname ("UTF-8"); // obtain the UTF-8 encoder charsetencoder encoder = CH. newencoder (); // obtain the charsetdecoder of UTF-8 decoder. decoder = CH. newdecoder (); system. out. println (buffer. remaining (); // encoded string cs = ""; // encode the string and put it in the cache buffer. putstring (CS, encoder); system. out. println (buffer. remaining (); // set the cache location to the location buffer. limit (buffer. position (); // set the cache position to 0 buffer. position (0); // read the string STR = buffer in the cache. getstring (decoder); // print the information in the output cache system. out. println (STR); system. out. println (buffer. remaining ());
Note that charset is used here. It is used for encoding and decoding. Therefore, you must use the same encoding when encoding and decoding strings.
(3) subclass of iobuffer
To better use iobuffer for development, iobuffer
Provides two sub-classes: basebytebuffer and bytebufferproxy.
. Basebytebuffer implements iobuffer
The vast majority of methods defined in. This class can be inherited if you want to extend the method that suits you in actual development, because it can make your implementation easier. Bytebufferproxy
Contains an iobuffer.
All operations can be implemented through the methods provided by this class.
This article briefly introduces iobuffer and bytebuffer
For more information about iobuffer, see Mina.
Help documentation and Mina source code.