encode method

List<int> encode(
  1. MetadataV2_4Body data
)

Implementation

List<int> encode(MetadataV2_4Body data) {
  int start = 0;
  final idBytes = _output.sublist(start, start + 3);
  if (iso_8859_1_codec.decode(idBytes) == 'ID3') {
    start += 3;
    _deepDecode(start, data);
  } else {
    // Search end of file
    // Maybe there exists id3v1, so we should first search for ID3v1
    int outputLength = _output.length;
    start = outputLength - 128;
    if (start > 0 &&
        iso_8859_1_codec.decode(_output.sublist(start, start + 3)) == 'TAG') {
      // exist ID3v1
      start -= 10;
    } else {
      start = outputLength - 10;
    }
    if (start > 0 &&
        iso_8859_1_codec.decode(_output.sublist(start, start + 3)) == '3DI') {
      start += 3;
      // exist footer, find the `start` of id3v2.4
      start = _decodeFooterReturnFixStart(start);
      // subtract 3 bytes of ‘ID3’
      start += 3;
      _deepDecode(start, data);
    } else {
      start = 0;
      final insertBytes = _createNewID3Body(start, data);
      _output.insertAll(0, insertBytes);
    }
  }
  return _output;
}