A unified abstraction for text encoding and decoding operations, providing a consistent interface for converting between raw bytes and human-readable strings across multiple character encodings.
The EncodingDecoder interface defines a contract for encoding and decoding text data in different formats such as UTF-8, ISO-8859-1, ASCII, and others. It enables seamless integration of custom or platform-specific codecs within JetLeaf’s I/O and HTTP subsystems, including file uploads, request parsing, and response serialization.
Responsibilities
- Decode byte arrays into strings using a specified character encoding.
- Encode strings into byte arrays for transmission or storage.
- Report supported encodings and provide introspection capabilities.
- Validate encoding availability to ensure cross-platform compatibility.
Design Notes
- Implementations should use lossless encoding whenever possible to preserve data integrity.
- The default encoding is
'utf-8', which is recommended for most use cases. - Encoders and decoders must handle both text and binary-safe transformations.
- When unsupported encodings are requested, implementations should either throw descriptive exceptions or fall back to UTF-8 gracefully.
- Commonly used within JetLeaf’s HTTP request/response, multipart, and file I/O handling layers.
Example
final decoder = DefaultEncodingDecoder();
// Decode UTF-8 bytes
final text = decoder.decode(Uint8List.fromList([72, 101, 108, 108, 111]));
print(text); // "Hello"
// Encode a string
final bytes = decoder.encode("JetLeaf");
print(bytes); // [74, 101, 116, 76, 101, 97, 102]
// Check for supported encodings
if (decoder.supportsEncoding("iso-8859-1")) {
print("ISO-8859-1 is supported!");
}
Typical Implementations
DefaultEncodingDecoder— Built-in UTF-8 and ISO-8859-1 support.CustomEncodingDecoder— Pluggable codec provider for extended charset support.PlatformEncodingDecoder— Uses native system libraries for charset operations.
See Also
- dart:convert.Encoding — Standard Dart encoding utilities.
InputStreamSource— For working with encoded stream data.- HttpBody — For decoding text-based HTTP payloads.
- Implementers
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
decode(
Uint8List bytes, {String encodingString = 'utf-8', Encoding? encoding}) → String -
Decodes a byte array to a String using the specified
encoding. -
encode(
String text, {String encodingString = 'utf-8', Encoding? encoding}) → Uint8List -
Encodes a String into a Uint8List using the specified
encoding. -
getSupportedEncodings(
) → List< String> - Returns the list of all supported encodings.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
supportsEncoding(
String encoding) → bool -
Returns
trueif the specifiedencodingis supported. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited