forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineDecoder.java
More file actions
50 lines (43 loc) · 1.48 KB
/
LineDecoder.java
File metadata and controls
50 lines (43 loc) · 1.48 KB
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
42
43
44
45
46
47
48
49
50
package cn.aofeng.demo.reactor;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 行数据解析器。
*
* @author <a href="mailto:aofengblog@163.com">NieYong </a>
*/
public class LineDecoder implements Decoder {
private final static Logger _logger = Logger.getLogger(LineDecoder.class.getName());
/**
* 从字节缓冲区中获取"一行"。
*
* @param buffer 输入缓冲区
* @return 有遇到行结束符,返回不包括行结束符的字符串;否则返回null。
*/
@Override
public String decode(ByteBuffer source) {
int index = 0;
boolean findCR = false;
int len = source.limit();
byte[] bytes = source.array();
while(index < len) {
index ++;
byte temp = bytes[index-1];
if (Constant.CR == temp) {
findCR = true;
}
if (Constant.LF == temp && findCR) { // 找到了行结束符
byte[] copy = new byte[index];
System.arraycopy(bytes, 0, copy, 0, index);
try {
return new String(copy, Constant.CHARSET_UTF8);
} catch (UnsupportedEncodingException e) {
_logger.log(Level.SEVERE, "将解析完成的请求数据转换成字符串出错", e);
}
}
}
return null;
}
}