diff --git a/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/MessageUtil.java b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/MessageUtil.java new file mode 100644 index 0000000..0dadcb2 --- /dev/null +++ b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/util/MessageUtil.java @@ -0,0 +1,101 @@ +package com.klaro.acquirecore.framework.util; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * Static utility class for message encoding/decoding operations migrated from legacy C util_msg.c + */ +public final class MessageUtil { + + private MessageUtil() { + // Utility class - prevent instantiation + } + + public static byte[] create(String data) { + return data == null ? null : data.getBytes(StandardCharsets.UTF_8); + } + + public static byte[] create(byte[] data) { + if (data == null) return null; + byte[] result = new byte[data.length]; + System.arraycopy(data, 0, result, 0, data.length); + return result; + } + + public static String toHex(byte[] data) { + if (data == null) return null; + StringBuilder hex = new StringBuilder(data.length * 2); + for (byte b : data) { + hex.append(String.format("%02x", b & 0xFF)); + } + return hex.toString(); + } + + public static byte[] fromHex(String hex) { + if (hex == null || hex.isEmpty()) return null; + if (hex.length() % 2 != 0) throw new IllegalArgumentException("Hex string must have even length"); + int len = hex.length(); + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16); + } + return data; + } + + public static String toBase64(byte[] data) { + return data == null ? null : Base64.getEncoder().encodeToString(data); + } + + public static byte[] fromBase64(String base64) { + if (base64 == null || base64.isEmpty()) return null; + return Base64.getDecoder().decode(base64); + } + + public static int getLength(byte[] data) { + return data == null ? 0 : data.length; + } + + public static String toString(byte[] data) { + return data == null ? null : new String(data, StandardCharsets.UTF_8); + } + + public static byte[] concat(byte[] a, byte[] b) { + if (a == null && b == null) return null; + if (a == null) return create(b); + if (b == null) return create(a); + byte[] result = new byte[a.length + b.length]; + System.arraycopy(a, 0, result, 0, a.length); + System.arraycopy(b, 0, result, a.length, b.length); + return result; + } + + public static byte[] subarray(byte[] data, int offset, int length) { + if (data == null) return null; + if (offset < 0 || offset > data.length) throw new IllegalArgumentException("Invalid offset"); + if (length < 0 || offset + length > data.length) throw new IllegalArgumentException("Invalid length"); + byte[] result = new byte[length]; + System.arraycopy(data, offset, result, 0, length); + return result; + } + + public static boolean equals(byte[] a, byte[] b) { + if (a == null && b == null) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + for (int i = 0; i < a.length; i++) { + if (a[i] != b[i]) return false; + } + return true; + } + + public static byte[] fill(byte[] data, byte value) { + if (data == null) return null; + for (int i = 0; i < data.length; i++) data[i] = value; + return data; + } + + public static byte[] empty() { + return new byte[0]; + } +}