1
1
use crate :: set_panic_hook;
2
2
use brotli:: enc:: encode:: {
3
- BrotliEncoderCompressStream , BrotliEncoderCreateInstance , BrotliEncoderDestroyInstance ,
4
- BrotliEncoderIsFinished , BrotliEncoderOperation , BrotliEncoderParameter ,
5
- BrotliEncoderSetParameter , BrotliEncoderStateStruct ,
3
+ BrotliEncoderDestroyInstance , BrotliEncoderOperation , BrotliEncoderParameter ,
4
+ BrotliEncoderStateStruct ,
6
5
} ;
7
6
use brotli:: enc:: StandardAlloc ; // Re-exported from alloc_stdlib::StandardAlloc
8
7
use brotli:: { self , BrotliDecompressStream , BrotliResult , BrotliState } ;
@@ -58,15 +57,11 @@ impl CompressStream {
58
57
pub fn new ( quality : Option < u32 > ) -> CompressStream {
59
58
set_panic_hook ( ) ;
60
59
let alloc = StandardAlloc :: default ( ) ;
61
- let mut state = BrotliEncoderCreateInstance ( alloc) ;
60
+ let mut state = BrotliEncoderStateStruct :: new ( alloc) ;
62
61
match quality {
63
62
None => ( ) ,
64
63
Some ( quality) => {
65
- BrotliEncoderSetParameter (
66
- & mut state,
67
- BrotliEncoderParameter :: BROTLI_PARAM_QUALITY ,
68
- quality,
69
- ) ;
64
+ state. set_parameter ( BrotliEncoderParameter :: BROTLI_PARAM_QUALITY , quality) ;
70
65
}
71
66
}
72
67
Self {
@@ -97,8 +92,7 @@ impl CompressStream {
97
92
// `BrotliEncoderCompressStream` does not return a `BrotliResult` but returns a boolean,
98
93
// which is different from `BrotliDecompressStream`.
99
94
// But the requirement for input/output buf is common so we reused `BrotliStreamResult` to report it.
100
- let ret = BrotliEncoderCompressStream (
101
- & mut self . state ,
95
+ if self . state . compress_stream (
102
96
op,
103
97
& mut available_in,
104
98
& input,
@@ -108,8 +102,7 @@ impl CompressStream {
108
102
& mut output_offset,
109
103
& mut Some ( self . total_out ) ,
110
104
& mut nop_callback,
111
- ) ;
112
- if ret != 0 {
105
+ ) {
113
106
if available_in == 0 {
114
107
output. truncate ( output_offset) ;
115
108
Ok ( BrotliStreamResult {
@@ -136,9 +129,8 @@ impl CompressStream {
136
129
let op = BrotliEncoderOperation :: BROTLI_OPERATION_FINISH ;
137
130
let input = Vec :: new ( ) . into_boxed_slice ( ) ;
138
131
let mut available_in = 0 ;
139
- while BrotliEncoderIsFinished ( & mut self . state ) == 0 && available_out > 0 {
140
- let ret = BrotliEncoderCompressStream (
141
- & mut self . state ,
132
+ while !self . state . is_finished ( ) && available_out > 0 {
133
+ if !self . state . compress_stream (
142
134
op,
143
135
& mut available_in,
144
136
& input,
@@ -148,8 +140,7 @@ impl CompressStream {
148
140
& mut output_offset,
149
141
& mut Some ( self . total_out ) ,
150
142
& mut nop_callback,
151
- ) ;
152
- if ret == 0 {
143
+ ) {
153
144
return Err ( JsError :: new (
154
145
"Brotli streaming compress failed: When finishing" ,
155
146
) ) ;
0 commit comments