1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
29
29
import org .springframework .core .codec .Encoder ;
30
30
import org .springframework .core .codec .Hints ;
31
31
import org .springframework .core .io .buffer .DataBuffer ;
32
+ import org .springframework .core .io .buffer .DataBufferUtils ;
32
33
import org .springframework .core .io .buffer .PooledDataBuffer ;
33
- import org .springframework .http .HttpHeaders ;
34
34
import org .springframework .http .HttpLogging ;
35
35
import org .springframework .http .MediaType ;
36
36
import org .springframework .http .ReactiveHttpOutputMessage ;
@@ -108,7 +108,6 @@ public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaTyp
108
108
return this .encoder .canEncode (elementType , mediaType );
109
109
}
110
110
111
- @ SuppressWarnings ("unchecked" )
112
111
@ Override
113
112
public Mono <Void > write (Publisher <? extends T > inputStream , ResolvableType elementType ,
114
113
@ Nullable MediaType mediaType , ReactiveHttpOutputMessage message , Map <String , Object > hints ) {
@@ -119,23 +118,23 @@ public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType eleme
119
118
inputStream , message .bufferFactory (), elementType , contentType , hints );
120
119
121
120
if (inputStream instanceof Mono ) {
122
- HttpHeaders headers = message .getHeaders ();
123
121
return body
124
122
.singleOrEmpty ()
125
123
.switchIfEmpty (Mono .defer (() -> {
126
- headers .setContentLength (0 );
124
+ message . getHeaders () .setContentLength (0 );
127
125
return message .setComplete ().then (Mono .empty ());
128
126
}))
129
127
.flatMap (buffer -> {
130
- headers .setContentLength (buffer .readableByteCount ());
128
+ message . getHeaders () .setContentLength (buffer .readableByteCount ());
131
129
return message .writeWith (Mono .just (buffer )
132
- .doOnDiscard (PooledDataBuffer .class , PooledDataBuffer ::release ));
133
- });
130
+ .doOnDiscard (PooledDataBuffer .class , DataBufferUtils ::release ));
131
+ })
132
+ .doOnDiscard (PooledDataBuffer .class , DataBufferUtils ::release );
134
133
}
135
134
136
135
if (isStreamingMediaType (contentType )) {
137
136
return message .writeAndFlushWith (body .map (buffer ->
138
- Mono .just (buffer ).doOnDiscard (PooledDataBuffer .class , PooledDataBuffer ::release )));
137
+ Mono .just (buffer ).doOnDiscard (PooledDataBuffer .class , DataBufferUtils ::release )));
139
138
}
140
139
141
140
return message .writeWith (body );
0 commit comments