Skip to content

Commit e3faddb

Browse files
committed
Do not allocate temp byte array
1 parent ee5be68 commit e3faddb

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/MemoryEndPointPipe.java

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -173,67 +173,50 @@ else if (filled < 0)
173173
return filled;
174174
}
175175

176-
private int fillInto(ByteBuffer buffer)
176+
private int fillInto(ByteBuffer dest)
177177
{
178178
int filled = 0;
179-
try (AutoLock ignored = lock.lock())
179+
int pos = BufferUtil.flipToFill(dest);
180+
try
180181
{
181-
while (true)
182+
try (AutoLock ignored = lock.lock())
182183
{
183-
RetainableByteBuffer data = buffers.peek();
184-
if (data == null)
185-
return filled;
186-
if (data == EOF)
187-
return filled > 0 ? filled : -1;
188-
189-
int sizeBefore = (int)data.size();
190-
int copied = copyTo(data, buffer);
191-
capacity -= copied;
192-
filled += copied;
193-
194-
if (copied < sizeBefore)
184+
while (true)
195185
{
196-
// Destination buffer is full, can't copy more
197-
return filled;
186+
RetainableByteBuffer data = buffers.peek();
187+
if (data == null)
188+
return filled;
189+
if (data == EOF)
190+
return filled > 0 ? filled : -1;
191+
192+
int space = dest.remaining();
193+
if (space == 0)
194+
return filled;
195+
196+
int available = (int)data.size();
197+
int toCopy = Math.min(space, available);
198+
199+
if (toCopy == available)
200+
{
201+
// Copy all and consume
202+
data.putTo(dest);
203+
data.release();
204+
buffers.poll();
205+
}
206+
else
207+
{
208+
// Partial copy using slice
209+
RetainableByteBuffer slice = data.slice(toCopy);
210+
slice.putTo(dest);
211+
slice.release();
212+
data.skip(toCopy);
213+
}
214+
215+
capacity -= toCopy;
216+
filled += toCopy;
198217
}
199-
200-
// Fully consumed this buffer, release and remove
201-
data.release();
202-
buffers.poll();
203218
}
204219
}
205-
}
206-
207-
/**
208-
* Copy data from source RetainableByteBuffer to destination ByteBuffer.
209-
* Uses flipToFill/flipToFlush to handle buffer state, allowing
210-
* the buffer to be reused across multiple fill calls.
211-
*
212-
* @param src the source buffer to copy from
213-
* @param dest the destination buffer to copy to
214-
* @return the number of bytes copied
215-
*/
216-
private int copyTo(RetainableByteBuffer src, ByteBuffer dest)
217-
{
218-
// flipToFill must be called first to prepare the buffer,
219-
// especially when position == limit (buffer fully consumed)
220-
int pos = BufferUtil.flipToFill(dest);
221-
try
222-
{
223-
int space = dest.remaining();
224-
if (space == 0)
225-
return 0;
226-
227-
int toCopy = (int)Math.min(space, src.size());
228-
if (toCopy == 0)
229-
return 0;
230-
231-
byte[] temp = new byte[toCopy];
232-
int read = src.get(temp, 0, toCopy);
233-
dest.put(temp, 0, read);
234-
235-
return read;
236-
}
237220
finally
238221
{
239222
BufferUtil.flipToFlush(dest, pos);

0 commit comments

Comments
 (0)