resultparser.java
来自「利用Java Socket写的一段通讯协议」· Java 代码 · 共 80 行
JAVA
80 行
package com.ict.netcom2.parser;
import com.ict.netcom2.result.*;
import com.ict.netcom2.task.*;
public class ResultParser {
public static MeasureResult parseMesureResult(int taskType, byte[] result) {
if (taskType == TaskType.HTTP_PERF) {
return parseHttpPerfResult(result);
}
else if (taskType == TaskType.PASSIVE_ETH) {
return parsePasvEthResult(result);
}
return null;
}
private static HttpPerfResult parseHttpPerfResult(byte[] result) {
HttpPerfResult ret = new HttpPerfResult();
String str = new String(result);
//str format: rstcode$testtime^connecttime^usrconntime^responsetime^indexgettime^usrgettime^downloadrate
//str looks like "0$1156144267^1^396^142^143^143^ 167.067^"
int index = str.indexOf("$");
String tmp = str.substring(0, index);
str = str.substring(index+1);
ret.rstCode = Integer.parseInt(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.testTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.connectTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.usrConnTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.responseTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.indexGotTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
str = str.substring(index+1);
ret.usrGotTime = Long.parseLong(tmp);
index = str.indexOf("^");
tmp = str.substring(0, index);
tmp = tmp.trim();
ret.downloadRate = Float.parseFloat(tmp);
return ret;
}
private static PasvEthResult parsePasvEthResult(byte[] result) {
PasvEthResult ret = new PasvEthResult();
ret.storeResult(result);
return ret;
}
public static void main(String[] args) {
System.out.println(parseHttpPerfResult(
(new String("0$1156144267^1^396^142^143^143^ 167.067^"))
.getBytes()));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?