How to perform a zero-copy upload and download with WebClient?

后端 未结 1 1847
深忆病人
深忆病人 2020-12-29 14:44

Can you perform a zero-copy upload and download with Spring 5 WebFlux using org.springframework.web.reactive.function.client.WebClient?

相关标签:
1条回答
  • 2020-12-29 15:07

    You're right, zero-copy is supported for now when posting data from a File-based Resource.

    So the following looks right:

    client.post()
          .body(BodyInserters.fromResource(new FileSystemResource(new File("file.txt"))));
    

    Now for the reading part, zero-copy is not supported on the reading side right now in Spring Framework; you could create an enhancement issue on jira.spring.io for that.

    Your code sample should look like:

    Flux<DataBuffer> incoming = client.post()
          .retrieve().bodyToFlux(DataBuffer.class);
    Mono<Void> writeOperation = DataBufferUtils.write(incoming, channel)
          .map(DataBufferUtils::release)
          .then();
    // subscribe to the returned value, which will complete when writing is done
    

    Unfortunately, reading data to DataBuffer won't do zero-copy as data will be copied in memory. I don't think zero-copy is supported right know on the reading side, so this could be an enhancement request on https://jira.spring.io.

    0 讨论(0)
提交回复
热议问题