@@ -4,6 +4,7 @@ struct streaming_compress_t {
4
4
ZSTD_CCtx * ctx ;
5
5
VALUE buf ;
6
6
size_t buf_size ;
7
+ VALUE pending ; /* accumulate compressed bytes produced by write() */
7
8
};
8
9
9
10
static void
@@ -12,8 +13,10 @@ streaming_compress_mark(void *p)
12
13
struct streaming_compress_t * sc = p ;
13
14
#ifdef HAVE_RB_GC_MARK_MOVABLE
14
15
rb_gc_mark_movable (sc -> buf );
16
+ rb_gc_mark_movable (sc -> pending );
15
17
#else
16
18
rb_gc_mark (sc -> buf );
19
+ rb_gc_mark (sc -> pending );
17
20
#endif
18
21
}
19
22
@@ -40,6 +43,7 @@ streaming_compress_compact(void *p)
40
43
{
41
44
struct streaming_compress_t * sc = p ;
42
45
sc -> buf = rb_gc_location (sc -> buf );
46
+ sc -> pending = rb_gc_location (sc -> pending );
43
47
}
44
48
#endif
45
49
@@ -64,6 +68,7 @@ rb_streaming_compress_allocate(VALUE klass)
64
68
sc -> ctx = NULL ;
65
69
sc -> buf = Qnil ;
66
70
sc -> buf_size = 0 ;
71
+ sc -> pending = Qnil ;
67
72
return obj ;
68
73
}
69
74
@@ -86,6 +91,7 @@ rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj)
86
91
sc -> ctx = ctx ;
87
92
sc -> buf = rb_str_new (NULL , buffOutSize );
88
93
sc -> buf_size = buffOutSize ;
94
+ sc -> pending = rb_str_new (0 , 0 );
89
95
90
96
return obj ;
91
97
}
@@ -142,7 +148,6 @@ static VALUE
142
148
rb_streaming_compress_write (int argc , VALUE * argv , VALUE obj )
143
149
{
144
150
size_t total = 0 ;
145
- VALUE result = rb_str_new (0 , 0 );
146
151
struct streaming_compress_t * sc ;
147
152
TypedData_Get_Struct (obj , struct streaming_compress_t , & streaming_compress_type , sc );
148
153
const char * output_data = RSTRING_PTR (sc -> buf );
@@ -160,6 +165,10 @@ rb_streaming_compress_write(int argc, VALUE *argv, VALUE obj)
160
165
if (ZSTD_isError (ret )) {
161
166
rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (ret ));
162
167
}
168
+ /* collect produced bytes */
169
+ if (output .pos > 0 ) {
170
+ rb_str_cat (sc -> pending , output .dst , output .pos );
171
+ }
163
172
total += RSTRING_LEN (str );
164
173
}
165
174
}
@@ -192,17 +201,23 @@ rb_streaming_compress_flush(VALUE obj)
192
201
{
193
202
struct streaming_compress_t * sc ;
194
203
TypedData_Get_Struct (obj , struct streaming_compress_t , & streaming_compress_type , sc );
195
- VALUE result = no_compress (sc , ZSTD_e_flush );
196
- return result ;
204
+ VALUE drained = no_compress (sc , ZSTD_e_flush );
205
+ rb_str_cat (sc -> pending , RSTRING_PTR (drained ), RSTRING_LEN (drained ));
206
+ VALUE out = sc -> pending ;
207
+ sc -> pending = rb_str_new (0 , 0 );
208
+ return out ;
197
209
}
198
210
199
211
static VALUE
200
212
rb_streaming_compress_finish (VALUE obj )
201
213
{
202
214
struct streaming_compress_t * sc ;
203
215
TypedData_Get_Struct (obj , struct streaming_compress_t , & streaming_compress_type , sc );
204
- VALUE result = no_compress (sc , ZSTD_e_end );
205
- return result ;
216
+ VALUE drained = no_compress (sc , ZSTD_e_end );
217
+ rb_str_cat (sc -> pending , RSTRING_PTR (drained ), RSTRING_LEN (drained ));
218
+ VALUE out = sc -> pending ;
219
+ sc -> pending = rb_str_new (0 , 0 );
220
+ return out ;
206
221
}
207
222
208
223
extern VALUE rb_mZstd , cStreamingCompress ;
0 commit comments