Skip to content

Commit 86440d0

Browse files
committed
improve performance by removing unnecessary flushes
1 parent 707d7e3 commit 86440d0

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/main/java/robaho/net/httpserver/ChunkedOutputStream.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ public void close() throws IOException {
132132
if (closed) {
133133
return;
134134
}
135-
flush();
135+
if (count > 0) {
136+
writeChunk();
137+
}
136138
try {
137139
/* write an empty chunk */
138140
writeChunk();

src/main/java/robaho/net/httpserver/ExchangeImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ public void sendResponseHeaders(int rCode, long contentLen)
239239
getConnection().getSocket().setOption(StandardSocketOptions.SO_SNDBUF, bufferSize);
240240
}
241241

242+
boolean flush = false;
243+
242244
/* check for response type that is not allowed to send a body */
243245
if (rCode == 101) {
244246
logger.log(Level.DEBUG, () -> "switching protocols");
@@ -249,6 +251,7 @@ public void sendResponseHeaders(int rCode, long contentLen)
249251
logger.log(Level.WARNING, msg);
250252
}
251253
contentLen = 0;
254+
flush = true;
252255

253256
} else if ((rCode >= 100 && rCode < 200) /* informational */
254257
|| (rCode == 204) /* no content */
@@ -280,6 +283,7 @@ public void sendResponseHeaders(int rCode, long contentLen)
280283
if (websocket || isConnectRequest()) {
281284
o.setWrappedStream(ros);
282285
close = true;
286+
flush = true;
283287
}
284288
else if (http10) {
285289
o.setWrappedStream(new UndefLengthOutputStream(this, ros));
@@ -323,7 +327,7 @@ else if (http10) {
323327
if(logger.isLoggable(Level.TRACE)) {
324328
logger.log(Level.TRACE, "Sent headers: noContentToSend=" + noContentToSend);
325329
}
326-
if(contentLen==0) {
330+
if(flush) {
327331
ros.flush();
328332
}
329333
if (noContentToSend) {

0 commit comments

Comments
 (0)