Java network programming from getting started to mastering (34)

Source: Internet
Author: User
Tags abstract final

Java network programming from getting started to mastering (34): reading and writing data in buffer---using GET and put methods to read and write individual data sequentially

The most important operation for a buffer is the read and write operation. The buffer provides two ways to read and write data in the buffer: Get, put method, and array method. The get, put method can have three ways to read and write data: Read and write a single data sequentially, read and write a single data at a specified location, and read and write data blocks. In addition to several of the above methods of reading and writing data, the Charbuffer class also provides a put and append method for writing strings specifically. The methods of reading and writing buffers are described separately in this article and later in this article.

Although a buffer created using the Allocate method is not a one-time allocation of memory space, we can imagine a buffer from the user's point of view as an array of length capacity. When a buffer is created, the size of the buffer (the capacity value) cannot be changed and the data outside the buffer cannot be accessed, as in the array. As the following code creates a byte buffer of size 6.

ByteBuffer byteBuffer = ByteBuffer.allocate(6);

For Bytebuffer, only six bytes of data belonging to this buffer can be accessed, and if this range is exceeded, a bufferoverflowexception exception is thrown, which is a run-time error, because the error can only be found when the program is running.

Since buffers and arrays are similar, buffers should also be able to identify the current location as an array. The position method of the buffer provides us with this function. The position method has two overloaded forms, which are defined as follows:

public final int position()
public final Buffer position(int newPosition)

The first overloaded form is used to get the current position of the buffer. After the buffer is created, the initial value of the position is 0, which is the position of the first element of the buffer. When an element is read from the buffer, the value of the position is added to 1. We can see from this point that the position method returns the position of the element that is currently available for reading. The position range ranges from 0 to capacity–1. If the value of position equals capacity, there is currently no data readable for the buffer.

The second overloaded form of the position method can set the current position of the buffer. The value range of parameter newposition is 0 <= newposition < capacity. If the value of the newposition is outside this range, the position method throws a IllegalArgumentException exception.

In most cases, you do not need to directly control the location of the buffer. The method provided by the buffer class for reading and writing data can automatically set the current position of the buffer. In the buffer class, the get and put methods are used to read and write data in the buffer. The Get and put methods are defined as follows:

Get and put methods for the Bytebuffer class:

public abstract byte get()
public abstract ByteBuffer put(byte b)

Get and put methods for the Intbuffer class:

public abstract int get()
public abstract IntBuffer put(int i)

The get and put method definitions in the other five buffer classes are similar to the definitions above, except that the Get method returns the corresponding data type, and the parameter of the Put method is the corresponding data type, and the type of the return value is the corresponding buffer class.

Each time the Put method writes a data to the buffer, the current position of the buffer is incremented by 1. If the current position of the buffer is already equal to capacity, invoking the Put method throws a Java.nio.BufferOverflowException exception. Areas that are not initially assigned to the buffer are populated by 0. Use the Get method to obtain the current position of the buffer and to add 1 to the current position of the buffer. As with the Put method, using the Get method also throws a Java.nio.BufferOverflowException exception when the current position of the buffer equals capacity. The initial state of the buffer is shown in Figure 1.

Figure 1 The initial state of the buffer

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.