Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a26158c

Browse files
rstoyanchevDave Syer
authored and
Dave Syer
committed
Polish in FluxWriter and MustacheView
1 parent 8b75b8f commit a26158c

File tree

2 files changed

+30
-47
lines changed

2 files changed

+30
-47
lines changed

‎spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/FluxWriter.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import java.nio.charset.Charset;
2222
import java.util.ArrayList;
2323
import java.util.List;
24-
import java.util.function.Supplier;
2524

2625
import org.reactivestreams.Publisher;
2726
import reactor.core.publisher.Flux;
2827
import reactor.core.publisher.Mono;
2928

3029
import org.springframework.core.io.buffer.DataBuffer;
30+
import org.springframework.core.io.buffer.DataBufferFactory;
3131

3232
/**
3333
* A {@link Writer} that can write a {@link Flux} (or {@link Publisher}) to a data buffer.
@@ -37,47 +37,46 @@
3737
*/
3838
class FluxWriter extends Writer {
3939

40-
private final Supplier<DataBuffer> factory;
40+
private final DataBufferFactory factory;
4141

4242
private final Charset charset;
4343

44-
private List<String> current = new ArrayList<>();
45-
4644
private List<Object> accumulated = new ArrayList<>();
4745

48-
FluxWriter(Supplier<DataBuffer> factory) {
49-
this(factory, Charset.defaultCharset());
50-
}
51-
52-
FluxWriter(Supplier<DataBuffer> factory, Charset charset) {
46+
FluxWriter(DataBufferFactory factory, Charset charset) {
5347
this.factory = factory;
5448
this.charset = charset;
5549
}
5650

57-
public Publisher<? extends Publisher<? extends DataBuffer>> getBuffers() {
51+
@SuppressWarnings("unchecked")
52+
public Flux<? extends Publisher<? extends DataBuffer>> getBuffers() {
5853
Flux<String> buffers = Flux.empty();
59-
if (!this.current.isEmpty()) {
60-
this.accumulated.add(new ArrayList<>(this.current));
61-
this.current.clear();
62-
}
54+
List<String> chunks = new ArrayList<>();
6355
for (Object thing : this.accumulated) {
6456
if (thing instanceof Publisher) {
65-
@SuppressWarnings("unchecked")
66-
Publisher<String> publisher = (Publisher<String>) thing;
67-
buffers = buffers.concatWith(publisher);
57+
buffers = concatValues(chunks, buffers);
58+
buffers = buffers.concatWith((Publisher<String>) thing);
6859
}
6960
else {
70-
@SuppressWarnings("unchecked")
71-
List<String> list = (List<String>) thing;
72-
buffers = buffers.concatWithValues(list.toArray(new String[0]));
61+
chunks.add((String) thing);
7362
}
7463
}
75-
return buffers.map((string) -> Mono.just(buffer().write(string, this.charset)));
64+
buffers = concatValues(chunks, buffers);
65+
return buffers.map((string) -> Mono.fromCallable(() ->
66+
this.factory.allocateBuffer().write(string, this.charset)));
67+
}
68+
69+
private Flux<String> concatValues(List<String> chunks, Flux<String> buffers) {
70+
if (!chunks.isEmpty()) {
71+
buffers = buffers.concatWithValues(chunks.toArray(new String[0]));
72+
chunks.clear();
73+
}
74+
return buffers;
7675
}
7776

7877
@Override
7978
public void write(char[] cbuf, int off, int len) throws IOException {
80-
this.current.add(new String(cbuf, off, len));
79+
this.accumulated.add(new String(cbuf, off, len));
8180
}
8281

8382
@Override
@@ -92,23 +91,8 @@ public void release() {
9291
// TODO: maybe implement this and call it on error
9392
}
9493

95-
private DataBuffer buffer() {
96-
return this.factory.get();
97-
}
98-
9994
public void write(Object thing) {
100-
if (thing instanceof Publisher) {
101-
if (!this.current.isEmpty()) {
102-
this.accumulated.add(new ArrayList<>(this.current));
103-
this.current.clear();
104-
}
105-
this.accumulated.add(thing);
106-
}
107-
else {
108-
if (thing instanceof String) {
109-
this.current.add((String) thing);
110-
}
111-
}
95+
this.accumulated.add(thing);
11296
}
11397

11498
}

‎spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/result/view/MustacheView.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.springframework.core.io.Resource;
4242
import org.springframework.http.MediaType;
43+
import org.springframework.http.server.reactive.ServerHttpResponse;
4344
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
4445
import org.springframework.web.reactive.result.view.View;
4546
import org.springframework.web.server.ServerWebExchange;
@@ -101,8 +102,8 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
101102
}
102103
boolean sse = MediaType.TEXT_EVENT_STREAM.isCompatibleWith(contentType);
103104
Charset charset = getCharset(contentType).orElse(getDefaultCharset());
104-
FluxWriterwriter = newFluxWriter(
105-
() -> exchange.getResponse().bufferFactory().allocateBuffer(), charset);
105+
ServerHttpResponseresponse = exchange.getResponse();
106+
FluxWriterwriter = newFluxWriter(response.bufferFactory(), charset);
106107
Mono<Template> rendered;
107108
if (!this.cache || !templates.containsKey(resource)) {
108109
rendered = Mono.fromCallable(() -> compile(resource))
@@ -119,11 +120,10 @@ protected Mono<Void> renderInternal(Map<String, Object> model, MediaType content
119120
else {
120121
map = model;
121122
}
122-
rendered = rendered.doOnSuccess((template) -> template.execute(map, writer));
123-
return rendered
124-
.thenEmpty(Mono.defer(() -> exchange.getResponse()
125-
.writeAndFlushWith(Flux.from(writer.getBuffers()))))
126-
.doOnTerminate(() -> close(writer));
123+
return rendered.flatMap((template) -> {
124+
template.execute(map, writer);
125+
return response.writeAndFlushWith(writer.getBuffers());
126+
}).doOnTerminate(() -> close(writer));
127127
}
128128

129129
private void close(FluxWriter writer) {
@@ -199,8 +199,7 @@ public void execute(Fragment frag, Writer out) throws IOException {
199199
if (out instanceof FluxWriter) {
200200
FluxWriter fluxWriter = (FluxWriter) out;
201201
fluxWriter.flush();
202-
fluxWriter.write(Flux.from(this.publisher)
203-
.map((value) -> frag.execute(value)));
202+
fluxWriter.write(Flux.from(this.publisher).map(frag::execute));
204203
}
205204
}
206205
catch (IOException ex) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /