Skip to content

Commit

Permalink
fix(topicdata): BufferUnderflowException on header deserialization (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisSouquiere authored Nov 19, 2023
1 parent 468df43 commit 81c036c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/akhq/utils/ContentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

public class ContentUtils {
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);

/**
* Detects if bytes contain a UTF-8 string or something else
Expand Down Expand Up @@ -87,11 +89,22 @@ public static Object convertToObject(byte[] value) {
}
}
}
} catch(UnsupportedEncodingException ex) {
valueAsObject = "[encoding error]";
} catch(Exception ex) {
// Show the header as hexadecimal string
valueAsObject = bytesToHex(value);
}
}
return valueAsObject;
}

// https://stackoverflow.com/questions/9655181/java-convert-a-byte-array-to-a-hex-string
public static String bytesToHex(byte[] bytes) {
byte[] hexChars = new byte[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars, StandardCharsets.UTF_8);
}
}

0 comments on commit 81c036c

Please sign in to comment.