1
1
package io .quarkus .vertx .utils ;
2
2
3
- import java .io .ByteArrayOutputStream ;
4
3
import java .io .IOException ;
5
4
import java .io .InterruptedIOException ;
6
5
import java .io .OutputStream ;
7
6
import java .util .Optional ;
8
7
8
+ import io .vertx .core .Context ;
9
9
import org .jboss .logging .Logger ;
10
10
11
11
import io .netty .buffer .ByteBuf ;
12
12
import io .netty .handler .codec .http .HttpHeaderNames ;
13
13
import io .netty .handler .codec .http .HttpResponse ;
14
14
import io .vertx .core .AsyncResult ;
15
15
import io .vertx .core .Handler ;
16
- import io .vertx .core .Vertx ;
17
16
import io .vertx .core .buffer .Buffer ;
18
17
import io .vertx .core .http .HttpServerRequest ;
19
18
import io .vertx .core .http .HttpServerResponse ;
20
- import io .vertx .core .http .impl .HttpServerRequestInternal ;
21
19
22
20
/**
23
21
* An {@link OutputStream} forwarding the bytes to Vert.x Web {@link HttpResponse}.
@@ -35,9 +33,7 @@ public class VertxOutputStream extends OutputStream {
35
33
private boolean committed ;
36
34
private boolean closed ;
37
35
private boolean waitingForDrain ;
38
- private boolean first = true ;
39
36
private Throwable throwable ;
40
- private ByteArrayOutputStream overflow ;
41
37
42
38
public VertxOutputStream (VertxJavaIoContext context ) {
43
39
this .context = context ;
@@ -87,29 +83,13 @@ private void write(ByteBuf data, boolean last) throws IOException {
87
83
//do all this in the same lock
88
84
synchronized (request .connection ()) {
89
85
try {
90
- boolean bufferRequired = awaitWriteable () || (overflow != null && overflow .size () > 0 );
91
- if (bufferRequired ) {
92
- //just buffer everything
93
- if (overflow == null ) {
94
- overflow = new ByteArrayOutputStream ();
86
+ awaitWriteable ();
87
+ if (last ) {
88
+ if (!response .ended ()) { // can happen when an exception occurs during JSON serialization with Jackson
89
+ response .end (createBuffer (data ), null );
95
90
}
96
- if (data .hasArray ()) {
97
- overflow .write (data .array (), data .arrayOffset () + data .readerIndex (), data .readableBytes ());
98
- } else {
99
- data .getBytes (data .readerIndex (), overflow , data .readableBytes ());
100
- }
101
- if (last ) {
102
- closed = true ;
103
- }
104
- data .release ();
105
91
} else {
106
- if (last ) {
107
- if (!response .ended ()) { // can happen when an exception occurs during JSON serialization with Jackson
108
- response .end (createBuffer (data ), null );
109
- }
110
- } else {
111
- response .write (createBuffer (data ), null );
112
- }
92
+ response .write (createBuffer (data ), null );
113
93
}
114
94
} catch (Exception e ) {
115
95
if (data != null && data .refCnt () > 0 ) {
@@ -120,13 +100,11 @@ private void write(ByteBuf data, boolean last) throws IOException {
120
100
}
121
101
}
122
102
123
- private boolean awaitWriteable () throws IOException {
124
- if (Vertx .currentContext () == ((HttpServerRequestInternal ) request ).context ()) {
125
- return false ; // we are on the (right) event loop, so we can write - Netty will do the right thing.
126
- }
127
- if (first ) {
128
- first = false ;
129
- return false ;
103
+ private void awaitWriteable () throws IOException {
104
+ // is it running in an event loop?
105
+ if (Context .isOnEventLoopThread ()) {
106
+ // NEVER block the event loop!
107
+ return ;
130
108
}
131
109
assert Thread .holdsLock (request .connection ());
132
110
while (response .writeQueueFull ()) {
@@ -136,7 +114,6 @@ private boolean awaitWriteable() throws IOException {
136
114
if (response .closed ()) {
137
115
throw new IOException ("Connection has been closed" );
138
116
}
139
- // registerDrainHandler();
140
117
try {
141
118
waitingForDrain = true ;
142
119
request .connection ().wait ();
@@ -146,7 +123,6 @@ private boolean awaitWriteable() throws IOException {
146
123
waitingForDrain = false ;
147
124
}
148
125
}
149
- return false ;
150
126
}
151
127
152
128
/**
@@ -258,16 +234,6 @@ public void handle(Void event) {
258
234
if (out .waitingForDrain ) {
259
235
out .request .connection ().notifyAll ();
260
236
}
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 ();
269
- }
270
- }
271
237
}
272
238
}
273
239
}
0 commit comments