encode method
Encodes the input string using the codec's specific algorithm.
input
The plain text string to be encoded.
Returns the encoded string.
Throws ArgumentError if the input is invalid for this codec. Throws FormatException if encoding fails due to format issues.
Implementation
@override
String encode(String input) {
if (input.isEmpty) {
throw ArgumentError('Input cannot be empty');
}
if (level < 0 || level > 9) {
throw ArgumentError('Compression level must be between 0 and 9');
}
try {
final bytes = utf8.encode(input);
// Use GZipCodec from dart:io with specified compression level
final gzipCodec = GZipCodec(level: level);
final compressed = gzipCodec.encode(bytes);
return base64.encode(compressed);
} catch (e) {
throw FormatException('Failed to compress input: $e');
}
}