NIO学习小结--Channel and Buffer

来源:互联网 发布:ce引擎源码 编辑:程序博客网 时间:2024/04/29 21:24

Channels:

A Java channel represents an open data path, possibly bidirectional, to an external data source or sink such as a file or a socket.

Channel interfaces and methods:

1.Channel interface only has close() and isOpen();

2.ReadableByteChannel and WritableByteChannel interfaces extend from Channel, and export read() and Write() methods to operate to Data in or out .

3.The ByteChannel interface unifies the readable and writable interfaces(so ofen used);

4 ScatteringByteChannel and GatheringByteChannel we can get the function through the metheds it exports:i.e. read(ByteBuffer[] targets).see? it makes us can deal with multiple buffers.

5 InterruptibleChannel may be closed asynchronously.

How to Obtain a channel:

1.FileChannel FileInputStream.getChannel(); (FileOutputStream, RandomAccessFile are just in the same way)

2.SocketChannel channel = SocketChannel.open();

    Socket socket = channel.socket(); 

 channel from a Socket, ServerSocket, or DatagramSocket,only if the socket was created from a channel, so the operation is circular.

3.Pipe pipe = Pipe.open();

   Pipe.SinkChannel sinkChannel = pipe.sink();

   Pipe.SourceChannel sourceChannel = pipe.source();

Buffer:

A channel can only perform input-output in conjunction with a buffer.Make sure the four interdependent state attributes satisfy the invariant:

Attributes:

(a) Capacity  is the number of elements it contains. What this represents in bytes depends on the size of the datatype supported by the buffer. The capacity is immutable: it is fixed when the buffer is created.

(b) Llimit of a buffer is the index of the first element that should not be read or written. The limit is mutable.

(c) Position of a buffer is the index of the next element that should be read or written. The position is mutable.

(d) Mark of a buffer is the index to which its position will be restored if its method is invoked. The mark is mutable: it is not always defined but it can be defined and subsequently modified by program operations. It is undefined when a buffer is first created. The mark is discarded (,, becomes undefined) if the position or the limit is adjusted to be less than the current mark.

0<=mark<=position<=limit<=capacity

Methods:

clear(): set position to zero and limit to capacity to make a buffer reuse;

flip():flip the buffer's mode from put/read to get/write and set limit to position and position to zero at same time;

rewind():set position to zero and does not alter limit;

compact():move remaining data to the head of buffer and set position to the last of the valid data and limit to capacity;

Simple demo FileCopy:

public static void copy(String src, String dest) throws IOException{FileChannel cin = new FileInputStream(src).getChannel();FileChannel cout = new FileOutputStream(dest).getChannel();ByteBuffer buf = ByteBuffer.allocate(1024);buf.clear();while(cin.read(buf) > 0 ){buf.flip();cout.write(buf);buf.clear();}