Class ByteUtilities

java.lang.Object
com.cedarsoftware.util.ByteUtilities

public final class ByteUtilities extends Object
A utility class providing static methods for operations on byte arrays and hexadecimal representations.

ByteUtilities simplifies common tasks such as encoding byte arrays to hexadecimal strings, decoding hexadecimal strings back to byte arrays, and identifying if a byte array represents GZIP-compressed data.

Key Features

  • Convert hexadecimal strings to byte arrays (decode(String)).
  • Convert byte arrays to hexadecimal strings (encode(byte[])).
  • Check if a byte array is GZIP-compressed (isGzipped(byte[])).
  • Internally optimized for performance with reusable utilities like convertDigit(int).

Usage Example


 // Encode a byte array to a hexadecimal string
 byte[] data = {0x1f, 0x8b, 0x3c};
 String hex = ByteUtilities.encode(data); // "1F8B3C"

 // Decode a hexadecimal string back to a byte array
 byte[] decoded = ByteUtilities.decode("1F8B3C"); // {0x1f, 0x8b, 0x3c}

 // Check if a byte array is GZIP-compressed
 boolean isGzip = ByteUtilities.isGzipped(data); // true
 

Design Notes

  • The class is designed as a utility class, and its constructor is private to prevent instantiation.
  • All methods are static and thread-safe, making them suitable for use in concurrent environments.
  • The decode method returns null for invalid inputs (e.g., strings with an odd number of characters).

Performance Considerations

The methods in this class are optimized for performance:

Author:
John DeRegnaucourt ([email protected]) Ken Partlow ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Method Details

    • decode

      public static byte[] decode(String s)
      Converts a hexadecimal string into a byte array.

      This method interprets each pair of characters in the input string as a hexadecimal number and converts it to the corresponding byte value. For example, the string "1F" is converted to the byte value 31 (decimal).

      Examples:

      
       byte[] bytes1 = ByteUtilities.decode("1F8B3C"); // Returns {0x1F, 0x8B, 0x3C}
       byte[] bytes2 = ByteUtilities.decode("FFFF");   // Returns {-1, -1}
       byte[] bytes3 = ByteUtilities.decode("1");      // Returns null (odd length)
       byte[] bytes4 = ByteUtilities.decode("");       // Returns empty byte array
       

      Requirements:

      • Input string must have an even number of characters
      • All characters must be valid hexadecimal digits (0-9, a-f, A-F)
      Parameters:
      s - the hexadecimal string to convert, may be empty but not null
      Returns:
      a byte array containing the decoded values, or null if:
      • the input string has an odd number of characters
      • the input string contains non-hexadecimal characters
      Throws:
      NullPointerException - if the input string is null
      See Also:
    • encode

      public static String encode(byte[] bytes)
      Convert a byte array into a printable format containing a String of hex digit characters (two per byte).
      Parameters:
      bytes - array representation
      Returns:
      String hex digits
    • isGzipped

      public static boolean isGzipped(byte[] bytes)
      Parameters:
      bytes - byte[] of bytes to test
      Returns:
      true if bytes are gzip compressed, false otherwise.