@@ -125,6 +125,7 @@ rb_streaming_compress_compress(VALUE obj, VALUE src)
125125
126126 struct streaming_compress_t * sc ;
127127 TypedData_Get_Struct (obj , struct streaming_compress_t , & streaming_compress_type , sc );
128+
128129 const char * output_data = RSTRING_PTR (sc -> buf );
129130 VALUE result = rb_str_new (0 , 0 );
130131 while (input .pos < input .size ) {
@@ -139,27 +140,54 @@ rb_streaming_compress_compress(VALUE obj, VALUE src)
139140}
140141
141142static VALUE
142- rb_streaming_compress_addstr ( VALUE obj , VALUE src )
143+ rb_streaming_compress_write ( int argc , VALUE * argv , VALUE obj )
143144{
144- StringValue (src );
145- const char * input_data = RSTRING_PTR (src );
146- size_t input_size = RSTRING_LEN (src );
147- ZSTD_inBuffer input = { input_data , input_size , 0 };
148-
145+ size_t total = 0 ;
146+ VALUE result = rb_str_new (0 , 0 );
149147 struct streaming_compress_t * sc ;
150148 TypedData_Get_Struct (obj , struct streaming_compress_t , & streaming_compress_type , sc );
151149 const char * output_data = RSTRING_PTR (sc -> buf );
152-
153- while (input .pos < input .size ) {
154- ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
155- size_t const result = ZSTD_compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
156- if (ZSTD_isError (result )) {
157- rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (result ));
150+ ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
151+
152+ while (argc -- > 0 ) {
153+ VALUE str = * argv ++ ;
154+ StringValue (str );
155+ const char * input_data = RSTRING_PTR (str );
156+ size_t input_size = RSTRING_LEN (str );
157+ ZSTD_inBuffer input = { input_data , input_size , 0 };
158+
159+ while (input .pos < input .size ) {
160+ size_t const ret = ZSTD_compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
161+ if (ZSTD_isError (ret )) {
162+ rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (ret ));
163+ }
164+ total += RSTRING_LEN (str );
158165 }
159166 }
160- return obj ;
167+ return SIZET2NUM ( total ) ;
161168}
162169
170+ /*
171+ * Document-method: <<
172+ * Same as IO.
173+ */
174+ #define rb_streaming_compress_addstr rb_io_addstr
175+ /*
176+ * Document-method: printf
177+ * Same as IO.
178+ */
179+ #define rb_streaming_compress_printf rb_io_printf
180+ /*
181+ * Document-method: print
182+ * Same as IO.
183+ */
184+ #define rb_streaming_compress_print rb_io_print
185+ /*
186+ * Document-method: puts
187+ * Same as IO.
188+ */
189+ #define rb_streaming_compress_puts rb_io_puts
190+
163191static VALUE
164192rb_streaming_compress_flush (VALUE obj )
165193{
@@ -186,12 +214,16 @@ zstd_ruby_streaming_compress_init(void)
186214 rb_define_alloc_func (cStreamingCompress , rb_streaming_compress_allocate );
187215 rb_define_method (cStreamingCompress , "initialize" , rb_streaming_compress_initialize , -1 );
188216 rb_define_method (cStreamingCompress , "compress" , rb_streaming_compress_compress , 1 );
217+ rb_define_method (cStreamingCompress , "write" , rb_streaming_compress_write , -1 );
189218 rb_define_method (cStreamingCompress , "<<" , rb_streaming_compress_addstr , 1 );
219+ rb_define_method (cStreamingCompress , "printf" , rb_streaming_compress_printf , -1 );
220+ rb_define_method (cStreamingCompress , "print" , rb_streaming_compress_print , -1 );
221+ rb_define_method (cStreamingCompress , "puts" , rb_streaming_compress_puts , -1 );
222+
190223 rb_define_method (cStreamingCompress , "flush" , rb_streaming_compress_flush , 0 );
191224 rb_define_method (cStreamingCompress , "finish" , rb_streaming_compress_finish , 0 );
192225
193226 rb_define_const (cStreamingCompress , "CONTINUE" , INT2FIX (ZSTD_e_continue ));
194227 rb_define_const (cStreamingCompress , "FLUSH" , INT2FIX (ZSTD_e_flush ));
195228 rb_define_const (cStreamingCompress , "END" , INT2FIX (ZSTD_e_end ));
196229}
197-
0 commit comments