Skip to content

Commit 6bee8b7

Browse files
authored
Merge pull request #48923 from geoand/polish-VertxOutputStream
Polish VertxOutputStream
2 parents 0e3d9c9 + 02c5412 commit 6bee8b7

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

independent-projects/vertx-utils/src/main/java/io/quarkus/vertx/utils/VertxOutputStream.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class VertxOutputStream extends OutputStream {
3030
private final VertxJavaIoContext context;
3131
private final HttpServerRequest request;
3232
private final AppendBuffer appendBuffer;
33+
private final HttpServerResponse response;
3334

3435
private boolean committed;
3536
private boolean closed;
@@ -44,7 +45,8 @@ public VertxOutputStream(VertxJavaIoContext context) {
4445
this.appendBuffer = AppendBuffer.withMinChunks(
4546
context.getMinChunkSize(),
4647
context.getOutputBufferCapacity());
47-
request.response().exceptionHandler(new Handler<Throwable>() {
48+
response = request.response();
49+
response.exceptionHandler(new Handler<>() {
4850
@Override
4951
public void handle(Throwable event) {
5052
throwable = event;
@@ -58,10 +60,10 @@ public void handle(Throwable event) {
5860
}
5961
});
6062
Handler<Void> handler = new DrainHandler(this);
61-
request.response().drainHandler(handler);
62-
request.response().closeHandler(handler);
63+
response.drainHandler(handler);
64+
response.closeHandler(handler);
6365

64-
context.getRoutingContext().addEndHandler(new Handler<AsyncResult<Void>>() {
66+
context.getRoutingContext().addEndHandler(new Handler<>() {
6567
@Override
6668
public void handle(AsyncResult<Void> event) {
6769
synchronized (request.connection()) {
@@ -79,7 +81,7 @@ private Buffer createBuffer(ByteBuf data) {
7981

8082
private void write(ByteBuf data, boolean last) throws IOException {
8183
if (last && data == null) {
82-
request.response().end((Handler<AsyncResult<Void>>) null);
84+
response.end((Handler<AsyncResult<Void>>) null);
8385
return;
8486
}
8587
//do all this in the same lock
@@ -102,11 +104,11 @@ private void write(ByteBuf data, boolean last) throws IOException {
102104
data.release();
103105
} else {
104106
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);
107109
}
108110
} else {
109-
request.response().write(createBuffer(data), null);
111+
response.write(createBuffer(data), null);
110112
}
111113
}
112114
} catch (Exception e) {
@@ -127,11 +129,11 @@ private boolean awaitWriteable() throws IOException {
127129
return false;
128130
}
129131
assert Thread.holdsLock(request.connection());
130-
while (request.response().writeQueueFull()) {
132+
while (response.writeQueueFull()) {
131133
if (throwable != null) {
132134
throw new IOException(throwable);
133135
}
134-
if (request.response().closed()) {
136+
if (response.closed()) {
135137
throw new IOException("Connection has been closed");
136138
}
137139
// registerDrainHandler();
@@ -196,7 +198,6 @@ private void writeBlocking(ByteBuf buffer, boolean finished) throws IOException
196198
private void prepareWrite(ByteBuf buffer, boolean finished) throws IOException {
197199
if (!committed) {
198200
committed = true;
199-
final HttpServerResponse response = request.response();
200201
if (finished) {
201202
if (!response.headWritten()) {
202203
if (buffer == null) {
@@ -249,30 +250,25 @@ public void close() throws IOException {
249250
}
250251
}
251252

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> {
258254

259255
@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();
271269
}
272-
out.overflow.reset();
273270
}
274271
}
275272
}
276273
}
277-
}
278274
}

0 commit comments

Comments
 (0)