Package com.cedarsoftware.util
Class ByteUtilities
java.lang.Object
com.cedarsoftware.util.ByteUtilities
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
toHexChar(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 returnsnull
for invalid inputs (e.g., strings with an odd number of characters).
Performance Considerations
The methods in this class are optimized for performance:
encode(byte[])
avoids excessive memory allocations by pre-sizing theStringBuilder
.decode(String)
uses minimal overhead to parse hexadecimal strings into bytes.
- 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 TypeMethodDescriptionstatic 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.
-
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
Converts a hexadecimal string into a byte array. Returns null if the string length is odd or any character is non-hex. -
encode
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.
-