Repository navigation
Expand file tree
/
Copy pathPingRouter.java
More file actions
49 lines (41 loc) · 1.68 KB
/
Copy pathPingRouter.java
File metadata and controls
49 lines (41 loc) · 1.68 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
package server;
import com.modulecode.net.IConnection;
import com.modulecode.net.IRequest;
import com.modulecode.net.IRouter;
import com.modulecode.net.impl.BaseRouter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class PingRouter extends BaseRouter {
@Override
public void preHandle(IRequest request) throws IOException {
// IConnection connection = request.getConnection();
// connection.getTCPSocket().getOutputStream().write("preHandle\n".getBytes());
}
@Override
public void handle(IRequest request) throws IOException {
byte[] data = request.getData();
String binary = binary(data, 2);
IConnection connection = request.getConnection();
DataOutputStream dataOutputStream = new DataOutputStream(connection.getTCPSocket().getOutputStream());
dataOutputStream.writeInt(binary.getBytes(StandardCharsets.UTF_8).length);
dataOutputStream.write(binary.getBytes());
}
/**
* 将byte[]转为各种进制的字符串
*
* @param bytes byte[]
* @param radix 基数可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
* @return 转换后的字符串
*/
public String binary(byte[] bytes, int radix) {
return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
}
@Override
public void postHandle(IRequest request) throws IOException {
// IConnection connection = request.getConnection();
// connection.getTCPSocket().getOutputStream().write("postHandle\n".getBytes());
}
}