Class: Brotli::Writer
- Inherits:
-
Object
- Object
- Brotli::Writer
- Defined in:
- ext/brotli/brotli.c
Instance Method Summary collapse
- #close ⇒ Object
- #finish ⇒ Object
- #flush ⇒ Object
- #initialize(*args) ⇒ Object constructor
- #write(buf) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'ext/brotli/brotli.c', line 440
static VALUE rb_writer_initialize(int argc, VALUE* argv, VALUE self) {
VALUE io = Qnil;
VALUE opts = Qnil;
VALUE dict = Qnil;
rb_scan_args(argc, argv, "11", &io, &opts);
if (NIL_P(io)) {
rb_raise(rb_eArgError, "io should not be nil");
return Qnil;
}
struct brotli *br;
TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
brotli_deflate_parse_options(br->state, opts);
br->io = io;
#if defined(HAVE_BROTLIENCODERPREPAREDICTIONARY) && defined(HAVE_BROTLIENCODERATTACHPREPAREDDICTIONARY)
/* Extract and attach dictionary if provided */
if (!NIL_P(opts)) {
Check_Type(opts, T_HASH);
dict = rb_hash_aref(opts, CSTR2SYM("dictionary"));
if (!NIL_P(dict)) {
StringValue(dict);
BrotliEncoderPreparedDictionary* prepared_dict = BrotliEncoderPrepareDictionary(
BROTLI_SHARED_DICTIONARY_RAW,
(size_t)RSTRING_LEN(dict),
(uint8_t*)RSTRING_PTR(dict),
BROTLI_MAX_QUALITY,
brotli_alloc,
brotli_free,
NULL);
if (prepared_dict) {
BrotliEncoderAttachPreparedDictionary(br->state, prepared_dict);
/* Note: dict is owned by encoder after attach, no need to free */
} else {
rb_raise(rb_eBrotli, "Failed to prepare dictionary for compression");
}
}
}
#else
/* Check if dictionary is requested but not supported */
if (!NIL_P(opts)) {
Check_Type(opts, T_HASH);
dict = rb_hash_aref(opts, CSTR2SYM("dictionary"));
if (!NIL_P(dict)) {
rb_raise(rb_eBrotli, "Dictionary support not available in this build");
}
}
#endif
return self;
}
|
Instance Method Details
#close ⇒ Object
554 555 556 557 558 559 560 561 562 563 564 |
# File 'ext/brotli/brotli.c', line 554
static VALUE rb_writer_close(VALUE self) {
struct brotli* br;
TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
rb_writer_finish(self);
if (rb_respond_to(br->io, id_close)) {
rb_funcall(br->io, id_close, 0);
}
return br->io;
}
|
#finish ⇒ Object
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'ext/brotli/brotli.c', line 514
static VALUE rb_writer_finish(VALUE self) {
struct brotli* br;
TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
brotli_encoder_args_t args = {
.state = br->state,
.op = BROTLI_OPERATION_FINISH,
.available_in = 0,
.next_in = NULL
};
while (!BrotliEncoderIsFinished(br->state)) {
rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
push_output(br);
}
return br->io;
}
|
#flush ⇒ Object
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'ext/brotli/brotli.c', line 532
static VALUE rb_writer_flush(VALUE self) {
struct brotli *br;
TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
brotli_encoder_args_t args = {
.state = br->state,
.op = BROTLI_OPERATION_FLUSH,
.available_in = 0,
.next_in = NULL
};
do {
rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
push_output(br);
} while (BrotliEncoderHasMoreOutput(br->state));
if (rb_respond_to(br->io, id_flush)) {
rb_funcall(br->io, id_flush, 0);
}
return self;
}
|
#write(buf) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'ext/brotli/brotli.c', line 492
static VALUE rb_writer_write(VALUE self, VALUE buf) {
struct brotli* br;
TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
StringValue(buf);
const size_t total = (size_t)RSTRING_LEN(buf);
brotli_encoder_args_t args = {
.state = br->state,
.op = BROTLI_OPERATION_PROCESS,
.available_in = total,
.next_in = (uint8_t*)RSTRING_PTR(buf)
};
while (args.available_in > 0) {
rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
push_output(br);
}
return SIZET2NUM(total);
}
|