"JAVA NIO" Channel

Source: Internet
Author: User

1. Class Diagram

Channle is divided into two main categories: The file operation corresponds to the FileChannel and stream operation corresponding to the socket of the 3 channel. question: Does the stream operation have anything but sockets? such as serial port, etc.

3.1.1 Open channel channels can be created in several ways. The socket channel can directly create a new socket channel work?? Method. However, a FileChannel object can only be obtained by invoking the Getchannel () method on an open randomaccessfile, FileInputStream, or FileOutputStream object. You cannot create a FileChannel object directly. File and socket channels are

discussed in detail in a later section.
Socketchannel sc = Socketchannel.open ();
Sc.connect (New Inetsocketaddress ("Somehost", Someport));
Serversocketchannel SSC = Serversocketchannel.open ();
Ssc.socket (). bind (New Inetsocketaddress (Somelocalport));
Datagramchannel DC = Datagramchannel.open ();
Randomaccessfile RAF = new Randomaccessfile ("Somefile", "R");
FileChannel FC = Raf.getchannel ();

3.1.2 Using channels

1. Channel is for byte operation

2, Readonlubytechannel and Writablebytechannel are single-duplex, Bytechannle is full-duplex.

3, each file and socket Channle are implemented Bytechannel, supposedly should be full duplex, socket channel is indeed full duplex, but FileChannel is not necessarily.

We know that a file can be opened at different times with different permissions. The FileChannel object obtained from the Getchannel () method of the FileInputStream object is read-only, but is bidirectional from the point of view of the interface declaration, because FileChannel implements the Bytechannel interface. Calling the Write () method on such a channel throws an unchecked nonwritablechannelexception exception because the FileInputStream object is always read-only Permission to open the file.

4. can the channel be blocked? (blocking) or non-resistive? (nonblocking) mode operation. Non-resistive?? Mode of the channel?? Far from letting the calling thread????。 The requested operation either completes immediately or returns a result indicating that no action has been made. Only stream-oriented (stream-oriented) channels, such as sockets and pipes, can use non-blocking.

3.1.3 Closing the channel when you call the close () method of a channel, it may cause the process of closing the underlying I/O service on the channel to temporarily block the thread?? 7, even if the channel is non-resistive?? Mode. What is the resistance when the channel is closed? The behavior, if any, is highly dependent on the operating system or the file . The close () method is called multiple times on one channel. , but if the first thread is blocked in the close () method , any other call to the close () method will block until it finishes closing the channel??。 Subsequent call-up on the closed channel with close () will not produce any action and will only return immediately.

"JAVA NIO" Channel

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.