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
Security Configuration
ByteUtilities provides configurable security options through system properties. All security features are disabled by default for backward compatibility:
byteutilities.security.enabled=false
— Master switch to enable all security featuresbyteutilities.max.hex.string.length=0
— Hex string length limit for decode operations (0=disabled)byteutilities.max.array.size=0
— Byte array size limit for encode operations (0=disabled)
Example Usage:
// Enable security with default limits
System.setProperty("byteutilities.security.enabled", "true");
// Or enable with custom limits
System.setProperty("byteutilities.security.enabled", "true");
System.setProperty("byteutilities.max.hex.string.length", "10000");
System.setProperty("byteutilities.max.array.size", "1000000");
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 CharSequence into a byte array.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 boolean
isGzipped
(byte[] bytes, int offset) Checks if the byte array represents gzip-compressed data starting at the given offset.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.- Parameters:
s
- the hexadecimal string to decode- Returns:
- the decoded byte array, or null if input is null, has odd length, or contains non-hex characters
-
decode
Converts a hexadecimal CharSequence into a byte array.- Parameters:
s
- the hexadecimal CharSequence to decode- Returns:
- the decoded byte array, or null if input is null, has odd length, or contains non-hex characters
-
encode
Converts a byte array into a string of hex digits.- Parameters:
bytes
- the byte array to encode- Returns:
- the hexadecimal string representation, or null if input is null
-
isGzipped
public static boolean isGzipped(byte[] bytes) Checks if the byte array represents gzip-compressed data. -
isGzipped
public static boolean isGzipped(byte[] bytes, int offset) Checks if the byte array represents gzip-compressed data starting at the given offset.- Parameters:
bytes
- the byte array to inspectoffset
- the starting offset within the array- Returns:
- true if the bytes appear to be GZIP compressed, false if bytes is null, offset is invalid, or not enough bytes
-