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:
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(); }
相关文章推荐
- NIO学习:使用Channel、Buffer写入文件
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- NIO学习笔记(1)-Buffer,Channel
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- Java nio 学习笔记(一) Buffer(缓冲区)与Channel(通道)的相关知识
- 002 java nio 01 - channel and buffer
- netty学习之ChannelSink(NioClientSocketPipelineSink)
- 转载:Java NIO 学习笔记 - ByteBuffer
- java.nio.ByteBuffer用法小结
- java NIO中的buffer和channel
- Nio再学习之NIO的buffer缓冲区
- Java IO系统——NIO之Buffer、Channel和Charset类
- JAVA NIO : Buffer ,Channel, Selector
- NIO之Buffer channel
- NIO学习(二) buffer
- JAVA NIO : Buffer ,Channel, Selector,Pipe
- java NIO 之 buffer and channel(2)
- Java NIO 学习笔记 - ByteBuffer (早期笔记)
- Netty学习之NIO---通道Channel(一)