Utf8
Overview
Module
- package:
org.eclipse.dirigible.sdk.utils - source: utils/Utf8.java
UTF-8 byte / String conversion helpers. Equivalent to str.getBytes(StandardCharsets.UTF_8) and new String(bytes, StandardCharsets.UTF_8); the wrapper exists so client code that reads and writes through byte[] streams (e.g. org.eclipse.dirigible.sdk.io.Streams output) can keep encoding choices in one obvious place.
bytesToString(byte[], int, int) lets callers decode a slice of a larger buffer without an intermediate copy — handy when chunking I/O.
Key Features:
- Single charset, single API: No charset argument needed — UTF-8 is always the answer.
- Slice-aware decode:
bytesToString(bytes, offset, length)decodes a window of a larger buffer. - Round-trip safe:
decode(encode(s))returns the original string.
Example Usage:
java
import org.eclipse.dirigible.sdk.utils.Utf8;
// String → UTF-8 bytes
byte[] bytes = Utf8.encode("Hello, World!");
// UTF-8 bytes → String
String text = Utf8.decode(bytes);
// Decode a slice of a larger buffer (e.g. partial network read)
byte[] buffer = readChunk();
String slice = Utf8.bytesToString(buffer, 0, 32);Methods
encode()
Encodes a string as a UTF-8 byte array.
javapublic static byte[] encode(String input) throws UnsupportedEncodingException;
Parameter Type Description inputStringThe text to encode. Returns
- Type:
byte[]- Description: The UTF-8 byte representation of
input.
decode()
Decodes a UTF-8 byte array into a string.
javapublic static String decode(byte[] input);
Parameter Type Description inputbyte[]The UTF-8 bytes to decode. Returns
- Type:
String- Description: The decoded string.
bytesToString()
Decodes a slice of a UTF-8 byte buffer — offset and length define the window within bytes to decode, with no intermediate copy.
javapublic static String bytesToString(byte[] bytes, int offset, int length) throws UnsupportedEncodingException;
Parameter Type Description bytesbyte[]The source buffer. offsetintIndex of the first byte to decode. lengthintNumber of bytes to decode starting from offset.Returns
- Type:
String- Description: The decoded string for the specified window of
bytes.