Java NiO bytebuffer Use Records __java

Source: Internet
Author: User

Recently in doing an amateur project, expect to learn the Java language in the project, encountered in the process of bytebuffer, feeling in this project is a very important foundation of a thing, so take this opportunity to learn, to remember.

Flapmmo-20140225\src\main\java\org\javaee7\websocket\flapmmo\flapendpoint.java

Package Org.javaee7.websocket.flapmmo;
Import java.io.IOException;
Import Java.nio.ByteBuffer;
Import Javax.websocket.OnMessage;
Import Javax.websocket.OnOpen;
Import Javax.websocket.OnClose;
Import javax.websocket.Session;

Import Javax.websocket.server.ServerEndpoint; @ServerEndpoint ("/websocket") public class Flapendpoint {    @OnMessage     public void OnMessage (byt Ebuffer data, Session session) throws IOException {       //SYSTEM.OUT.PRINTLN ("echobinary:" + data
);         for (Byte B:data.array ()) {            System.out.print (integ
Er.tohexstring (b & 0xFF). toUpperCase () + "");
       }         SYSTEM.OUT.PRINTLN ("");
        Bytebuffer BB = bytebuffer.allocate (256);
        bb.put ((byte) 0);
        Bb.putint (0x12345678);         session.getbasicremote () sendbiNary (BB);    }     @OnOpen     public void OnOpen () {        SYSTEM.OUT.PRINTLN
("Client connected");    }     @OnClose     public void OnClose () {        System.out.print
ln ("Connection closed");
    }}


Code logic:

When the browser sends data to the WebSocket server, the OnMessage method executes and the data is stored in the form of a byte buffer in parameter data. The server receives the data, invokes the OnMessage method, prints the data in 16, and creates a new byte buffer bb, populates the BB with the data to be sent, and sends the data to the browser end.

http://docs.oracle.com/javase/7/docs/api/

Java.lang.Object

Java.nio.Buffer

Java.nio.ByteBuffer

Public abstract class Bytebuffer
Extends Buffer
Implements Comparable<bytebuffer>

A Bytebuffer.

Modifier and Type

Method and Description

Static Bytebuffer

Allocate (int capacity)

Allocates a new byte buffer.

Byte[]

Array ()

Returns the byte array that backs this buffer (optional operation).

Abstract Bytebuffer

Put (Byte b)

Relative put method (optional operation).

Abstract Bytebuffer

Putint (int value)

Relative put to writing an int value (optional operation).

Summary of Bytebuffer usage

http://blog.csdn.net/mars5337/article/details/6576417

In NiO, data read and write operations are always associated with buffers. The Read-time channel (Socketchannel) reads data into the buffer, which first fills the buffer with the data sent in order. The buffer is fixed-length, basically it's just a list, all of its elements are basic data types. Bytebuffer is the most commonly used buffer that provides a way to read and write to other data types, and the read-write method of the channel only receives bytebuffer. Therefore, the use of bytebuffer is necessary to be firmly grasped.

1. Create Bytebuffer

1.1 using allocate () static method

Bytebufferbuffer=bytebuffer.allocate (256);

The above method creates a bytebuffer with a capacity of 256 bytes, and if you find that the buffer size you created is too small, the only option is to recreate a buffer that is the right fit.

learn the bytebuffer of Socketnio

http://blog.csdn.net/maosijunzi/article/details/14127481

1: What is bytebuffer.

Bytebuffer is the byte buffer.

What 2:bytebuffer is used for.

As the name suggests, it is used for storing bytes. The most common place is when the program transmits data to the network.

3: How to use Bytebuffer.

To use Bytebuffer, it is important to understand 3 pointers: position, limit, and capacity

The relationship between these three pointers is position<=limit<=capacity

Position is the current read-write location.

Limit is the most readable and writable position.

Capacity is the capacity of the buffer.

A piece of procedure:

1:bytebufferbuffer = Bytebuffer.allocate (5); Position=0,limit=5,capacity=5

2:buffer.put ((byte) 1); Position=1,limit=5,capacity=5

3:buffer.flip (); Position=0,limit=1,capacity=5

4:buffer.get (); Position=1,limit=1,capacity=5

First line: Creates a bytebuffer, initializes the state, postion is the 0,limit=capacity= buffer size

Second line: Put one byte into the buffer and the position will move back, but not over limit. If the error is exceeded.

Third line: The flip method sets the limit to the position value, and position reverts to 0 to prepare for the read buffer.

Line four: Read a byte from the buffer, position will move back, but not more than limit, if more than the error.

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.