encode method

List<int> encode(
  1. String string, {
  2. int? limitByteLength,
  3. ByteCodecType? forceType,
})

Implementation

List<int> encode(String string, {int? limitByteLength, ByteCodecType? forceType}) {
  final decodeType = forceType ?? codecType;
  if (decodeType == ByteCodecType.ISO_8859_1) {
    return transferToLength(latin1.encode(string), byteLength: limitByteLength);
  } else if (decodeType == ByteCodecType.UTF16) {
    final bytes = _encodeWithUTF16(string);
    if (limitByteLength != null) {
      return transferToLength(bytes, byteLength: limitByteLength + (limitByteLength % 2 != 0 ? 1 : 0));
    } else {
      return bytes;
    }
  } else if (decodeType == ByteCodecType.UTF16BE) {
    final bytes = _encodeWithUTF16BE(string);
    if (limitByteLength != null) {
      return transferToLength(bytes, byteLength: limitByteLength + (limitByteLength % 2 != 0 ? 1 : 0));
    } else {
      return bytes;
    }
  } else if (decodeType == ByteCodecType.UTF16LE) {
    final bytes = _encodeWithUTF16LE(string);
    if (limitByteLength != null) {
      return transferToLength(bytes, byteLength: limitByteLength + (limitByteLength % 2 != 0 ? 1 : 0));
    } else {
      return bytes;
    }
  } else if (decodeType == ByteCodecType.UTF8) {
    return transferToLength(utf8.encode(string), byteLength: limitByteLength);
  } else {
    return [];
  }
}