The example of this article summarizes the usage usage in Java. Share it for everyone for your reference. The specific analysis is as follows:
1. Definition of Channel interface:
Public interface channel {public boolean isopen (); public void close () throws IOEXCEPTION;}
2. Common types of Channel:
FileChannel, SocketChannel, ServersocketChannel, and DataGramchannel;
FileChannel is initialized through the getChannel () of RandomaccessFile, FileInputStream, FileoutPutstream.
SocketChannel sc = SocketChannel.open (); Sc.Connect ; ssc.socket (). = Datagramchannel.open ();
3.Scatter/Gather, must use bytebuffer.allocatedirect (100)
public interface ScatteringByteChannel extends ReadableByteChannel { public long read (ByteBuffer [] dsts) throws IOException; public long read (ByteBuffer [] dsts, int offset, int length) throws IOException;}public interface GatheringByteChannel extends WritableByteChannel { public long write(ByteBuffer[] srcs) Throws IOEXCEPION; Public Long Write (bytebuffer [] srcs, int Office, int Length) Throws IOEXCEPTION;}
4. File Lock is related to File, not Channel. It can be effective for processes, not threads. You can implement thread synchronization through memory-mapped file
5.buffer = FileChannel.map (FileChannel.mapmode.read_only, 100, 200);
6. Mappedbytebuffer are direct. Load () loads the entire file to memory (the modification method cannot be guaranteed). Force () puts data flush to the hard disk.
7. The DataGramChannel system of the unbound port will automatically allocate ports. DataGramchannel's Connect () will ensure that only data packets with specified source addresses will be guaranteed. At this time, you can use ordinary Read and Wing methods, including Scatter/Gather
It is hoped that this article is helpful to everyone's Java program design.