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

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 Summary

    Modifier and Type
    Method
    Description
    static byte[]
    Converts a hexadecimal string into a byte array.
    static String
    encode(byte[] bytes)
    Converts a byte array into a string of hex digits.
    static boolean
    isGzipped(byte[] bytes)
    Checks if the byte array represents gzip-compressed data.
    static char
    toHexChar(int value)
    Convert the specified value (0 .. 15) to the corresponding hex digit.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toHexChar

      public static char toHexChar(int value)
      Convert the specified value (0 .. 15) to the corresponding hex digit.
      Parameters:
      value - to be converted
      Returns:
      '0'...'F' in char format.
    • decode

      public static byte[] decode(String s)
      Converts a hexadecimal string into a byte array. Returns null if the string length is odd or any character is non-hex.
    • encode

      public static String encode(byte[] bytes)
      Converts a byte array into a string of hex digits.
    • isGzipped

      public static boolean isGzipped(byte[] bytes)
      Checks if the byte array represents gzip-compressed data.