Skip to content

Commit

Permalink
Filter empty buffers and arrays in ByteBufVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Feb 5, 2024
1 parent 2c73dca commit c215db8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ default boolean acceptsMemoryAddress(T context) {
*/
public static <T> void visitBuffers(ByteBuf buffer, int offset, int length, ByteBufVisitorCallback<T> callback,
T context, int maxDepth) {
if (length == 0) {
// skip visiting empty buffers
return;
}
InternalContext<T> internalContext = new InternalContext<>();
internalContext.maxDepth = maxDepth;
internalContext.callbackContext = context;
Expand All @@ -123,10 +127,6 @@ private static class InternalContext<T> {
GetBytesCallbackByteBuf<T> 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()) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c215db8

Please sign in to comment.