Apache-Mina (IoBuffer putString getString方法注意事项)

来源:互联网 发布:为不善乎显明之中者 编辑:程序博客网 时间:2024/06/05 18:56

IoBuffer在使用putString方法时,需要注意放入string后,需要手动放入结束标志位,就是putString后,再put一个byte.

因为IoBuffer的getString方法读取字符串时,会一直读取直到遇到null结尾或者读取到buffer最后的位置才算结束。null结尾转换成16进制,也就是00结尾,具体使用如下:

IoBuffer buf = IoBuffer.allocate(len, false);

buf.putInt(1);

buf.putString("hello");

buf.put(new byte[1]);

//上面的new byte[1]作为hello字符串的结束标志,byte[1]一个字节16进制两位00,在接收端调用buf.getString

//能够正确读取到hello,不然有可能会影响到下面put的放入值,导致读取粘连。发生异常。

buf.putXXX(xxx);

...


原创粉丝点击