Class: MessagePack::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack/buffer.rb,
ext/msgpack/buffer_class.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/msgpack/buffer_class.c', line 220

static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE io = Qnil;
    VALUE options = Qnil;

    if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
        /* Qnil */

    } else if(argc == 1) {
        VALUE v = argv[0];
        if(rb_type(v) == T_HASH) {
            options = v;
        } else {
            io = v;
        }

    } else if(argc == 2) {
        io = argv[0];
        options = argv[1];
        if(rb_type(options) != T_HASH) {
            rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
        }

    } else {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
    }

    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    MessagePack_Buffer_set_options(b, io, options);

    return self;
}

Instance Method Details

#<<(string_or_buffer) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
# File 'ext/msgpack/buffer_class.c', line 290

static VALUE Buffer_append(VALUE self, VALUE string_or_buffer)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    VALUE string = string_or_buffer;  // TODO optimize if string_or_buffer is a Buffer
    StringValue(string);

    msgpack_buffer_append_string(b, string);

    return self;
}

#clearObject



254
255
256
257
258
259
# File 'ext/msgpack/buffer_class.c', line 254

static VALUE Buffer_clear(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    msgpack_buffer_clear(b);
    return Qnil;
}

#closeObject



560
561
562
563
564
565
566
567
# File 'ext/msgpack/buffer_class.c', line 560

static VALUE Buffer_close(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    if(b->io != Qnil) {
        return rb_funcall(b->io, s_close, 0);
    }
    return Qnil;
}

#empty?Boolean

Returns:

  • (Boolean)


268
269
270
271
272
273
274
275
276
# File 'ext/msgpack/buffer_class.c', line 268

static VALUE Buffer_empty_p(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    if(msgpack_buffer_top_readable_size(b) == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#flushObject



547
548
549
550
551
552
# File 'ext/msgpack/buffer_class.c', line 547

static VALUE Buffer_flush(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    msgpack_buffer_flush(b);
    return self;
}

#ioObject



554
555
556
557
558
# File 'ext/msgpack/buffer_class.c', line 554

static VALUE Buffer_io(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    return b->io;
}

#read(*args) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'ext/msgpack/buffer_class.c', line 476

static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
{
    VALUE out = Qnil;
    unsigned long n = -1;
    bool all = false;

    switch(argc) {
    case 2:
        out = argv[1];
        /* pass through */
    case 1:
        n = FIX2ULONG(argv[0]);
        break;
    case 0:
        all = true;
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
    }

    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    if(out != Qnil) {
        CHECK_STRING_TYPE(out);
    }

    if(all) {
        return read_all(b, out);
    }

    if(n == 0) {
        /* do nothing */
        MAKE_EMPTY_STRING(out);
        return out;
    }

    if(!msgpack_buffer_has_io(b) && out == Qnil &&
            msgpack_buffer_all_readable_size(b) <= n) {
        /* same as to_s && clear; optimize */
        VALUE str = msgpack_buffer_all_as_string(b);
        msgpack_buffer_clear(b);

        if(RSTRING_LEN(str) == 0) {
            return Qnil;
        } else {
            return str;
        }
    }

    MAKE_EMPTY_STRING(out);
    read_until_eof(b, out, n);

    if(RSTRING_LEN(out) == 0) {
        return Qnil;
    } else {
        return out;
    }
}

#read_all(*args) ⇒ Object



430
431
432
433
434
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'ext/msgpack/buffer_class.c', line 430

static VALUE Buffer_read_all(int argc, VALUE* argv, VALUE self)
{
    VALUE out = Qnil;
    unsigned long n = 0;
    bool all = false;

    switch(argc) {
    case 2:
        out = argv[1];
        /* pass through */
    case 1:
        n = FIX2ULONG(argv[0]);
        break;
    case 0:
        all = true;
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
    }

    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    if(out != Qnil) {
        CHECK_STRING_TYPE(out);
    }

    if(all) {
        return read_all(b, out);
    }

    if(n == 0) {
        /* do nothing */
        MAKE_EMPTY_STRING(out);
        return out;
    }

    if(!msgpack_buffer_ensure_readable(b, n)) {
        rb_raise(rb_eEOFError, "end of buffer reached");
    }

    MAKE_EMPTY_STRING(out);
    msgpack_buffer_read_to_string_nonblock(b, out, n);

    return out;
}

#sizeObject



261
262
263
264
265
266
# File 'ext/msgpack/buffer_class.c', line 261

static VALUE Buffer_size(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    size_t size = msgpack_buffer_all_readable_size(b);
    return SIZET2NUM(size);
}

#skip(sn) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/msgpack/buffer_class.c', line 395

static VALUE Buffer_skip(VALUE self, VALUE sn)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    unsigned long n = FIX2ULONG(sn);

    /* do nothing */
    if(n == 0) {
        return INT2NUM(0);
    }

    size_t sz = read_until_eof(b, Qnil, n);
    return SIZET2NUM(sz);
}

#skip_all(sn) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'ext/msgpack/buffer_class.c', line 410

static VALUE Buffer_skip_all(VALUE self, VALUE sn)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    unsigned long n = FIX2ULONG(sn);

    /* do nothing */
    if(n == 0) {
        return self;
    }

    if(!msgpack_buffer_ensure_readable(b, n)) {
        rb_raise(rb_eEOFError, "end of buffer reached");
    }

    msgpack_buffer_skip_nonblock(b, n);

    return self;
}

#to_aObject



541
542
543
544
545
# File 'ext/msgpack/buffer_class.c', line 541

static VALUE Buffer_to_a(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    return msgpack_buffer_all_as_string_array(b);
}

#to_strObject Also known as: to_s



535
536
537
538
539
# File 'ext/msgpack/buffer_class.c', line 535

static VALUE Buffer_to_str(VALUE self)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    return msgpack_buffer_all_as_string(b);
}

#write(string_or_buffer) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
# File 'ext/msgpack/buffer_class.c', line 278

static VALUE Buffer_write(VALUE self, VALUE string_or_buffer)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);

    VALUE string = string_or_buffer;  // TODO optimize if string_or_buffer is a Buffer
    StringValue(string);

    size_t length = msgpack_buffer_append_string(b, string);

    return SIZET2NUM(length);
}

#write_to(io) ⇒ Object



569
570
571
572
573
574
# File 'ext/msgpack/buffer_class.c', line 569

static VALUE Buffer_write_to(VALUE self, VALUE io)
{
    msgpack_buffer_t *b = MessagePack_Buffer_get(self);
    size_t sz = msgpack_buffer_flush_to_io(b, io, s_write, true);
    return SIZET2NUM(sz);
}