Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How NIO channels and buffers work in java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how the NIO channel and buffer in java work. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

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

The channel is a simulation of the flow in the original Istroke O packet. 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 the buffer; similarly, any data read from the channel must be read into the buffer.

What is a buffer zone?

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

In the NIO library, all data is processed with buffers. When reading data, it reads directly into the buffer. When the data is written, it is written to the buffer. Whenever you access data in NIO, you put it in a buffer.

The buffer is essentially an array. It is usually a byte array, but other kinds of arrays can also be used. But a buffer is more than an array. Buffers provide structured access to data and track the read / write process of the system.

Buffer type

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

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

ByteBufferCharBuffer ShortBuffer IntBuffer LongBuffer FloatBuffer DoubleBuffer

Each Buffer class is an instance of the Buffer interface. Except for ByteBuffer, every Buffer class has exactly the same operation, except that they deal with different data types. Because most standard iCandle O operations use ByteBuffer, it has all shared buffer operations as well as some unique operations.

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

What is a passage?

Channel is an object through which you can read and write data. Compare NIO with the original Ibank O, and the channel is like a stream.

As mentioned earlier, all data is processed through the Buffer object. You never write bytes directly to the channel; instead, you write data to a buffer that contains one or more bytes. Similarly, instead of reading a byte directly from the channel, you read the data from the channel into the buffer and get the byte from the buffer.

Channel Typ

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

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

This is the end of this article on "how NIO channels and buffers work in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report