18
18
19
19
import java .io .IOException ;
20
20
import java .io .InputStream ;
21
- import java .io .UncheckedIOException ;
22
21
import java .nio .charset .Charset ;
23
22
import java .util .List ;
24
23
import java .util .Map ;
@@ -52,6 +51,8 @@ public class ServletServerHttpResponse extends AbstractListenerServerHttpRespons
52
51
53
52
private final HttpServletResponse response ;
54
53
54
+ private final ServletOutputStream outputStream ;
55
+
55
56
private final int bufferSize ;
56
57
57
58
@ Nullable
@@ -73,6 +74,7 @@ public ServletServerHttpResponse(HttpServletResponse response, AsyncContext asyn
73
74
Assert .isTrue (bufferSize > 0 , "Buffer size must be greater than 0" );
74
75
75
76
this .response = response ;
77
+ this .outputStream = response .getOutputStream ();
76
78
this .bufferSize = bufferSize ;
77
79
78
80
asyncContext .addListener (new ResponseAsyncListener ());
@@ -147,7 +149,7 @@ protected Processor<? super Publisher<? extends DataBuffer>, Void> createBodyFlu
147
149
* @return the number of bytes written
148
150
*/
149
151
protected int writeToOutputStream (DataBuffer dataBuffer ) throws IOException {
150
- ServletOutputStream outputStream = response . getOutputStream () ;
152
+ ServletOutputStream outputStream = this . outputStream ;
151
153
InputStream input = dataBuffer .asInputStream ();
152
154
int bytesWritten = 0 ;
153
155
byte [] buffer = new byte [this .bufferSize ];
@@ -160,7 +162,7 @@ protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
160
162
}
161
163
162
164
private void flush () throws IOException {
163
- ServletOutputStream outputStream = this .response . getOutputStream () ;
165
+ ServletOutputStream outputStream = this .outputStream ;
164
166
if (outputStream .isReady ()) {
165
167
try {
166
168
outputStream .flush ();
@@ -176,6 +178,10 @@ private void flush() throws IOException {
176
178
}
177
179
}
178
180
181
+ private boolean isWritePossible () {
182
+ return this .outputStream .isReady ();
183
+ }
184
+
179
185
180
186
private final class ResponseAsyncListener implements AsyncListener {
181
187
@@ -233,6 +239,12 @@ public void onWritePossible() throws IOException {
233
239
if (processor != null ) {
234
240
processor .onWritePossible ();
235
241
}
242
+ else {
243
+ ResponseBodyFlushProcessor flushProcessor = bodyFlushProcessor ;
244
+ if (flushProcessor != null ) {
245
+ flushProcessor .onFlushPossible ();
246
+ }
247
+ }
236
248
}
237
249
238
250
@ Override
@@ -242,6 +254,13 @@ public void onError(Throwable ex) {
242
254
processor .cancel ();
243
255
processor .onError (ex );
244
256
}
257
+ else {
258
+ ResponseBodyFlushProcessor flushProcessor = bodyFlushProcessor ;
259
+ if (flushProcessor != null ) {
260
+ flushProcessor .cancel ();
261
+ flushProcessor .onError (ex );
262
+ }
263
+ }
245
264
}
246
265
}
247
266
@@ -250,15 +269,9 @@ private class ResponseBodyFlushProcessor extends AbstractListenerWriteFlushProce
250
269
251
270
@ Override
252
271
protected Processor <? super DataBuffer , Void > createWriteProcessor () {
253
- try {
254
- ServletOutputStream outputStream = response .getOutputStream ();
255
- ResponseBodyProcessor processor = new ResponseBodyProcessor (outputStream );
256
- bodyProcessor = processor ;
257
- return processor ;
258
- }
259
- catch (IOException ex ) {
260
- throw new UncheckedIOException (ex );
261
- }
272
+ ResponseBodyProcessor processor = new ResponseBodyProcessor ();
273
+ bodyProcessor = processor ;
274
+ return processor ;
262
275
}
263
276
264
277
@ Override
@@ -268,20 +281,24 @@ protected void flush() throws IOException {
268
281
}
269
282
ServletServerHttpResponse .this .flush ();
270
283
}
271
- }
272
284
285
+ @ Override
286
+ protected boolean isWritePossible () {
287
+ return ServletServerHttpResponse .this .isWritePossible ();
288
+ }
273
289
274
- private class ResponseBodyProcessor extends AbstractListenerWriteProcessor <DataBuffer > {
290
+ @ Override
291
+ protected boolean isFlushPending () {
292
+ return flushOnNext ;
293
+ }
294
+ }
275
295
276
- private final ServletOutputStream outputStream ;
277
296
278
- public ResponseBodyProcessor (ServletOutputStream outputStream ) {
279
- this .outputStream = outputStream ;
280
- }
297
+ private class ResponseBodyProcessor extends AbstractListenerWriteProcessor <DataBuffer > {
281
298
282
299
@ Override
283
300
protected boolean isWritePossible () {
284
- return this .outputStream . isReady ();
301
+ return ServletServerHttpResponse . this .isWritePossible ();
285
302
}
286
303
287
304
@ Override
@@ -306,7 +323,7 @@ protected boolean write(DataBuffer dataBuffer) throws IOException {
306
323
}
307
324
flush ();
308
325
}
309
- boolean ready = this .outputStream . isReady ();
326
+ boolean ready = ServletServerHttpResponse . this .isWritePossible ();
310
327
if (this .logger .isTraceEnabled ()) {
311
328
this .logger .trace ("write: " + dataBuffer + " ready: " + ready );
312
329
}
0 commit comments