@@ -30,6 +30,7 @@ public class VertxOutputStream extends OutputStream {
30
30
private final VertxJavaIoContext context ;
31
31
private final HttpServerRequest request ;
32
32
private final AppendBuffer appendBuffer ;
33
+ private final HttpServerResponse response ;
33
34
34
35
private boolean committed ;
35
36
private boolean closed ;
@@ -44,7 +45,8 @@ public VertxOutputStream(VertxJavaIoContext context) {
44
45
this .appendBuffer = AppendBuffer .withMinChunks (
45
46
context .getMinChunkSize (),
46
47
context .getOutputBufferCapacity ());
47
- request .response ().exceptionHandler (new Handler <Throwable >() {
48
+ response = request .response ();
49
+ response .exceptionHandler (new Handler <>() {
48
50
@ Override
49
51
public void handle (Throwable event ) {
50
52
throwable = event ;
@@ -58,10 +60,10 @@ public void handle(Throwable event) {
58
60
}
59
61
});
60
62
Handler <Void > handler = new DrainHandler (this );
61
- request . response () .drainHandler (handler );
62
- request . response () .closeHandler (handler );
63
+ response .drainHandler (handler );
64
+ response .closeHandler (handler );
63
65
64
- context .getRoutingContext ().addEndHandler (new Handler <AsyncResult < Void > >() {
66
+ context .getRoutingContext ().addEndHandler (new Handler <>() {
65
67
@ Override
66
68
public void handle (AsyncResult <Void > event ) {
67
69
synchronized (request .connection ()) {
@@ -79,7 +81,7 @@ private Buffer createBuffer(ByteBuf data) {
79
81
80
82
private void write (ByteBuf data , boolean last ) throws IOException {
81
83
if (last && data == null ) {
82
- request . response () .end ((Handler <AsyncResult <Void >>) null );
84
+ response .end ((Handler <AsyncResult <Void >>) null );
83
85
return ;
84
86
}
85
87
//do all this in the same lock
@@ -102,11 +104,11 @@ private void write(ByteBuf data, boolean last) throws IOException {
102
104
data .release ();
103
105
} else {
104
106
if (last ) {
105
- if (!request . response () .ended ()) { // can happen when an exception occurs during JSON serialization with Jackson
106
- request . response () .end (createBuffer (data ), null );
107
+ if (!response .ended ()) { // can happen when an exception occurs during JSON serialization with Jackson
108
+ response .end (createBuffer (data ), null );
107
109
}
108
110
} else {
109
- request . response () .write (createBuffer (data ), null );
111
+ response .write (createBuffer (data ), null );
110
112
}
111
113
}
112
114
} catch (Exception e ) {
@@ -127,11 +129,11 @@ private boolean awaitWriteable() throws IOException {
127
129
return false ;
128
130
}
129
131
assert Thread .holdsLock (request .connection ());
130
- while (request . response () .writeQueueFull ()) {
132
+ while (response .writeQueueFull ()) {
131
133
if (throwable != null ) {
132
134
throw new IOException (throwable );
133
135
}
134
- if (request . response () .closed ()) {
136
+ if (response .closed ()) {
135
137
throw new IOException ("Connection has been closed" );
136
138
}
137
139
// registerDrainHandler();
@@ -196,7 +198,6 @@ private void writeBlocking(ByteBuf buffer, boolean finished) throws IOException
196
198
private void prepareWrite (ByteBuf buffer , boolean finished ) throws IOException {
197
199
if (!committed ) {
198
200
committed = true ;
199
- final HttpServerResponse response = request .response ();
200
201
if (finished ) {
201
202
if (!response .headWritten ()) {
202
203
if (buffer == null ) {
@@ -249,30 +250,25 @@ public void close() throws IOException {
249
250
}
250
251
}
251
252
252
- private static class DrainHandler implements Handler <Void > {
253
- private final VertxOutputStream out ;
254
-
255
- public DrainHandler (VertxOutputStream out ) {
256
- this .out = out ;
257
- }
253
+ private record DrainHandler (VertxOutputStream out ) implements Handler <Void > {
258
254
259
255
@ Override
260
- public void handle (Void event ) {
261
- synchronized (out .request .connection ()) {
262
- if (out .waitingForDrain ) {
263
- out .request .connection ().notifyAll ();
264
- }
265
- if (out .overflow != null ) {
266
- if (out .overflow .size () > 0 ) {
267
- if (out .closed ) {
268
- out .request .response ().end (Buffer .buffer (out .overflow .toByteArray ()), null );
269
- } else {
270
- out .request .response ().write (Buffer .buffer (out .overflow .toByteArray ()), null );
256
+ public void handle (Void event ) {
257
+ synchronized (out .request .connection ()) {
258
+ if (out .waitingForDrain ) {
259
+ out .request .connection ().notifyAll ();
260
+ }
261
+ if (out .overflow != null ) {
262
+ if (out .overflow .size () > 0 ) {
263
+ if (out .closed ) {
264
+ out .response .end (Buffer .buffer (out .overflow .toByteArray ()), null );
265
+ } else {
266
+ out .response .write (Buffer .buffer (out .overflow .toByteArray ()), null );
267
+ }
268
+ out .overflow .reset ();
271
269
}
272
- out .overflow .reset ();
273
270
}
274
271
}
275
272
}
276
273
}
277
- }
278
274
}
0 commit comments