Browse Source

Deferred buffer allocation in SSE message writer

See gh-26746
pull/26752/head
Rossen Stoyanchev 5 years ago
parent
commit
7dc3a55648
  1. 17
      spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java

17
spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.http.codec;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration; import java.time.Duration;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -171,12 +170,14 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
if (this.encoder == null) { if (this.encoder == null) {
throw new CodecException("No SSE encoder configured and the data is not String."); throw new CodecException("No SSE encoder configured and the data is not String.");
} }
DataBuffer buffer = ((Encoder<T>) this.encoder).encodeValue(data, factory, dataType, mediaType, hints);
Hints.touchDataBuffer(buffer, hints, logger); return Flux.defer(() -> {
return Flux.just(factory.join(Arrays.asList( DataBuffer startBuffer = encodeText(eventContent, mediaType, factory);
encodeText(eventContent, mediaType, factory), DataBuffer endBuffer = encodeText("\n\n", mediaType, factory);
buffer, DataBuffer dataBuffer = ((Encoder<T>) this.encoder).encodeValue(data, factory, dataType, mediaType, hints);
encodeText("\n\n", mediaType, factory)))); Hints.touchDataBuffer(dataBuffer, hints, logger);
return Flux.just(startBuffer, dataBuffer, endBuffer);
});
} }
private void writeField(String fieldName, Object fieldValue, StringBuilder sb) { private void writeField(String fieldName, Object fieldValue, StringBuilder sb) {

Loading…
Cancel
Save