简体   繁体   中英

SocketChannel how to deal with cutted UTF-8 chars and lines in ByteBuffer

My system is receiving UTF-8 lines ended by 0x0D 0x0A through a SocketChannel. I read data in a ByteBuffer like this:

final ByteBuffer buffer = ByteBuffer.allocate(bufsize);
final int bytesRead = _mySocket.read(buffer);

The buffer is big enough to contain a lot of lines. But I can't have a buffer big enough to contain all lines, lines are processed on the fly.

The buffer can't contain all the lines, so lines may be splitted, a character encoded in more that one byte may also be splitted.

I thought about a solution:

  1. process all lines that I can extract by analyzing the ByteBuffer byte by byte (I search 0x0D 0x0A)
  2. identify unprocessed bytes
  3. store them in a temporary variable
  4. clear the ByteBuffer
  5. add unprocessed bytes to the ByteBuffer
  6. read again the SocketChannel.
  7. Repeat this until I have found the line indicating I received all the lines.

Does java or any library already provide this functionnality ?

Thanks for any answer, Mickaël

我建议使用java.util.Scanner,它知道如何处理SocketChannel,并且知道如何读取行

    Scanner sc = new Scanner(_mySocket);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM