-
-
Notifications
You must be signed in to change notification settings - Fork 196
BE: Messages: Format non-string headers #1077
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91629c6
ISSUE-919 Best efforts to convert header into string
germanosin 02959bb
Fixed sonar issues
germanosin 1a841d7
Performance improvements
germanosin d25b87d
fixes in the code
germanosin a7374bf
Merge branch 'main' into issue/919
germanosin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package io.kafbat.ui.util; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.nio.CharBuffer; | ||
| import java.nio.charset.CharsetDecoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Inspired by: https://github.com/tchiotludo/akhq/blob/dev/src/main/java/org/akhq/utils/ContentUtils.java | ||
| */ | ||
| public class ContentUtils { | ||
| private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); | ||
|
|
||
| private static final CharsetDecoder UTF8_DECODER = StandardCharsets.UTF_8.newDecoder(); | ||
|
|
||
| private ContentUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Detects if bytes contain a UTF-8 string or something else. | ||
| * @param value the bytes to test for a UTF-8 encoded {@code java.lang.String} value | ||
| * @return true, if the byte[] contains a UTF-8 encode {@code java.lang.String} | ||
| */ | ||
| public static boolean isValidUtf8(byte[] value) { | ||
| // Any data exceeding 10KB will be treated as a string. | ||
| if (value.length > 10_000) { | ||
| return true; | ||
| } | ||
| try { | ||
| CharBuffer decode = UTF8_DECODER.decode(ByteBuffer.wrap(value)); | ||
| return decode.chars().allMatch(ContentUtils::isValidUtf8); | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean isValidUtf8(int c) { | ||
| // SKIP NULL Symbols | ||
| if (c == 0) { | ||
| return false; | ||
| } | ||
| // Well known symbols | ||
| if (Character.isAlphabetic(c) | ||
| || Character.isDigit(c) | ||
| || Character.isWhitespace(c) | ||
| || Character.isEmoji(c) | ||
| ) { | ||
| return true; | ||
| } | ||
| // We could read only whitespace controls like | ||
| return !Character.isISOControl(c); | ||
| } | ||
|
|
||
| /** | ||
| * Converts bytes to long. | ||
| * | ||
| * @param value the bytes to convert in to a long | ||
| * @return the long build from the given bytes | ||
| */ | ||
| public static Long asLong(byte[] value) { | ||
| return value != null ? ByteBuffer.wrap(value).getLong() : null; | ||
| } | ||
|
|
||
| /** | ||
| * Converts the given bytes to {@code int}. | ||
| * | ||
| * @param value the bytes to convert into a {@code int} | ||
| * @return the {@code int} build from the given bytes | ||
| */ | ||
| public static Integer asInt(byte[] value) { | ||
| return value != null ? ByteBuffer.wrap(value).getInt() : null; | ||
| } | ||
|
|
||
| /** | ||
| * Converts the given bytes to {@code short}. | ||
| * | ||
| * @param value the bytes to convert into a {@code short} | ||
| * @return the {@code short} build from the given bytes | ||
| */ | ||
| public static Short asShort(byte[] value) { | ||
| return value != null ? ByteBuffer.wrap(value).getShort() : null; | ||
| } | ||
|
|
||
| /** | ||
| * Converts the given bytes either into a {@code java.lang.string}, {@code int}, | ||
| * {@code long} or {@code short} depending on the content it contains. | ||
| * @param value the bytes to convert | ||
| * @return the value as an {@code java.lang.string}, {@code int}, {@code long} or {@code short} | ||
| */ | ||
| public static String convertToString(byte[] value) { | ||
| String valueAsString = null; | ||
|
|
||
| if (value != null) { | ||
| try { | ||
| if (ContentUtils.isValidUtf8(value)) { | ||
| valueAsString = new String(value); | ||
| } else { | ||
| if (value.length == Long.BYTES) { | ||
| valueAsString = String.valueOf(ContentUtils.asLong(value)); | ||
| } else if (value.length == Integer.BYTES) { | ||
| valueAsString = String.valueOf(ContentUtils.asInt(value)); | ||
| } else if (value.length == Short.BYTES) { | ||
| valueAsString = String.valueOf(ContentUtils.asShort(value)); | ||
| } else { | ||
| valueAsString = bytesToHex(value); | ||
| } | ||
| } | ||
| } catch (Exception ex) { | ||
| // Show the header as hexadecimal string | ||
| valueAsString = bytesToHex(value); | ||
| } | ||
| } | ||
| return valueAsString; | ||
| } | ||
|
|
||
| // 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); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package io.kafbat.ui.util; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ContentUtilsTest { | ||
|
|
||
| private static byte[] toBytes(Short value) { | ||
| ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); | ||
| buffer.putShort(value); | ||
| return buffer.array(); | ||
| } | ||
|
|
||
| private static byte[] toBytes(Integer value) { | ||
| ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); | ||
| buffer.putInt(value); | ||
| return buffer.array(); | ||
| } | ||
|
|
||
| private static byte[] toBytes(Long value) { | ||
| ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); | ||
| buffer.putLong(value); | ||
| return buffer.array(); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderValueStringUtf8() { | ||
| String testValue = "Test"; | ||
|
|
||
| assertEquals(testValue, ContentUtils.convertToString(testValue.getBytes(StandardCharsets.UTF_8))); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderValueInteger() { | ||
| int testValue = 1; | ||
| assertEquals(String.valueOf(testValue), ContentUtils.convertToString(toBytes(testValue))); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderValueLong() { | ||
| long testValue = 111L; | ||
|
|
||
| assertEquals(String.valueOf(testValue), ContentUtils.convertToString(toBytes(testValue))); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderValueShort() { | ||
| short testValue = 10; | ||
|
|
||
| assertEquals(String.valueOf(testValue), ContentUtils.convertToString(toBytes(testValue))); | ||
| } | ||
|
|
||
| @Test | ||
| void testHeaderValueLongStringUtf8() { | ||
| String testValue = RandomStringUtils.random(10000, true, false); | ||
|
|
||
| assertEquals(testValue, ContentUtils.convertToString(testValue.getBytes(StandardCharsets.UTF_8))); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.