Java network programming from getting started to proficient (33): Non-blocking I/O buffers (buffer)

Source: Internet
Author: User

If the data transfer in synchronous I/O mode is compared to the sporadic means of data transfer (this is sporadic in the case of a sporadic byte in the process of data transfer), the data transfer in non-blocking I/O mode can be compared to the container mode of data transmission (between byte and lower data transmission, A buffer is more than one layer, so you can consider a buffer as a container for loading bytes. As you can imagine, if we were to ship less goods, it would be a bit less expensive to use a container, and if we were to ship hundreds of tons of goods, the cost of transporting them in containers would be lower. The same is true in the data transfer process, if the volume is very small, the use of synchronous I/O will be more appropriate, if the data volume is very large (generally in g), the use of non-blocking I/O method is more efficient. Therefore, theoretically, the larger the amount of data, the lower the unit cost of using non-blocking I/O. The reason for this result is directly related to some of the characteristics of the buffer. In this section, we will explain some of the key features of the buffer, so that the reader can fully understand the concept of the buffer, and can improve the execution efficiency of the program through the buffer.

Create buffer

Java provides seven basic buffers that are managed by seven classes, all of which can be found in the Java.nio package. These seven classes are as follows:

Bytebuffer

Shortbuffer

Intbuffer

Charbuffer

Floatbuffer

DoubleBuffer

Longbuffer

The methods in these seven classes are similar, except that their return values or parameters correspond to the corresponding simple types, such as the Get method of the Bytebuffer class that returns the byte type of data, and the Put method requires a byte parameter. The data type returned and passed by the get and put methods in the Charbuffer class is char. None of these seven classes have public constructor methods, so they cannot create corresponding object instances through new. These classes can create a corresponding object instance in two ways.

1. Create a buffer by static method allocate.

These seven classes have a static allocate method that allows you to create a buffer object with the maximum capacity limit. The definition of allocate is as follows:

Allocate methods in the Bytebuffer class:

public static Bytebuffer allocate (int capacity)

Allocate methods in the Intbuffer class:

public static Intbuffer allocate (int capacity)

The Allocate method definition in the other five buffer classes is similar to the above definition, except that the type of the return value is the corresponding buffer class.

The allocate method has a parameter capacity to specify the maximum buffer capacity. Capacity cannot be less than 0, otherwise a IllegalArgumentException exception will be thrown. Using allocate to create a buffer is not a space allocated to the size of the buffer capacity, but rather the size of the buffer dynamically allocated based on the data stored in the buffer (in fact, the heap in the lower Java data structure is used to manage the size of the buffer). This capacity can be a very large value, such as 1024*1024 (1M). The use of allocate is as follows:

ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
IntBuffer intBuffer = IntBuffer.allocate(1024); 

When you create a buffer using allocate, note that the meaning of the capacity varies with the buffer. When you create a byte buffer, capacity refers to the number of bytes. When creating an integer (int) buffer, capacity refers to the number of int values and, if converted to words, the capacity value should multiply by 4. The maximum number of bytes that can be accommodated in the Intbuffer buffer, as in the above code, is 1024*4 = 4,096.

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.