You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by GitBox <gi...@apache.org> on 2020/06/09 15:56:19 UTC

[GitHub] [zookeeper] symat commented on a change in pull request #1350: Pre-defined the size of ByteArrayOutputStream

symat commented on a change in pull request #1350:
URL: https://github.com/apache/zookeeper/pull/1350#discussion_r436714137



##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Learner.java
##########
@@ -246,18 +246,23 @@ void request(Request request) throws IOException {
             LOG.error("Throttled request sent to leader: {}. Exiting", request);
             ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue());
         }
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        DataOutputStream oa = new DataOutputStream(baos);
-        oa.writeLong(request.sessionId);
-        oa.writeInt(request.cxid);
-        oa.writeInt(request.type);
-        if (request.request != null) {
+        // size of sessionId, cxId and type in bytes
+        int size = Long.BYTES + 2 * Integer.BYTES;
+        byte[] bytes = null;
+        if(request.request != null){
             request.request.rewind();
             int len = request.request.remaining();
             byte[] b = new byte[len];
             request.request.get(b);
-            request.request.rewind();
-            oa.write(b);
+            size = size + len;
+        }
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
+        DataOutputStream oa = new DataOutputStream(baos);
+        oa.writeLong(request.sessionId);
+        oa.writeInt(request.cxid);
+        oa.writeInt(request.type);
+        if (bytes != null) {

Review comment:
       in your code `bytes` is always `null` now, you never set it.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org