Java nio Channel, javaniochannel

Source: Internet
Author: User

Java nio Channel, javaniochannel
Basic:Most channels are linked to the developed file descriptor. The Channel class provides an abstract process for maintaining platform independence. A channel is a way to access and operate the operating system. A buffer zone is a data operation point:Channel class inheritance structure:APIs are mainly designated by interfaces (interface-oriented). Channel implementation often requires operations on the code of the local operating system. Therefore, the implementation of different operating systems may be fundamentally different, the channel interface allows you to access the underlying I/O in a controllable and controllable manner.IO in two broad categories:File IO and Stream IO "=" channels: FileChannel (File channel) and SocketChannel-ServerSocketChannel-DatagramChannel (socket channel ). One-way and two-way channel: implement the WritableByteChannel write () method in ReadableByteChannel read () method =.ByteChannle is just an excuse to provide convenient inheritance. The syntax sugar of the class definition is simplified. All channels are bidirectional.The channel connects to a specific IO service, and the channel instance performance is limited by the features of the connected IO service.ByteChannel's read () and write () methods use ByteBuffer as parameters to return the number of transmitted bytes, which is less than the number of bytes in the buffer or may be 0, because one output is incomplete, the buffer goes forward with the same number of transmitted bytes. If only partial transmission is performed, the buffer can be resubmitted to the channel, continue transmission from the last interrupted location (based on the Buffer's positon attribute, continue to operate the data from postion to limit ). This process repeats until the Buffer. hasRemaining () method returns false.:

1/** 2 * File Replication operations based on basic channel buffer 3 */4 public static void fileTransferByNormal () {5 try {6 RandomAccessFile afile = new RandomAccessFile ("hello.txt ", "rw"); 7 RandomAccessFile bfile = new RandomAccessFile ("hehe.txt", "rw"); 8 FileChannel ac = afile. getChannel (); 9 FileChannel bc = bfile. getChannel (); 10 11 ByteBuffer bf = ByteBuffer. allocateDirect (16*1024); 12 while (ac. read (bf )! =-1) {13 bf. flip (); 14 while (bf. hasRemaining () {15 bc. write (bf); 16} 17 bf. clear (); 18} 19} catch (FileNotFoundException e) {20 e. printStackTrace (); 21} catch (IOException e) {22 e. printStackTrace (); 23} 24}

 

Blocking Nonblocking: the non-Blocking mode does not sleep the calling thread, either the request is completed immediately or an error is returned. Stream-oriented can be used to set nonblocking. Socket Channel inherits from SelectableChannel =, Selectors =, and multiplexing. (Non-blocking IO ).  Channel closed:The channel cannot be reused. The opened channel represents a specific link to a specific IO service and encapsulates the link. If the channel is closed, the link is lost. Multiple close () times on the channel, no effect. If a thread is blocked and interrupted, the channel is closed and the thread throws a ClosedByInterruptException exception. When the interrupt status of a thread is set and the thread view accesses a channel, the channel is closed and a ClosedByInterruptException exception is thrown. The interruption of sleep threads on the channel is different from that of sleep threads on selectors.   The Channel implementing InterruptableChannel can be closed at any time, and the sleep thread on the Channel will be awakened and an AsynchronousInterruptedException will be received. Disable asynchronous mode.

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.