Java NIO's bytebuffer test case

Source: Internet
Author: User

Share some very basic test cases about Bytebuffer

package xingej.buffer.test001;import java.nio.bytebuffer;//Note: 1, native java  NIO's Bytebuffer buffer is not able to add strings, in fact, can be seen from the name, is byte + buffer =>bytebuffer//that is to say, Bytebuffer is a buffer for bytes public class bytebuffertest {    public static  void main (String[] args)  {        //allocates 8 bytes of buffer         bytebuffer buffer = bytebuffer.allocate (8);         //prints the value of position, limit, capacity in the initial state          SYSTEM.OUT.PRINTLN ("---->position:\t"  + buffer.position ()  +  "\ n---->limit:\t"  + buffer.limit ()  +  "\ n---->capacity:\t"  +  Buffer.capacity ());         //------------------------------- Test-----Put----Operation---------------------------------------------------------------        SYSTEM.OUT.PRINTLN ("-----Put------before----------->position:\t"  + buffer.position ()  +  "  ---->limit:\t"  + buffer.limit ()  +   "  ---->capacity:\t"  + buffer.capacity ());         //into the buffer, add a character "H", and then look at the value of Position, limit, capacity          buffer.put ("H". GetBytes ());         SYSTEM.OUT.PRINTLN ("--- --put---operation-------------->position:\t " + buffer.position ()  + "   ---->limit : \ T " + buffer.limit ()  + "   ---->capacity:\t " + buffer.capacity ());         //------------------------------- Test-----Flip----Operation---------------------------------------------------------------         system.out.println ("\ n-----flip------before Operation----------->position:\t " + buffer.position ()  + "   ---->limit:\t "  + buffer.limit ()  +  "  ---->capacity:\t"  + buffer.capacity ());         buffer.flip ();         SYSTEM.OUT.PRINTLN ("-----Flip---after Operation-------------->position:\t"  + buffer.position ()  +  "   ---->limit:\t " + buffer.limit ()  + "   ---->capacity:\t " +  buffer.capacity ());         //------------------------------- Test-----Get----Operations---------------------------------------------------------------         system.out.println ("\ n-----Get---operation---before----------->position:\t"  +  Buffer.position ()  +  "  ---->limit:\t"  + buffer.limit ()  +  "   ---->capacity:\t " + bUffer.capacity ());         SYSTEM.OUT.PRINTLN ("-----fetch data from cache------: \ t"  + buffer.get ());         system.out.println ("----- Get---operation-------------->position:\t " + buffer.position ()  + "   ---->limit:\ T " + buffer.limit ()  + "   ---->capacity:\t " + buffer.capacity ());         //------------------------------- Test-----Limit----Operation---------------------------------------------------------------         //Reset the cache can store capacity size is 1 bytes,         //is obvious, If the actual stored byte size exceeds 1 bytes, the exception will be thrown         system.out.println ("\ N-----Limit---operation---before----------->position:\t " + buffer.position ()  + "   ---- >limit:\t " + buffer.limit ()  + "   ---->capacity:\t" + buffer.capacity ());         buffer.limit (1);         try{             //because h 1 bytes are stored in the cache, ell is not stored in the cache and throws an exception              buffer.put ("ell". GetBytes ());         } catch   (exception e)  {//             E.printstacktrace ();        }         SYSTEM.OUT.PRINTLN ("-----Limit---after operation-------------->position:\t"  + buffer.position ()  +  "  ---->limit:\t"  + buffer.limit ()  +  "  ----> Capacity:\t " + buffer.capacity ());         //---------------- ---------------Test-----Clear----Operation---------------------------------------------------------------         System.out.println ("\ n-----Clear---operation---before----------->position:\t"  + buffer.position ()  +   "  ---->limit:\t"  + buffer.limit ()  +  "  ---->capacity:\t"  + buffer.capacity ());         buffer.clear ();         SYSTEM.OUT.PRINTLN ("-----Clear---after operation-------------->position:\t"  +  buffer.position ()  +  "  ---->limit:\t"  + buffer.limit ()  +  "   ---->capacity:\t " + buffer.capacity ());     }}


Summarize:

A put operation, a get operation, changes the value of position


The disadvantage of buffer is that it is generally necessary to call the flip () method before reading or writing.


The test case has been uploaded to Git.

Https://github.com/xej520/xingej-nio







This article is from the "Xej Distributed Studio" blog, so be sure to keep this source http://xingej.blog.51cto.com/7912529/1970073

Java NIO's bytebuffer test case

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.