Diagram of Bytebuffer in Java

Source: Internet
Author: User

  1. Why write an article about Bytebuffer on the internet is really a lot, why do you want to write one here? Mainly based on the following considerations
    1. Many people in the use of T-io, will not Bytebuffer, will only follow T-io provided by the example tiger, not conducive to flexible use
    2. Online search of some related articles, not too easy to understand, not conducive to beginners flexible use of bytebuffer
    3. This article is intended to explain the minimal knowledge needed to flexibly apply bytebuffer to help users quickly master Bytebuffer
  2. meet Bytebuffer in a very easy way
    1. First impressions of Bytebuffer's eyesWe can think of Bytebuffer as a new object composed of several members, that is, a generic Java object, like a string Java object. (to emphasize, this is just to say that, in fact, some of the Bytebuffer implementation classes are not implemented, and here are only the minimum set of knowledge needed to master Bytebuffer, and other fields such as Mark are not introduced in order to avoid increasing the confusion of beginners)
        1. byte[] bytes: Used to store data
        2. int capacity: Used to represent the capacity of bytes, then you can imagine that capacity is equal to Bytes.size (), which is immutable after initialization of bytes.
        3. int limit: Used to indicate how much data the bytes actually loaded, it is easy to imagine the limit <= capacity, which can be flexibly changed
        4. int position: used to indicate where to start writing data to bytes or to read data, this value can be flexibly changed
      Through, to bytebuffer form a sense of understanding it
    2. The common operation of Bytebuffer and the changes of internal variables caused by each operation
      1. Create Bytebuffer:ByteBuffer.allocate (6)

      2. Write one byte: Bytebuffer.put ((byte) 3)

      3. Read a byte: byte bs = Bytebuffer.get () for the bytebuffer that we have just written, we need to set up position and limit before we can read it.
        byteBuffer.position(0);  //设置position到0位置,这样读数据时就从这个位置开始读byteBuffer.limit(1);     //设置limit为1,表示当前bytebuffer的有效数据长度是1
        Let's look at the internal changes of Bytebuffer after setting position and limit

        Next, we'll be able to read the data we just wrote.
        byte bs = byteBuffer.get();

    3. Bytebuffer Use of the experienceHere is the author's own use of bytebuffer some of the experience, these are not so much a result, rather than practice + test some of the experiences, so not guarantee is authoritative, welcome to have a deeper study of friends to reasonable discussion, if there are different opinions, can be better argument to persuade each other.
      1. Heapbytebuffer.get (byte[], int, int) less efficient than system.arraycopy ()The former implementation of the principle is used for the loop to do, the latter is the memory copy, T-io is generally used to do the bytebuffer combination, such as Sendrunnable.java the following code
        Bytebuffer Allbytebuffer = bytebuffer.allocate (allbytebuffercapacity);byte[] Dest = Allbytebuffer.array (); for(BytebufferBytebuffer:Bytebuffers) {if(Bytebuffer! =NULL) {intLength = Bytebuffer.limit ();intPosition = Allbytebuffer.position (); System.arraycopy (Bytebuffer.array (),0, dest, position, length), allbytebuffer.position (position + length);}}
        Note: If Directbuffer does not use system.arraycopy instead of Get (byte[], int, int), because the internal implementation of the goods is not byte[]
      2. JDK Bytebuffer is already good enough to use some Nio/aio framework like to get a set of bytebuffer, not only increase the author's own workload, but also increase the user's learning costs, but we need to know a little, nio/aio when sending data, The final parameter is the JDK Bytebuffer, do we really need to do another conversion and calculation? Although some intermediate processes are "0 copies" (this "0 copy" also has an additional computational cost), the creation of the JDK version of Bytebuffer has been delivered, and what has been done to complete the process? Is it really like a "0 copy" of a book on a blog? It is also not necessary to put some object pools into the concept of "0 copies" to----object Pool object reuse category, since the reuse of nature has already defaulted on 0 copies of the property, but the object pool itself is also required to maintain the resource consumption, so not all scenarios that use the object pool, Performance is improved, sometimes with a bad instead of increasing the burden, so everything to test data, should not blindly conform!
  3. finally attach the sample program of BytebufferAttached here is a sample program for Bytebuffer, which allows users to debug their own observations and add bytebuffer concepts to make more flexible use of the Bytebuffer
    ImportJava.nio.ByteBuffer; /** *  @author  Tanyaowu * May 1, 2017 morning 9:00:50  * * Public   class   Ts  { /** * *  @author: Tanyaowu */ Public Ts   ( )   {} /** *  @param  args *  @author: Tanyaowu */        public static void Main (string[] args)   {Bytebuffer Bytebuffer = bytebuffer.allocate (6); Bytebuffer.put (byte)3); Bytebuffer.position (0);//Set position to 0 position so that reading data starts at this positionBytebuffer.limit (1);//Set limit to 1, indicating that the valid data length for the current bytebuffer is 1byteBS = Bytebuffer.get (); System.out.println (Bytebuffer);}}

Diagram of Bytebuffer in Java

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.