Skip to content

Commit dae2a96

Browse files
carterkozakfl4via
authored andcommitted
[UNDERTOW-2638] combine and comment
1 parent b5b4aa0 commit dae2a96

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,23 @@ long doWrite(final ByteBuffer[] srcs, int offset, int length) throws IOException
133133
if (anyAreSet(state, FLAG_WRITES_SHUTDOWN)) {
134134
throw new ClosedChannelException();
135135
}
136-
long totalRemaining = Buffers.remaining(srcs, offset, length);
136+
// Write as many buffers as possible without a chunk-size overflowing an integer.
137+
long totalRemaining = 0;
138+
for (int i = 0; i < length; i++) {
139+
ByteBuffer buf = srcs[i + offset];
140+
int remaining = buf.remaining();
141+
if (totalRemaining + remaining > Integer.MAX_VALUE) {
142+
// Avoid producing chunks too large for clients by reducing the number of buffers
143+
// until total remaining fits within a 32-bit signed integer value. This is safe
144+
// because a single java ByteBuffer has a capacity represented by an integer.
145+
length = i;
146+
break;
147+
}
148+
totalRemaining += remaining;
149+
}
137150
if(totalRemaining == 0) {
138151
return 0;
139152
}
140-
if (totalRemaining > Integer.MAX_VALUE) {
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;
150-
}
151-
total += remaining;
152-
}
153-
}
154153
int remaining = (int) totalRemaining;
155154
this.state |= FLAG_FIRST_DATA_WRITTEN;
156155
int oldLimit = srcs[length - 1].limit();

0 commit comments

Comments
 (0)