Java NIO (i)

Source: Internet
Author: User

Java NIO

Core section (Simple):

Channels (channel)

Buffers (buffer)

Selectors (selector)

-----------------------------------------------------

Channel implementations:

FileChannel (Read and write data from a file)

Datagramchannel (Can read and write data in the network via UDP)

Socketchannel (Can read and write data in the network via TCP)

Serversocketchannel (can listen for new incoming TCP connections, like a Web server, create a socketchannel for each new connection)

-----------------------------------------------------

Buffer implementation:

corresponds to seven basic types: Bytebuffer, Charbuffer, DoubleBuffer, Floatbuffer, Intbuffer, Longbuffer, Shortbuffer

as well Mappedbytebuffer

-----------------------------------------------------

Selector: Selector allows multiple channel processing in single thread

-----------------------------------------------------

The difference between NIO and traditional IO

1: The channel can read data, can also write data to the channel.

2: Channel can read and write data asynchronously

3: The data in the channel is always read to a buffer first, or it is always written from a buffer

public static void Main (string[] args) throws IOException {Randomaccessfile afile = new Randomaccessfile ("d:/payment merchant record. txt", "RW"); FileChannel Inchannel = Afile.getchannel (); Bytebuffer buff = bytebuffer.allocate (64);  Set buffer size int byteread = 0;//inchannel.read (buff); Read data from the channel to the buffer while (byteread! =-1) {//system.out.println ("read---------->" + byteread); Buff.flip ();//Invert.  Switch read/write mode while (Buff.hasremaining ()) {//Detect buffer System.out.print ((char) buff.get ()); The Fetch data}/**clear () method empties the entire buffer **///buff.clear (); the/**compact () method clears only the data that has been read. Any unread data is moved to the beginning of the buffer, and the newly written data is placed behind the buffer's unread data. **/buff.compact (); byteread = Inchannel.read (buff);} Afile.close ();}


Buffer interacts with the channel:

The channel reads the data into buffer;

Writes the data of buffer to the channel;

Basic use of buffer--Four steps:

1: Write data to buffer.

2: Call the Flip () method to reverse the read/write mode.

3: Read data from buffer.

4: Call the Clear () or compact () method.

A: When writing data to buffer, buffer records how much data is written, and once the data is read, buffer is Chei Huan from write mode to read mode via the Flip () method.

B: Once you have read all the data, you need to clear the buffer so that it can read and write again.

How the Buffer works:

The buffer is essentially a piece of memory that can read and write data, and it is packaged into a buffer object of NIO.

Three attributes: capacity, position, limit

Capacity: total capacity, which is the value set when getting the buffer

Position: the location of the current read or write.

The value of position and limit depends on whether buffer is in read or write mode

When writing data to buffer, position represents the current position, initially 0, and when one data is written to buffer, position moves forward to the next location where data can be inserted. Position Max can be: capacity-1

when reading data: switch from write mode to read mode, position will be set to 0, read will start from the position of position, read position forward to the next position.

Limit: in write mode, the limit of buffer indicates how much data you can write to buffer. Write mode, limit equals buffer's capacity.

When you switch buffer to read mode, limit indicates how much data you can read. Therefore, when you toggle buffer to read mode, limit is set to the position value in write mode. In other words, you can read all the previously written data (limit is set to the number of written data, this value is position in write mode)

==================


Java NIO (i)

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.