In-depth understanding of the differences between Apache Mina (6) -- Java Nio ByteBuffer and Mina ByteBuffer.

Source: Internet
Author: User

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
  1. // JDK does not provide the bytebuffer public constructor.
  2. // Method to create a cache area.
  3. Bytebuffer buffer = bytebuffer. Allocate (1024 );
  4. // ========== Test cache read/write a string ========== //
  5. String username = "chinaestone ";
  6. Char [] charArray = userName. toCharArray ();
  7. System. out. println ("this is a string stored in the cache ");
  8. For (int I = 0; I <charArray. length; I ++ ){
  9. System. out. println (charArray [I]);
  10. Buffer. putChar (charArray [I]);
  11. }
  12. Buffer. Limit (buffer. Position ());
  13. Buffer. Position (0 );
  14. System. Out. println ();
  15. System. Out. println ("this is the string retrieved from the cache ");
  16. While (buffer. hasremaining ()){
  17. System. Out. println (buffer. getchar ());
  18. }
[Java]
View plaincopyprint?
  1. // JDK does not provide the bytebuffer public constructor.
  2. // Method to create a cache area.
  3. Bytebuffer buffer = bytebuffer. Allocate (1024 );
  4. // ========== Test cache read/write a string ========== //
  5. String username = "chinaestone ";
  6. Char [] chararray = username. tochararray ();
  7. System. Out. println ("this is a string stored in the cache ");
  8. For (int I = 0; I <charArray. length; I ++ ){
  9. System. out. println (charArray [I]);
  10. Buffer. putChar (charArray [I]);
  11. }
  12. Buffer. limit (buffer. position ());
  13. Buffer. position (0 );
  14. System. out. println ();
  15. System. Out. println ("this is the string retrieved from the cache ");
  16. While (buffer. hasremaining ()){
  17. System. Out. println (buffer. getchar ());
  18. }
// 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
  1. // Obtain a 1024-bytebuffer
  2. Bytebuffer buffer = bytebuffer. Allocate (1024 );
  3. // Set the system character set to UTF-8
  4. Charset CH = charset. forname ("UTF-8 ");
  5. // Obtain the UTF-8 Encoder
  6. Charsetencoder encoder = CH. newencoder ();
  7. // Obtain the UTF-8 Decoder
  8. Charsetdecoder decoder = CH. newdecoder ();
  9. System. Out. println (buffer. Remaining ());
  10. // Encode the string
  11. String cs = "China's Yishi headers ";
  12. // Encode the string and put it into the cache
  13. Buffer. putstring (CS, encoder );
  14. System. Out. println (buffer. Remaining ());
  15. // Set the cache location to the location
  16. Buffer. Limit (buffer. Position ());
  17. // Set the cache location to 0
  18. Buffer. Position (0 );
  19. // Read the cached string
  20. String str = buffer. getString (decoder );
  21. // Print the information in the output Cache
  22. System. out. println (str );
  23. System. out. println (buffer. remaining ());
[Java]
View plaincopyprint?
  1. // Obtain a 1024-ByteBuffer
  2. ByteBuffer buffer = ByteBuffer. allocate (1024 );
  3. // Set the system character set to UTF-8
  4. Charset ch = Charset. forName ("UTF-8 ");
  5. // Obtain the UTF-8 Encoder
  6. Charsetencoder encoder = CH. newencoder ();
  7. // Obtain the UTF-8 Decoder
  8. Charsetdecoder decoder = CH. newdecoder ();
  9. System. Out. println (buffer. Remaining ());
  10. // Encode the string
  11. String cs = "China's Yishi headers ";
  12. // Encode the string and put it into the cache
  13. Buffer. putString (cs, encoder );
  14. System. out. println (buffer. remaining ());
  15. // Set the cache location to the location
  16. Buffer. limit (buffer. position ());
  17. // Set the cache location to 0
  18. Buffer. position (0 );
  19. // Read the cached string
  20. String STR = buffer. getstring (decoder );
  21. // Print the information in the output Cache
  22. System. Out. println (STR );
  23. 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.

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.