compress method

  1. @override
Future<Uint8List?> compress(
  1. Uint8List data,
  2. int compressionLevel
)

Implementation

@override
Future<Uint8List?> compress(Uint8List data, int compressionLevel) async {
  final int srcSize = data.lengthInBytes;
  final Pointer<Uint8> src = malloc.allocate<Uint8>(srcSize);
  src.asTypedList(srcSize).setAll(0, data);

  final int dstCapacity = _bindings.ZSTD_compressBound(srcSize);
  final Pointer<Uint8> dst = malloc.allocate<Uint8>(dstCapacity);

  try {
    final int compressedSize = _bindings.ZSTD_compress(
      dst.cast(),
      dstCapacity,
      src.cast(),
      srcSize,
      compressionLevel,
    );

    if (compressedSize > 0) {
      return Uint8List.fromList(dst.asTypedList(compressedSize));
    } else {
      return null;
    }
  } finally {
    malloc.free(src);
    malloc.free(dst);
  }
}