Home  >  Article  >  Java  >  How do Java NIO channels and buffers work?

How do Java NIO channels and buffers work?

WBOY
WBOYforward
2023-05-07 14:25:08754browse

Channels and buffers are the core objects in NIO, and they are used in almost every I/O operation.

A channel is a simulation of the stream in the original I/O package. All data to any destination (or from anywhere) must pass through a Channel object. A Buffer is essentially a container object. All objects sent to a channel must first be placed in a buffer; similarly, any data read from a channel must be read into a buffer.

What is a buffer?

Buffer is an object that contains some data to be written or just read. The addition of Buffer objects to NIO reflects an important difference between the new library and the original I/O. In stream-oriented I/O, you write or read data directly into a Stream object.

In the NIO library, all data is processed using buffers. When reading data, it is read directly into the buffer. When data is written, it is written to a buffer. Any time you access data in NIO, you are putting it into a buffer.

The buffer is essentially an array. Usually it is a byte array, but other kinds of arrays can be used. But a buffer is more than just an array. Buffers provide structured access to data and can also track the system's read/write processes.

Buffer type

The most commonly used buffer type is ByteBuffer. A ByteBuffer can perform get/set operations (i.e., getting and setting bytes) on its underlying byte array.

ByteBuffer is not the only buffer type in NIO. In fact, there is a buffer type for every basic Java type:

ByteBuffer
CharBuffer 
ShortBuffer 
IntBuffer 
LongBuffer 
FloatBuffer 
DoubleBuffer

Every Buffer class is an instance of the Buffer interface. Except for ByteBuffer, every Buffer class has exactly the same operations, but the data types they handle are different. Because most standard I/O operations use ByteBuffer, it has all the shared buffer operations as well as some unique operations.

Now you can take a moment to run UseFloatBuffer.java, which contains an example use of typed buffers.

What is a channel?

Channel is an object through which data can be read and written. Comparing NIO to original I/O, channels are like streams.

As mentioned earlier, all data is handled through Buffer objects. You never write bytes directly into a channel; instead, you write data into a buffer containing one or more bytes. Again, you don't read bytes directly from the channel, you read the data from the channel into a buffer, and get this byte from the buffer.

Channel type

The difference between a channel and a stream is that the channel is bidirectional. While streams only move in one direction (a stream must be a subclass of InputStream or OutputStream), channels can be used for reading, writing, or both.

Because they are bidirectional, channels can reflect the reality of the underlying operating system better than streams. Especially in the UNIX model, the underlying operating system channels are bidirectional.

The above is the detailed content of How do Java NIO channels and buffers work?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete