diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java index 3586be8ce48..1e78e4075eb 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java @@ -370,18 +370,14 @@ private class DigestUpdaterByteBufVisitorCallback implements ByteBufVisitor.Byte @Override public void visitBuffer(UpdateContext context, ByteBuf visitBuffer, int visitIndex, int visitLength) { - if (visitLength > 0) { - // recursively visit the sub buffer and update the digest - context.digest = internalUpdate(context.digest, visitBuffer, visitIndex, visitLength); - } + // recursively visit the sub buffer and update the digest + context.digest = internalUpdate(context.digest, visitBuffer, visitIndex, visitLength); } @Override public void visitArray(UpdateContext context, byte[] visitArray, int visitIndex, int visitLength) { - if (visitLength > 0) { - // update the digest with the array - context.digest = internalUpdate(context.digest, visitArray, visitIndex, visitLength); - } + // update the digest with the array + context.digest = internalUpdate(context.digest, visitArray, visitIndex, visitLength); } @Override diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufVisitor.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufVisitor.java index 858b098f934..32e9c8c55a4 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufVisitor.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufVisitor.java @@ -97,6 +97,10 @@ default boolean acceptsMemoryAddress(T context) { */ public static void visitBuffers(ByteBuf buffer, int offset, int length, ByteBufVisitorCallback callback, T context, int maxDepth) { + if (length == 0) { + // skip visiting empty buffers + return; + } InternalContext internalContext = new InternalContext<>(); internalContext.maxDepth = maxDepth; internalContext.callbackContext = context; @@ -123,10 +127,6 @@ private static class InternalContext { GetBytesCallbackByteBuf callbackByteBuf = new GetBytesCallbackByteBuf(this); void recursivelyVisitBuffers(ByteBuf visitBuffer, int visitIndex, int visitLength) { - if (visitLength == 0) { - // skip visiting empty buffers - return; - } // visit the wrapped buffers recursively if the buffer is not backed by an array or memory address // and the max depth has not been reached if (depth < maxDepth && !visitBuffer.hasMemoryAddress() && !visitBuffer.hasArray()) { @@ -143,6 +143,10 @@ void recursivelyVisitBuffers(ByteBuf visitBuffer, int visitIndex, int visitLengt } void handleBuffer(ByteBuf visitBuffer, int visitIndex, int visitLength) { + if (visitLength == 0) { + // skip visiting empty buffers + return; + } if (visitBuffer == parentBuffer && visitIndex == parentOffset && visitLength == parentLength) { // further recursion would cause unnecessary recursion up to the max depth of recursion passBufferToCallback(visitBuffer, visitIndex, visitLength); @@ -183,6 +187,10 @@ private void passBufferToCallback(ByteBuf visitBuffer, int visitIndex, int visit } void handleArray(byte[] visitArray, int visitIndex, int visitLength) { + if (visitLength == 0) { + // skip visiting empty arrays + return; + } // pass array to callback callback.visitArray(callbackContext, visitArray, visitIndex, visitLength); }