Skip to content

Commit 0c74932

Browse files
committed
Net-client: compact the buffer if possible instead of resizing
1 parent bae41fa commit 0c74932

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ public void run() {
236236
// todo: at some point buffer should stop increasing in size,
237237
// otherwise it could use up all the memory.
238238
if (bufferLast == buffer.length) {
239-
byte temp[] = new byte[bufferLast << 1];
240-
System.arraycopy(buffer, 0, temp, 0, bufferLast);
241-
buffer = temp;
239+
if (bufferIndex > 0) {
240+
// compact the buffer
241+
int bufferLength = bufferLast - bufferIndex;
242+
System.arraycopy(buffer, bufferIndex, buffer, 0, bufferLength);
243+
bufferLast -= bufferIndex;
244+
bufferIndex = 0;
245+
} else {
246+
// resize the buffer
247+
byte temp[] = new byte[bufferLast << 1];
248+
System.arraycopy(buffer, 0, temp, 0, bufferLast);
249+
buffer = temp;
250+
}
242251
}
243252
buffer[bufferLast++] = (byte)value;
244253
}

0 commit comments

Comments
 (0)