private void writeData(List<TsBlock> sortedData, String fileName) throws IoTDBException {
    Path filePath = Paths.get(fileName);
    // for stream sort we may reuse the previous tmp file name, so we need TRUNCATE_EXISTING and
    // CREATE
    try (FileChannel fileChannel =
        FileChannel.open(
            filePath,
            StandardOpenOption.WRITE,
            StandardOpenOption.TRUNCATE_EXISTING,
            StandardOpenOption.CREATE)) {
      for (TsBlock tsBlock : sortedData) {
        ByteBuffer tsBlockBuffer = serde.serialize(tsBlock);
        ByteBuffer length = ByteBuffer.allocate(4);
        length.putInt(tsBlockBuffer.capacity());
        length.flip();
        fileChannel.write(length);
        fileChannel.write(tsBlockBuffer);
      }
    } catch (IOException e) {
      throw new IoTDBException(
          "Can't write intermediate sorted data to file: " + fileName,
          e,
          TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
    }
  }
todo: directly serialize the sorted line instead of copy into a new tsBlock.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/sort/DiskSpiller.java