您的位置:首页 > Web前端

NIO学习小结--Channel and Buffer

2011-08-01 16:32 281 查看
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息