public static String readCMDFromSocket(InputStream in) {
// 8M
int MAX_BUFFER_BYTES = 1024 * 1024*8;
String msg = "";
byte[] tempbuffer = new byte[MAX_BUFFER_BYTES];
try {
int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");
// 报文总长度
long msgLenth = 0;
// 已经读取的长度
long readLenth = numReadedBytes;
if (!"".equals(msg)) {
msgLenth = Long.valueOf(msg.substring(0, 8));
msg = msg.substring(8, msg.length());
while (readLenth < msgLenth) {
// 一直读取
int newReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
msg += new String(tempbuffer, 0, newReadedBytes, "utf-8");
// 直到已经读取的总长度等于总长度,不再读取
readLenth
android从Socket流读取文件数据,按照头标记拆分byte数组
最新推荐文章于 2023-04-24 16:46:49 发布