How is java-16.6 bytebuffer stored from the beginning?

Source: Internet
Author: User

In this section, let's discuss how bytebuffer is stored.

1. Example:

Package Com.ray.ch16;import Java.io.ioexception;import Java.nio.bytebuffer;import java.nio.charbuffer;import Java.nio.intbuffer;import Java.nio.longbuffer;public class Test {public static void main (string[] args) throws IOException {Bytebuffer buffer = bytebuffer.allocate (16); Charbuffer Charbuffer = Buffer.ascharbuffer () charbuffer.put (new char[] {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h '}); Charbu Ffer.rewind (); while (Charbuffer.hasremaining ()) {System.out.print (charbuffer.position () + "+" + charbuffer.get () + " ");} System.out.println (); Intbuffer intbuffer = Buffer.asintbuffer (); Intbuffer.put (New int[] {1, 2, 3, 4}); Intbuffer.rewind (); while (Intbuffer.hasremaining ()) {System.out.print (intbuffer.position () + "+" + intbuffer.get () + "");} System.out.println (); Longbuffer Longbuffer = Buffer.aslongbuffer () longbuffer.put (new long[] {1, 2}); Longbuffer.rewind (); while ( Longbuffer.hasremaining ()) {System.out.print (longbuffer.position () + "+" + longbuffer.get () + ");}}}

Output:

0->a 1->b 2->c 3->d 4->e 5->f 6->g 7->h
0->1 1->2 2->3 3->4
0->1 1->2

Explain why there is less data on the above because each category occupies a different number of bits, and buffer is wrapping a 16-bit array, so this happens.


What is the storage of 2.buffer?


Depending on the number of bytes stored in each type, we can draw the diagram above.

The first type is char, the second is int, and the third is long

In fact, Xxxbuffer is guaranteed a specified length of the array, if more than this length, will throw an exception.


3. Controlling the order of storage by parameters

Package Com.ray.ch16;import Java.io.ioexception;import Java.nio.bytebuffer;import java.nio.byteorder;import Java.nio.charbuffer;import Java.util.arrays;public class Test {public static void main (string[] args) throws IOException {Bytebuffer buffer = bytebuffer.allocate (+); Buffer.order (Byteorder.big_endian); Charbuffer Charbuffer = Buffer.ascharbuffer (); Charbuffer.put ("ABCDEFG"); System.out.println (Arrays.tostring (Buffer.array ())); System.out.println (); Buffer.order (Byteorder.little_endian); charbuffer = Buffer.ascharbuffer (); Charbuffer.put (" ABCDEFG "); System.out.println (Arrays.tostring (Buffer.array ()));}}

Output:

[0, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 0]


[97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 0, 0]


Looking at the code above, we added order to control the sequence of storage, which can be seen from the output.


Summary: This chapter mainly discusses how Bytebuffer is stored.


This chapter is here, thank you.

-----------------------------------

Directory



How is java-16.6 bytebuffer stored from the beginning?

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.