forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.java
More file actions
41 lines (31 loc) · 916 Bytes
/
Writer.java
File metadata and controls
41 lines (31 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.aofeng.demo.reactor;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* 负责向客户端发送响应数据。
*
* @author <a href="mailto:aofengblog@163.com">NieYong </a>
*/
public class Writer {
private SocketChannel _clientChannel;
private Object _data;
private Encoder _encoder;
public Writer(SocketChannel clientChannel, Object data) {
this._clientChannel = clientChannel;
this._data = data;
}
public void setEncoder(Encoder encoder) {
this._encoder = encoder;
}
public void write() throws IOException {
if (null == _data || !_clientChannel.isOpen()) {
return;
}
ByteBuffer buffer = _encoder.encode(_data);
if (null == buffer) {
return;
}
_clientChannel.write(buffer);
}
}