@@ -41,6 +41,7 @@ impl<W: Write, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc>
41
41
buffer,
42
42
alloc,
43
43
Error :: new ( ErrorKind :: InvalidData , "Invalid Data" ) ,
44
+ Error :: new ( ErrorKind :: WriteZero , "No room in output." ) ,
44
45
q,
45
46
lgwin,
46
47
) )
@@ -127,14 +128,24 @@ pub struct CompressorWriterCustomIo<
127
128
output : Option < W > ,
128
129
error_if_invalid_data : Option < ErrType > ,
129
130
state : BrotliEncoderStateStruct < Alloc > ,
131
+ error_if_zero_bytes_written : Option < ErrType > ,
130
132
}
131
- pub fn write_all < ErrType , W : CustomWrite < ErrType > > (
133
+ pub fn write_all < ErrType , W : CustomWrite < ErrType > , ErrMaker : FnMut ( ) -> Option < ErrType > > (
132
134
writer : & mut W ,
133
135
mut buf : & [ u8 ] ,
136
+ mut error_to_return_if_zero_bytes_written : ErrMaker ,
134
137
) -> Result < ( ) , ErrType > {
135
138
while !buf. is_empty ( ) {
136
139
match writer. write ( buf) {
137
- Ok ( bytes_written) => buf = & buf[ bytes_written..] ,
140
+ Ok ( bytes_written) => if bytes_written != 0 {
141
+ buf = & buf[ bytes_written..]
142
+ } else {
143
+ if let Some ( err) = error_to_return_if_zero_bytes_written ( ) {
144
+ return Err ( err) ;
145
+ } else {
146
+ return Ok ( ( ) ) ;
147
+ }
148
+ } ,
138
149
Err ( e) => return Err ( e) ,
139
150
}
140
151
}
@@ -148,6 +159,7 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
148
159
buffer : BufferType ,
149
160
alloc : Alloc ,
150
161
invalid_data_error_type : ErrType ,
162
+ error_if_zero_bytes_written : ErrType ,
151
163
q : u32 ,
152
164
lgwin : u32 ,
153
165
) -> Self {
@@ -157,6 +169,7 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
157
169
output : Some ( w) ,
158
170
state : BrotliEncoderStateStruct :: new ( alloc) ,
159
171
error_if_invalid_data : Some ( invalid_data_error_type) ,
172
+ error_if_zero_bytes_written : Some ( error_if_zero_bytes_written) ,
160
173
} ;
161
174
ret. state
162
175
. set_parameter ( BrotliEncoderParameter :: BROTLI_PARAM_QUALITY , q) ;
@@ -189,9 +202,17 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
189
202
& mut nop_callback,
190
203
) ;
191
204
if output_offset > 0 {
205
+ let zero_err = & mut self . error_if_zero_bytes_written ;
206
+ let fallback = & mut self . error_if_invalid_data ;
192
207
match write_all (
193
208
self . output . as_mut ( ) . unwrap ( ) ,
194
209
& self . output_buffer . slice_mut ( ) [ ..output_offset] ,
210
+ || {
211
+ if let Some ( err) = zero_err. take ( ) {
212
+ return Some ( err) ;
213
+ }
214
+ fallback. take ( )
215
+ } ,
195
216
) {
196
217
Ok ( _) => { }
197
218
Err ( e) => return Err ( e) ,
@@ -266,12 +287,23 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
266
287
& mut nop_callback,
267
288
) ;
268
289
if output_offset > 0 {
290
+ let zero_err = & mut self . error_if_zero_bytes_written ;
291
+ let fallback = & mut self . error_if_invalid_data ;
269
292
match write_all (
270
293
self . output . as_mut ( ) . unwrap ( ) ,
271
294
& self . output_buffer . slice_mut ( ) [ ..output_offset] ,
295
+ || {
296
+ if let Some ( err) = zero_err. take ( ) {
297
+ return Some ( err) ;
298
+ }
299
+ fallback. take ( )
300
+ } ,
301
+
272
302
) {
273
303
Ok ( _) => { }
274
- Err ( e) => return Err ( e) ,
304
+ Err ( e) => {
305
+ return Err ( e)
306
+ } ,
275
307
}
276
308
}
277
309
if !ret {
0 commit comments