鸿蒙年度报告请查收!
2025/12/28 1:38:23
可以利用换行符进行切割
创建bytebuffer进行存储切割后的片段
@Slf4j public class ByteBuffer2Test { public static void main(String[] args) { ByteBuffer allocate = ByteBuffer.allocate(32); allocate.put("hello world\nheelo nihao\nhow".getBytes()); split(allocate); allocate.put(" are you\n".getBytes()); split(allocate); } public static void split(ByteBuffer byteBuffer) { byteBuffer.flip(); for (int i = 0; i < byteBuffer.limit(); i++) { byte b = byteBuffer.get(i); if (b == '\n') { int length = (i + 1) - byteBuffer.position(); ByteBuffer lineBuffer = ByteBuffer.allocate(length); for (int j = 0; j < length; j++) { lineBuffer.put(byteBuffer.get()); } lineBuffer.flip(); while (lineBuffer.hasRemaining()) { System.out.print((char) lineBuffer.get()); } } } byteBuffer.compact(); } }