Skip to content

Bytes

Overview

Module

Byte-buffer conversions that don't fit cleanly into a single JDK call — text-to-bytes with a named charset and integer / byte-array conversion with explicit byte order. The byte-order constants BIG_ENDIAN and LITTLE_ENDIAN match the values the underlying facade accepts; use them as the byteOrder argument rather than passing raw strings.

For straight ASCII / UTF-8 conversion you can use org.eclipse.dirigible.sdk.utils.Utf8 or the standard library directly; this class earns its keep when you actually need a specific charset or a known endianness.

Key Features:

  • Charset-aware text to bytes: Convert a String to a byte[] with the platform default or a named charset.
  • Endian-aware integer conversion: Pack and unpack int values to/from byte arrays in big- or little-endian order.
  • Byte-order constants: BIG_ENDIAN and LITTLE_ENDIAN string constants for use as the byteOrder argument.

Example Usage:

java
import org.eclipse.dirigible.sdk.io.Bytes;

// text -> bytes with a specific charset
byte[] utf16 = Bytes.textToByteArray("hello", "UTF-16");

// int -> bytes (network byte order)
byte[] header = Bytes.intToByteArray(42, Bytes.BIG_ENDIAN);

// bytes -> int
int value = Bytes.byteArrayToInt(header, Bytes.BIG_ENDIAN);

Fields

BIG_ENDIAN

String constant identifying big-endian byte order. Pass to methods that take a byteOrder argument.

java
public static final String BIG_ENDIAN = "BIG_ENDIAN";

LITTLE_ENDIAN

String constant identifying little-endian byte order. Pass to methods that take a byteOrder argument.

java
public static final String LITTLE_ENDIAN = "LITTLE_ENDIAN";

Methods

textToByteArray()

Converts a string to a byte array. The first overload uses the platform default charset; the second uses a named charset and throws UnsupportedEncodingException when the name is unknown.

java
public static byte[] textToByteArray(String text);
public static byte[] textToByteArray(String text, String charset) throws UnsupportedEncodingException;
ParameterTypeDescription
textStringThe string to encode.
charsetStringThe charset name (e.g. "UTF-8", "UTF-16"). Optional.

Returns

  • Type: byte[]
  • Description: The encoded byte array.

intToByteArray()

Encodes an integer into a byte array using the given byte order.

java
public static byte[] intToByteArray(int value, String byteOrder);
ParameterTypeDescription
valueintThe integer value to encode.
byteOrderStringOne of BIG_ENDIAN or LITTLE_ENDIAN.

Returns

  • Type: byte[]
  • Description: The encoded byte array.

byteArrayToInt()

Decodes a byte array into an integer using the given byte order.

java
public static int byteArrayToInt(byte[] data, String byteOrder);
ParameterTypeDescription
databyte[]The byte array to decode.
byteOrderStringOne of BIG_ENDIAN or LITTLE_ENDIAN.

Returns

  • Type: int
  • Description: The decoded integer value.

Released under the EPL-2.0 License.