Java NIO translation to establish a new buffer (1)

xiaoxiao2021-03-06  201

When you do a variety of I / O operations on the channel, you must be done using the buffer. Buffer is an array object containing data, and the data within the buffer can be used from access channels. Data. It is easy to create a buffer, here let's take a look at how to create a Bytebuffer object:

INT size = 1024; Bytebuffer Buffer = Bytebuffer.allocate (size);

This method only has a simple Size parameter: the underlying array length. Here we call it the capacity of the buffer. Once the buffer is created, its capacity will not change again. The best buffer capacity value depends on you Applications, larger capacity values ​​can provide higher throughput, but more memory. Vioerately, if you use a smaller capacity value, you will cost less memory, but it will also reduce some performance.

You may notice that we have not used traditional constructors, which is normal. The buffer is either by static allocate () method, either from the existing byte array through WARP () The function is coming, it does not need to be created directly.

You may also notice that we have created a Bytebuffer, which also includes intBuffer, ShortBuffer, FloatBuffer, etc. in the Java.NIO package, and the buffer has established a corresponding buffer subclass for each original type in Java. At the same time, there is an abstract class called buffer, which is the parent class of all buffer classes. In the later section, we will explain how to make basic

Channel I / O operation.

转载请注明原文地址:https://www.9cbs.com/read-128664.html

New Post(0)