Skip to content

Commit

Permalink
Fixed Issue #52
Browse files Browse the repository at this point in the history
  • Loading branch information
sethloco committed Sep 7, 2024
1 parent e19a3bb commit 1215671
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.13

- Fixed issue [#52](https://github.com/instantiations/es_compression/issues/52) (special thanks to [Abitofevrything](https://github.com/abitofevrything) for a helpful issue report)

## 2.0.12

- Removed deprecated `dart:cli.waitFor()` uses. [#50](https://github.com/instantiations/es_compression/issues/50)
Expand Down
7 changes: 6 additions & 1 deletion lib/src/brotli/ffi/decompress_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class BrotliDecompressFilter extends NativeCodecFilterBase {
/// Native brotli state object.
late final Pointer<BrotliDecoderState> _brotliState;

/// Special Case: Empty input
late bool _emptyInput = false;

/// Construct an [BrotliDecompressFilter] with the supplied options.
BrotliDecompressFilter(
{bool ringBufferReallocation = true,
Expand Down Expand Up @@ -66,6 +69,7 @@ class BrotliDecompressFilter extends NativeCodecFilterBase {
if (!outputBufferHolder.isLengthSet()) {
outputBufferHolder.length = brotliDecoderOutputBufferLength;
}
_emptyInput = end - start <= 0;
return 0;
}

Expand Down Expand Up @@ -94,7 +98,8 @@ class BrotliDecompressFilter extends NativeCodecFilterBase {
/// state.
@override
int doFinalize(CodecBuffer<dynamic> outputBuffer) {
if (!_dispatcher.callBrotliDecoderIsFinished(_brotliState)) {
if (!_dispatcher.callBrotliDecoderIsFinished(_brotliState) &&
!_emptyInput) {
throw const FormatException('Failure to finish decoding');
}
return 0;
Expand Down
6 changes: 6 additions & 0 deletions lib/src/framework/filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ abstract class CodecFilter<P, CB extends CodecBuffer<P>> {
if (flush == true) _flush(builder);
if (end == true) _finalize(builder);
}

// Return null if no bytes were added to the builder
if (builder.isEmpty) {
return null;
}

return builder.takeBytes();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/lz4/ffi/decompress_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class Lz4DecompressFilter extends NativeCodecFilterBase {
final numBytes = inputBufferHolder.buffer.nextPutAll(bytes, start, end);
if (numBytes > 0) _readFrameInfo(inputBufferHolder.buffer);

const defaultBlockSize = 65536;

outputBufferHolder.length = max(
_frameInfo!.ref.blockSize,
_frameInfo?.ref.blockSize ?? defaultBlockSize,
outputBufferHolder.isLengthSet()
? outputBufferHolder.length
: max(lz4DecoderOutputBufferLength, inputBufferHolder.length));
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: es_compression
description: Compression framework providing FFI implementations for Brotli, Lz4, Zstd (Zstandard) with ready-to-use prebuilt binaries for Win/Linux/Mac.
version: 2.0.12
version: 2.0.13
homepage: https://www.instantiations.com
issue_tracker: https://github.com/instantiations/es_compression/issues
repository: https://github.com/instantiations/es_compression
Expand Down
7 changes: 7 additions & 0 deletions test/brotli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ void main() {
expect(() => brotli.decode(<int>[1, 2, 3]), throwsFormatException);
});

test('Test Brotli Decode Empty Stream Produces No Chunks', () async {
final result =
await const Stream<List<int>>.empty().transform(brotli.decoder).isEmpty;
expect(result, isTrue,
reason: "Stream should be empty and emit no chunks.");
});

test('Test Empty Brotli Encode/Decode', () {
const data = '';
final header = [107, 0, 3];
Expand Down
7 changes: 7 additions & 0 deletions test/lz4_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ void main() {
expect(const ListEquality<int>().equals(dataBytes, decoded), true);
});

test('Test Lz4 Decode Empty Stream Produces No Chunks', () async {
final result =
await const Stream<List<int>>.empty().transform(lz4.decoder).isEmpty;
expect(result, isTrue,
reason: "Stream should be empty and emit no chunks.");
});

test('Test Simple Lz4 Encode/Decode', () {
const data = 'MyDart';
final expected = [
Expand Down
7 changes: 7 additions & 0 deletions test/zstd_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ void main() {
expect(const ListEquality<int>().equals(dataBytes, decoded), true);
});

test('Test Zstd Decode Empty Stream Produces No Chunks', () async {
final result =
await const Stream<List<int>>.empty().transform(zstd.decoder).isEmpty;
expect(result, isTrue,
reason: "Stream should be empty and emit no chunks.");
});

test('Test Simple Zstd Encode/Decode', () {
const data = 'MyDart';
final expected = [
Expand Down

0 comments on commit 1215671

Please sign in to comment.