@@ -43,13 +43,20 @@ where
4343 W : Write ,
4444 D : Operation ,
4545{
46- /// Creates a new `Writer`.
46+ /// Creates a new `Writer` with a fixed buffer capacity of 32KB
4747 ///
4848 /// All output from the given operation will be forwarded to `writer`.
4949 pub fn new ( writer : W , operation : D ) -> Self {
5050 // 32KB buffer? That's what flate2 uses
51+ new_with_capacity ( W , D , 32 * 1024 )
52+ }
53+
54+ /// Creates a new `Writer` with user defined capacity.
55+ ///
56+ /// All output from the given operation will be forwarded to `writer`.
57+ pub fn new_with_capacity ( writer : W , operation : D , capacity : usize ) -> Self {
5158 Self :: with_output_buffer (
52- Vec :: with_capacity ( 32 * 1024 ) ,
59+ Vec :: with_capacity ( capacity ) ,
5360 writer,
5461 operation,
5562 )
@@ -314,6 +321,25 @@ mod tests {
314321 assert_eq ! ( & decoded, input) ;
315322 }
316323
324+ #[ test]
325+ fn test_compress_with_capacity ( ) {
326+ use crate :: stream:: raw:: Encoder ;
327+
328+ let input = b"AbcdefghAbcdefgh." ;
329+
330+ // Test writer
331+ let mut output = Vec :: new ( ) ;
332+ {
333+ let mut writer =
334+ Writer :: new_with_capacity ( & mut output, Encoder :: new ( 1 ) . unwrap ( ) , 64 ) ;
335+ assert_eq ! ( writer. buffer( ) . capacity( ) == 64 ) ;
336+ writer. write_all ( input) . unwrap ( ) ;
337+ writer. finish ( ) . unwrap ( ) ;
338+ }
339+ let decoded = crate :: decode_all ( & output[ ..] ) . unwrap ( ) ;
340+ assert_eq ! ( & decoded, input) ;
341+ }
342+
317343 #[ test]
318344 fn test_decompress ( ) {
319345 use crate :: stream:: raw:: Decoder ;
@@ -331,4 +357,22 @@ mod tests {
331357 // println!("Output: {:?}", output);
332358 assert_eq ! ( & output, input) ;
333359 }
360+
361+ #[ test]
362+ fn test_decompress_with_capacity ( ) {
363+ use crate :: stream:: raw:: Decoder ;
364+
365+ let input = b"AbcdefghAbcdefgh." ;
366+ let compressed = crate :: encode_all ( & input[ ..] , 1 ) . unwrap ( ) ;
367+
368+ // Test writer
369+ let mut output = Vec :: new ( ) ;
370+ {
371+ let mut writer = Writer :: new ( & mut output, Decoder :: new ( ) . unwrap ( ) , 64 ) ;
372+ assert_eq ! ( writer. buffer( ) . capacity( ) == 64 ) ;
373+ writer. write_all ( & compressed) . unwrap ( ) ;
374+ writer. finish ( ) . unwrap ( ) ;
375+ }
376+ assert_eq ! ( & output, input) ;
377+ }
334378}
0 commit comments