Class: ZstdNative::CStream
- Inherits:
-
Object
- Object
- ZstdNative::CStream
- Defined in:
- ext/zstd_native/zstd_native.c
Class Method Summary collapse
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'ext/zstd_native/zstd_native.c', line 343
static VALUE cstream_initialize(int argc, VALUE *argv, VALUE self) {
VALUE opts;
rb_scan_args(argc, argv, "01", &opts);
int level = ZSTD_CLEVEL_DEFAULT;
if (!NIL_P(opts)) {
Check_Type(opts, T_HASH);
VALUE level_val = rb_hash_aref(opts, ID2SYM(rb_intern("level")));
if (!NIL_P(level_val)) {
level = NUM2INT(level_val);
}
}
ZSTD_CStream *stream = ZSTD_createCStream();
if (!stream) {
rb_raise(eZstdError, "Failed to create compression stream");
}
size_t result = ZSTD_initCStream(stream, level);
if (ZSTD_isError(result)) {
ZSTD_freeCStream(stream);
rb_raise(eZstdError, "Failed to initialize compression stream: %s", ZSTD_getErrorName(result));
}
DATA_PTR(self) = stream;
return self;
}
|
Class Method Details
.in_size ⇒ Object
462 463 464 |
# File 'ext/zstd_native/zstd_native.c', line 462 static VALUE cstream_in_size(VALUE self) { return SIZET2NUM(ZSTD_CStreamInSize()); } |
.out_size ⇒ Object
466 467 468 |
# File 'ext/zstd_native/zstd_native.c', line 466 static VALUE cstream_out_size(VALUE self) { return SIZET2NUM(ZSTD_CStreamOutSize()); } |
Instance Method Details
#compress(data) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'ext/zstd_native/zstd_native.c', line 375
static VALUE cstream_compress(VALUE self, VALUE data) {
Check_Type(data, T_STRING);
ZSTD_CStream *stream;
TypedData_Get_Struct(self, ZSTD_CStream, &cstream_type, stream);
const char *src = RSTRING_PTR(data);
size_t src_size = RSTRING_LEN(data);
ZSTD_inBuffer input = {src, src_size, 0};
size_t out_capacity = ZSTD_CStreamOutSize();
VALUE result = rb_str_buf_new(out_capacity);
char *dst = RSTRING_PTR(result);
size_t total_written = 0;
while (input.pos < input.size) {
ZSTD_outBuffer output = {dst + total_written, out_capacity - total_written, 0};
size_t remaining = ZSTD_compressStream(stream, &output, &input);
check_zstd_error(remaining, "Stream compression failed");
total_written += output.pos;
if (total_written >= out_capacity) {
out_capacity *= 2;
rb_str_resize(result, out_capacity);
dst = RSTRING_PTR(result);
}
}
rb_str_set_len(result, total_written);
return result;
}
|
#finish ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'ext/zstd_native/zstd_native.c', line 435
static VALUE cstream_finish(VALUE self) {
ZSTD_CStream *stream;
TypedData_Get_Struct(self, ZSTD_CStream, &cstream_type, stream);
size_t out_capacity = ZSTD_CStreamOutSize();
VALUE result = rb_str_buf_new(out_capacity);
char *dst = RSTRING_PTR(result);
size_t total_written = 0;
size_t remaining;
do {
ZSTD_outBuffer output = {dst + total_written, out_capacity - total_written, 0};
remaining = ZSTD_endStream(stream, &output);
check_zstd_error(remaining, "Stream end failed");
total_written += output.pos;
if (remaining > 0 && total_written >= out_capacity) {
out_capacity *= 2;
rb_str_resize(result, out_capacity);
dst = RSTRING_PTR(result);
}
} while (remaining > 0);
rb_str_set_len(result, total_written);
return result;
}
|
#flush ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'ext/zstd_native/zstd_native.c', line 408
static VALUE cstream_flush(VALUE self) {
ZSTD_CStream *stream;
TypedData_Get_Struct(self, ZSTD_CStream, &cstream_type, stream);
size_t out_capacity = ZSTD_CStreamOutSize();
VALUE result = rb_str_buf_new(out_capacity);
char *dst = RSTRING_PTR(result);
size_t total_written = 0;
size_t remaining;
do {
ZSTD_outBuffer output = {dst + total_written, out_capacity - total_written, 0};
remaining = ZSTD_flushStream(stream, &output);
check_zstd_error(remaining, "Stream flush failed");
total_written += output.pos;
if (remaining > 0 && total_written >= out_capacity) {
out_capacity *= 2;
rb_str_resize(result, out_capacity);
dst = RSTRING_PTR(result);
}
} while (remaining > 0);
rb_str_set_len(result, total_written);
return result;
}
|