diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.java index e5c635b85f46..c5de1bc7e26b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.java @@ -19,6 +19,8 @@ import java.io.IOException; import java.io.Writer; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; import java.util.function.Supplier; import org.reactivestreams.Publisher; @@ -39,7 +41,9 @@ class FluxWriter extends Writer { private final Charset charset; - private Flux buffers; + private List current = new ArrayList<>(); + + private List accumulated = new ArrayList<>(); public FluxWriter(Supplier factory) { this(factory, Charset.defaultCharset()); @@ -48,17 +52,32 @@ public FluxWriter(Supplier factory) { public FluxWriter(Supplier factory, Charset charset) { this.factory = factory; this.charset = charset; - this.buffers = Flux.empty(); } public Publisher> getBuffers() { - return this.buffers - .map(string -> Mono.just(buffer().write(string, this.charset))); + Flux buffers = Flux.empty(); + if (!this.current.isEmpty()) { + this.accumulated.add(new ArrayList<>(this.current)); + this.current.clear(); + } + for (Object thing : this.accumulated) { + if (thing instanceof Publisher) { + @SuppressWarnings("unchecked") + Publisher publisher = (Publisher) thing; + buffers = buffers.concatWith(publisher); + } + else { + @SuppressWarnings("unchecked") + List list = (List) thing; + buffers = buffers.concatWithValues(list.toArray(new String[0])); + } + } + return buffers.map(string -> Mono.just(buffer().write(string, this.charset))); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - this.buffers = this.buffers.concatWith(Mono.just(new String(cbuf, off, len))); + this.current.add(new String(cbuf, off, len)); } @Override @@ -79,13 +98,15 @@ private DataBuffer buffer() { public void write(Object thing) { if (thing instanceof Publisher) { - @SuppressWarnings("unchecked") - Publisher publisher = (Publisher) thing; - this.buffers = this.buffers.concatWith(Flux.from(publisher)); + if (!this.current.isEmpty()) { + this.accumulated.add(new ArrayList<>(this.current)); + this.current.clear(); + } + this.accumulated.add(thing); } else { if (thing instanceof String) { - this.buffers = this.buffers.concatWith(Mono.just((String) thing)); + this.current.add((String) thing); } } }