sijie commented on a change in pull request #151: DL-45: DL should allow 
ByteBuf based API to avoid copying bytes array
URL: https://github.com/apache/distributedlog/pull/151#discussion_r135971007
 
 

 ##########
 File path: 
distributedlog-core/src/main/java/org/apache/distributedlog/EnvelopedEntryWriter.java
 ##########
 @@ -155,24 +157,45 @@ public synchronized int getNumRecords() {
     }
 
     @Override
-    public synchronized Buffer getBuffer() throws 
InvalidEnvelopedEntryException, IOException {
-        if (!envelopeBeforeTransmit) {
-            return buffer;
+    public synchronized ByteBuf getBuffer() throws 
InvalidEnvelopedEntryException, IOException {
+        if (null == finalizedBuffer) {
+            finalizedBuffer = finalizeBuffer();
         }
-        // We can't escape this allocation because things need to be read from 
one byte array
-        // and then written to another. This is the destination.
-        Buffer toSend = new Buffer(buffer.size());
-        byte[] decompressed = buffer.getData();
-        int length = buffer.size();
-        EnvelopedEntry entry = new 
EnvelopedEntry(EnvelopedEntry.CURRENT_VERSION,
-                                                  codec,
-                                                  decompressed,
-                                                  length,
-                                                  statsLogger);
-        // This will cause an allocation of a byte[] for compression. This can 
be avoided
-        // but we can do that later only if needed.
-        entry.writeFully(new DataOutputStream(toSend));
-        return toSend;
+        return finalizedBuffer.slice();
+    }
+
+    private ByteBuf finalizeBuffer() {
+       if (!envelopeBeforeTransmit) {
+            return buffer.retain();
+        }
+
+        int dataOffset = HEADER_LENGTH;
+        int dataLen = buffer.readableBytes() - HEADER_LENGTH;
+
+        if (Type.NONE == codec) {
+            // update version
+            buffer.setByte(0, CURRENT_VERSION);
 
 Review comment:
   added CONSTANTS for the offsets.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to