我正在研究 java.nio 无阻塞客户端服务器应用程序,该应用程序有一个非常简单的自定义协议:
MyApplcationProtocol:
int lenpalcket;
int filedone;
String Description
byte[] myBodyProtcol
(体长不固定。)
问题是有时我在 ByteBuffer 中没有足够的字节,然后解码失败
while (myByteBuffer.hasRemaining()) {
Utils.fillMyClass(myclass,myByteBuffer);
public void fillMyClass(myclass,myByteBuffer){
myclass.filedone = myByteBuffer.getInt();
...
int bodyLen = myByteBuffer.getInt();
byte[] bodyByte = new byte[bodyLen];
myByteBuffer.get(bodyByte);
...
}
我必须等待下一个缓冲区完成填充类,所以我想避免因为它阻塞线程而进入睡眠状态。是否有解决此问题的模式/方式/链接/示例?
我还尝试将“未读字节”存储在临时缓冲区中,但我不喜欢它,有时我无法正常工作。
谢谢,任何帮助表示赞赏