Java NIO (2) Channels and buffers (Channel and buffer)

Source: Internet
Author: User

Java NIO (2) Channels and buffers (Channel and buffer)
Guibin.beijing@gmail.com

Overview
Channel and Buffer are the core objects in NIO. They are used in almost every NIO operation.

The Channel simulates the concept of stream in the old IO package. All data that goes anywhere (or from anywhere) must pass through the Channel object. Buffer is essentially a container object. Any data sent to the Channel must be put into the Buffer first. Similarly, any data read from the Channel is read into the Buffer first.

In this section, you will learn to use Channel and Buffer in NIO.

What is Buffer?
Buffer is a container object that loads data. The data is read from the Buffer or written into the Buffer. Buffer objects are added to NIO, which is the most important difference between NIO and old IO. In Stream-oriented I/O, you can directly write data to the Stream object, or directly read data from the Stream without any container.

Buffer is essentially an array ). It is usually a byte array or an available array of other types. However, in addition to an array, Buffer also provides structured data access methods and is used to track the system's read/write processes.

Buffer type
The most commonly used Buffer is ByteBuffer. ByteBuffer allows get/set (that is, to read/write bytes) to operate on the underlying byte array.

ByteBuffer is not the only buffer in NIO. In fact, each JAVA Native type corresponds to a corresponding Buffer.
ByteBuffer
CharBuffer
Protocol Buffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer

Each type of Buffer class is an instance of the Buffer interface. In addition to ByteBuffer, each Buffer has the same operation, only because of the Data Type they operate. ByteBuffer is used for the vast majority of standard I/O operations. In addition to other types of Buffer operations, it also has some personalized operations.

What is a Channel?
A Channel is an object where you can read data from a Channel and write data from the Channel. Compared with the old IO, NIO is like Stream.

As mentioned earlier, all data processed by NIO must be written to the Channel through the Buffer object instead of directly writing any bytes. Instead, data must be written to the Buffer first. Buffer can contain less than one byte and more than any number of bytes. Similarly, you cannot directly read any byte from the Channel. You must first read the data into the Buffer through the Channel and then obtain the data from the Buffer.

Channel Type
The difference between a Channel and a Stream is that a Channel is bidirectional, while a Stream can only be unidirectional (Stream must be a subclass of InputStream or OutpugStream, that is, either an input Stream or an output Stream, the Channel can be read, written, or read/write at the same time.

Because the Channel is bidirectional, it better reflects the essence of the underlying operating system IO than Stream. Especially in Linux, the underlying operating system is bidirectional.

Author: "guibin"
 

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.