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();}
- NIO学习小结--Channel and Buffer
- java NIO 之 buffer and channel
- java NIO 之 buffer and channel(2)
- 002 java nio 01 - channel and buffer
- 【NIO】1. Channel、Buffer
- NIO之Buffer channel
- NIO之Buffer、Channel
- 《Java NIO》:Channel and Buffer (通道和缓冲区)
- 《Java NIO》:Channel and Buffer (通道和缓冲区)
- Java NIO之Channel和Buffer学习记录
- 深入浅出NIO Channel和Buffer
- 深入浅出NIO Channel和Buffer
- NIO学习笔记(3)--NIO核心类(Buffer,Channel)
- NIO:与 Buffer 一起使用 Channel
- Java之NIO(一)Channel和Buffer
- NIO的Buffer类族和Channel
- JAVA NIO : Buffer ,Channel, Selector,Pipe
- Java NIO(1)----Channel 和 Buffer
- DropDownList中指定任意的选中项
- JS 操作 Excle
- 记录集对象的BookMark 属性的使用
- Android自动化测试初探
- Visual age C++ On AIX 安装
- NIO学习小结--Channel and Buffer
- diff & patch 命令用法
- html语义化
- zoj 3511 Cake Robbery
- 数据绑定(java类与ActionScript类绑定)
- ajaxFileUpload ajax上传文件!jquery插件
- vmware下linux上网设置
- 理解C++中的enum
- 数据对象剪贴板传输,COleDataSource和COleDataObject用法