ramanathan1504 commented on code in PR #4336:
URL: https://github.com/apache/logging-log4j2/pull/4336#discussion_r4060294114


##########
src/changelog/.2.x.x/4333_grow_datagram_output_stream_buffer.xml:
##########
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+           https://logging.apache.org/xml/ns
+           https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+  <issue id="4333" 
link="https://github.com/apache/logging-log4j2/issues/4333"/>

Review Comment:
   ```suggestion
     <issue id="4333" 
link="https://github.com/apache/logging-log4j2/issues/4333"/>
     <issue id="4336" 
link="https://github.com/apache/logging-log4j2/pull/4336"/>
   ```



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/net/DatagramOutputStream.java:
##########
@@ -121,12 +134,6 @@ public synchronized void close() throws IOException {
     }
 
     private void copy(final byte[] bytes, final int offset, final int length) {
-        final int index = data == null ? 0 : data.length;
-        final byte[] copy = new byte[length + index];
-        if (data != null) {
-            System.arraycopy(data, 0, copy, 0, data.length);
-        }
-        System.arraycopy(bytes, offset, copy, index, length);
-        data = copy;
+        data.write(bytes, offset, length);
     }

Review Comment:
   ```java
   private long droppedLength;
   
   private void copy(final byte[] bytes, final int offset, final int length) {
       if (droppedLength > 0 || data.size() + length > MAX_DATAGRAM_LENGTH) {
           droppedLength = (droppedLength > 0 ? droppedLength : data.size()) + 
length;
           return;
       }
       data.write(bytes, offset, length);
   }
   ```
   and in `flush()`:
   ```java
   if (droppedLength > 0) {
       LOGGER.warn(
               "UDP datagram of {} bytes exceeds the maximum of {} bytes and 
will be dropped",
               droppedLength,
               MAX_DATAGRAM_LENGTH);
   } else {
       datagramSocket.send(new DatagramPacket(data.toByteArray(), data.size(), 
inetAddress, port));
   }
   ...
   } finally {
       data.reset();
       droppedLength = 0;
       ...
   ```
   - The guard in `flush()` becomes `(data.size() > 0 || droppedLength > 0)`.
   - The existing test keeps passing as is (73,728 bytes → WARN containing 
"exceeds", nothing received).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to