Class: Zstd::StreamingCompress
- Inherits:
-
Object
- Object
- Zstd::StreamingCompress
- Defined in:
- ext/zstdruby/streaming_compress.c
Constant Summary collapse
- CONTINUE =
INT2FIX(ZSTD_e_continue)
- FLUSH =
INT2FIX(ZSTD_e_flush)
- END =
INT2FIX(ZSTD_e_end)
Instance Method Summary collapse
- #<<(src) ⇒ Object
- #compress(src) ⇒ Object
- #finish ⇒ Object
- #flush ⇒ Object
- #initialize(*args) ⇒ Object constructor
Constructor Details
#initialize(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'ext/zstdruby/streaming_compress.c', line 51 static VALUE rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj) { VALUE compression_level_value; rb_scan_args(argc, argv, "01", &compression_level_value); int compression_level; if (NIL_P(compression_level_value)) { compression_level = ZSTD_CLEVEL_DEFAULT; } else { compression_level = NUM2INT(compression_level_value); } struct streaming_compress_t* sc; TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); size_t const buffOutSize = ZSTD_CStreamOutSize(); ZSTD_CCtx* ctx = ZSTD_createCCtx(); if (ctx == NULL) { rb_raise(rb_eRuntimeError, "%s", "ZSTD_createCCtx error"); } ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, compression_level); sc->ctx = ctx; sc->buf = rb_str_new(NULL, buffOutSize); sc->buf_size = buffOutSize; return obj; } |
Instance Method Details
#<<(src) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'ext/zstdruby/streaming_compress.c', line 127 static VALUE rb_streaming_compress_addstr(VALUE obj, VALUE src) { StringValue(src); const char* input_data = RSTRING_PTR(src); size_t input_size = RSTRING_LEN(src); ZSTD_inBuffer input = { input_data, input_size, 0 }; struct streaming_compress_t* sc; TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); const char* output_data = RSTRING_PTR(sc->buf); while (input.pos < input.size) { ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; size_t const result = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_continue); if (ZSTD_isError(result)) { rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(result)); } } return obj; } |
#compress(src) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'ext/zstdruby/streaming_compress.c', line 104 static VALUE rb_streaming_compress_compress(VALUE obj, VALUE src) { StringValue(src); const char* input_data = RSTRING_PTR(src); size_t input_size = RSTRING_LEN(src); ZSTD_inBuffer input = { input_data, input_size, 0 }; struct streaming_compress_t* sc; TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); const char* output_data = RSTRING_PTR(sc->buf); VALUE result = rb_str_new(0, 0); while (input.pos < input.size) { ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 }; size_t const ret = ZSTD_compressStream2(sc->ctx, &output, &input, ZSTD_e_continue); if (ZSTD_isError(ret)) { rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret)); } rb_str_cat(result, output.dst, output.pos); } return result; } |
#finish ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'ext/zstdruby/streaming_compress.c', line 158 static VALUE rb_streaming_compress_finish(VALUE obj) { struct streaming_compress_t* sc; TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); VALUE result = no_compress(sc, ZSTD_e_end); return result; } |
#flush ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'ext/zstdruby/streaming_compress.c', line 149 static VALUE rb_streaming_compress_flush(VALUE obj) { struct streaming_compress_t* sc; TypedData_Get_Struct(obj, struct streaming_compress_t, &streaming_compress_type, sc); VALUE result = no_compress(sc, ZSTD_e_flush); return result; } |