GGraziadei commented on code in PR #8653:
URL: https://github.com/apache/storm/pull/8653#discussion_r3235177547


##########
storm-client/src/jvm/org/apache/storm/utils/Utils.java:
##########
@@ -960,6 +963,75 @@ public static byte[] gunzip(byte[] data) {
         }
     }
 
+    /**
+     * Static utility class for Zstandard (Zstd) compression and decompression.
+     */
+    public static final class ZstdUtils {
+
+        private static final int BUFFER_SIZE = 64 * 1024;
+
+        /**
+         * Private constructor to prevent instantiation.
+         * @throws UnsupportedOperationException if an attempt is made to 
instantiate this class.
+         */
+        private ZstdUtils() {
+            throw new UnsupportedOperationException("Utility class should not 
be instantiated.");
+        }
+
+        /**
+         * Compresses the provided byte array using Zstandard.
+         *
+         * <p>The output includes the standard Zstandard frame header, making 
it
+         * self-describing for the decompression phase.</p>
+         *
+         * @param data the raw byte array to compress.
+         * @return a compressed byte array, or the original array if 
null/empty.
+         * @throws RuntimeException wrapping an {@link IOException} if the 
compression fails.
+         */
+        public static byte[] compress(byte[] data) {
+            if (data == null || data.length == 0) {

Review Comment:
   Thank you. I am proposing an alternative to this method (now moved to the 
utility class).
   
   I found in the documentation a safe method to do the same comparison better: 
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/MethodHandles.html#byteArrayViewVarHandle(java.lang.Class,java.nio.ByteOrder)
   
   ```
   private static final int ZSTD_MAGIC_INT = 0xFD2FB528;
   
   private static final VarHandle INT_HANDLE = 
MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN);
   ...
   public static boolean isZstd(byte[] bytes) {
       if (bytes == null) {
           return false;
       }
       return (int) INT_HANDLE.get(bytes, 0) == ZSTD_MAGIC_INT;
   }
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to