Skip to main content

snappy_neo 0.2.2 copy "snappy_neo: ^0.2.2" to clipboard
snappy_neo: ^0.2.2 copied to clipboard

Pure Dart implementation of Google's Snappy compression: block and framing formats, streaming decoders, and a zero-copy Bytes API powered by blit.

example/snappy_neo_example.dart

import 'dart:convert';
import 'dart:typed_data';

import 'package:snappy_neo/snappy_neo.dart';

void main() {
  final source = Uint8List.fromList(utf8.encode('snappy ' * 100));

  // Block format: the most compact representation.
  final compressed = Snappy.compress(source);
  final restored = Snappy.decompress(compressed);
  print(
    'block:     ${source.length} -> ${compressed.length} -> '
    '${restored.length} bytes, roundtrip ${_same(restored, source)}',
  );

  // Framing format: chunked, checksummed, decodable incrementally.
  final framed = SnappyFraming.compress(source);
  final unpacked = SnappyFraming.decompress(framed);
  print(
    'framed:    ${source.length} -> ${framed.length} -> '
    '${unpacked.length} bytes, roundtrip ${_same(unpacked, source)}',
  );

  // Streaming: feed arbitrary slices, receive framed chunks.
  final output = BytesBuilder();
  final sink = const SnappyFramingEncoder().startChunkedConversion(
    ByteConversionSink.withCallback(output.add),
  );
  for (var start = 0; start < source.length; start += 100) {
    final end = start + 100 < source.length ? start + 100 : source.length;
    sink.add(Uint8List.sublistView(source, start, end));
  }
  sink.close();
  final streamed = output.toBytes();
  print(
    'streaming: ${streamed.length} bytes, same as framed: '
    '${_same(streamed, framed)}',
  );
}

bool _same(Uint8List a, Uint8List b) {
  if (a.length != b.length) return false;
  for (var i = 0; i < a.length; i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}
0
likes
160
points
191
downloads

Documentation

API reference

Publisher

verified publisherunk404.com

Weekly Downloads

Pure Dart implementation of Google's Snappy compression: block and framing formats, streaming decoders, and a zero-copy Bytes API powered by blit.

Repository (GitHub)
View/report issues

Topics

#compression #snappy #codec

License

BSD-3-Clause (license)

Dependencies

blit

More

Packages that depend on snappy_neo