阅读代码:
ByteBuffer buffer = ByteBuffer.allocate(1024);
public String readLine() {
StringBuilder sb = new StringBuilder();
int k;
do {
buffer.clear();
k = socketChannel.read(buffer);
buffer.flip();
CharBuffer charBuff = charset.decode(buffer);
for(int i=0; i<charBuff.limit(); i++)
sb.append(charBuff.get());
} while(k == 1024);
return sb.toString().trim();
}
编写代码:
public void writeLine(String string) {
socketChannel.write(charset.encode(string));
}
我有一个问题,我的ByteBuffer坏了,我无法将其转换为JSON,编码为utf-8,当我写入或读取数据时没有错误
缓冲区大小 - 10 Kbyte = 10240字节。
那么如何发送和读取数据不丢失任何字符?