Skip to content

Commit b5b4aa0

Browse files
carterkozakfl4via
authored andcommitted
[UNDERTOW-2638] Batch buffers in the overflow case
1 parent d08ab8c commit b5b4aa0

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

core/src/main/java/io/undertow/conduits/ChunkedStreamSinkConduit.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,18 @@ long doWrite(final ByteBuffer[] srcs, int offset, int length) throws IOException
138138
return 0;
139139
}
140140
if (totalRemaining > Integer.MAX_VALUE) {
141-
// Fall back to one buffer at a time if the total srcs remaining exceeds integer max-value
142-
// This should be very rare.
143-
for (int i = offset; i < offset + length; i++) {
144-
ByteBuffer buf = srcs[i];
145-
if (buf.hasRemaining()) {
146-
return write(buf);
141+
// Write as many buffers as possible without a chunk-size overflowing an integer.
142+
long total = 0;
143+
for (int i = 0; i < length; i++) {
144+
ByteBuffer buf = srcs[i + offset];
145+
int remaining = buf.remaining();
146+
if (total + remaining > Integer.MAX_VALUE) {
147+
length = i;
148+
totalRemaining = total;
149+
break;
147150
}
151+
total += remaining;
148152
}
149-
return 0;
150153
}
151154
int remaining = (int) totalRemaining;
152155
this.state |= FLAG_FIRST_DATA_WRITTEN;
@@ -205,7 +208,6 @@ long doWrite(final ByteBuffer[] srcs, int offset, int length) throws IOException
205208
} else {
206209
result = next.write(buf, 0, buf.length);
207210
}
208-
// TODO(ckozak): Buffers.hasRemaining fast-path?
209211
if (Buffers.remaining(srcs, offset, length) == 0) {
210212
state |= FLAG_WRITES_SHUTDOWN;
211213
}

0 commit comments

Comments
 (0)