Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled BufferUnderflowException on header deserialization #1588

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading