class IO::Buffer

Constants

MAX_SIZE

Public Class Methods

default_node_size → 4096 click to toggle source

Retrieves the current value of the default node size.

static VALUE
IO_Buffer_default_node_size(VALUE klass)
{
    return UINT2NUM(default_node_size);
}
default_node_size = 16384 click to toggle source

Sets the default node size for calling IO::Buffer.new with no arguments.

static VALUE
IO_Buffer_set_default_node_size(VALUE klass, VALUE size)
{
    default_node_size = convert_node_size(size);

    return size;
}
new(size = IO::Buffer.default_node_size) → IO_Buffer click to toggle source

Create a new IO_Buffer with linked segments of the given size

static VALUE
IO_Buffer_initialize(int argc, VALUE * argv, VALUE self)
{
    VALUE           node_size_obj;
    struct buffer *buf;

    if (rb_scan_args(argc, argv, "01", &node_size_obj) == 1) {
        Data_Get_Struct(self, struct buffer, buf);

        /*
         * Make sure we're not changing the buffer size after data
         * has been allocated
         */
        assert(!buf->head);
        assert(!buf->pool_head);

        buf->node_size = convert_node_size(node_size_obj);
    }
    return Qnil;
}

Public Instance Methods

IO_Buffer#append(data) → String click to toggle source

Append the given data to the end of the buffer

static VALUE
IO_Buffer_append(VALUE self, VALUE data)
{
    struct buffer *buf;
    Data_Get_Struct(self, struct buffer, buf);

    /* Is this needed?  Never seen anyone else do it... */
    data = rb_convert_type(data, T_STRING, "String", "to_str");
    buffer_append(buf, RSTRING_PTR(data), RSTRING_LEN(data));

    return data;
}
Also aliased as: append, write
IO_Buffer#append(data) → String

Append the given data to the end of the buffer

Alias for: <<
IO_Buffer#clear → nil click to toggle source

Clear all data from the IO_Buffer

static VALUE
IO_Buffer_clear(VALUE self)
{
    struct buffer *buf;
    Data_Get_Struct(self, struct buffer, buf);

    buffer_clear(buf);

    return Qnil;
}
IO_Buffer#empty? → Boolean click to toggle source

Is the buffer empty?

static VALUE
IO_Buffer_empty(VALUE self)
{
    struct buffer *buf;
    Data_Get_Struct(self, struct buffer, buf);

    return buf->size > 0 ? Qfalse : Qtrue;
}
IO_Buffer#prepend(data) → String click to toggle source

Prepend the given data to the beginning of the buffer

static VALUE
IO_Buffer_prepend(VALUE self, VALUE data)
{
    struct buffer *buf;
    Data_Get_Struct(self, struct buffer, buf);

    data = rb_convert_type(data, T_STRING, "String", "to_str");
    buffer_prepend(buf, RSTRING_PTR(data), RSTRING_LEN(data));

    return data;
}
IO_Buffer#read(length = nil) → String click to toggle source

Read the specified abount of data from the buffer. If no value is given the entire contents of the buffer are returned. Any data read from the buffer is cleared. The given length must be greater than 0 or an exception would raise. If the buffer size is zero then an empty string is returned (regardless the given length).

static VALUE
IO_Buffer_read(int argc, VALUE * argv, VALUE self)
{
    VALUE  length_obj, str;
    int    length;
    struct buffer *buf;

    Data_Get_Struct(self, struct buffer, buf);

    if (rb_scan_args(argc, argv, "01", &length_obj) == 1) {
        length = NUM2INT(length_obj);
        if(length < 1)
          rb_raise(rb_eArgError, "length must be greater than zero");
        if(length > buf->size)
          length = buf->size;
    } else
        length = buf->size;

    if(buf->size == 0)
        return rb_str_new2("");

    str = rb_str_new(0, length);
    buffer_read(buf, RSTRING_PTR(str), length);

    return str;
}
IO_Buffer#read_frame(str, mark) → boolean click to toggle source

Read up to and including the given frame marker (expressed a a Fixnum 0-255) byte, copying into the supplied string object. If the mark is not encountered before the end of the buffer, false is returned but data is still copied into str. True is returned if the end of a frame is reached.

static VALUE
IO_Buffer_read_frame(VALUE self, VALUE data, VALUE mark)
{
    char mark_c = (char) NUM2INT(mark);
    struct buffer *buf;

    Data_Get_Struct(self, struct buffer, buf);

    if (buffer_read_frame(buf, data, mark_c)) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}
IO_Buffer#read_from(io) → Integer click to toggle source

Perform a nonblocking read of the the given IO object and fill the buffer with any data received. The call will read as much data as it can until the read would block.

static VALUE
IO_Buffer_read_from(VALUE self, VALUE io)
{
    struct buffer  *buf;
    int             ret;
#if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
    rb_io_t        *fptr;
#else
    OpenFile       *fptr;
#endif

    Data_Get_Struct(self, struct buffer, buf);
    io = rb_convert_type(io, T_FILE, "IO", "to_io");
    GetOpenFile(io, fptr);
    rb_io_set_nonblock(fptr);

#ifdef HAVE_RB_IO_DESCRIPTOR
    ret = buffer_read_from(buf, rb_io_descriptor(io));
#else
    ret = buffer_read_from(buf, FPTR_TO_FD(fptr));
#endif
    return ret == -1 ? Qnil : INT2NUM(ret);
}
IO_Buffer#size → Integer click to toggle source

Return the size of the buffer in bytes

static VALUE
IO_Buffer_size(VALUE self)
{
    struct buffer *buf;
    Data_Get_Struct(self, struct buffer, buf);

    return INT2NUM(buf->size);
}
IO_Buffer#to_str → String click to toggle source

Convert the Buffer to a String. The original buffer is unmodified.

static VALUE
IO_Buffer_to_str(VALUE self)
{
    VALUE          str;
    struct buffer *buf;

    Data_Get_Struct(self, struct buffer, buf);

    str = rb_str_new(0, buf->size);
    buffer_copy(buf, RSTRING_PTR(str), buf->size);

    return str;
}
IO_Buffer#append(data) → String

Append the given data to the end of the buffer

Alias for: <<
IO_Buffer#write_to(io) → Integer click to toggle source

Perform a nonblocking write of the buffer to the given IO object. As much data as possible is written until the call would block. Any data which is written is removed from the buffer.

static VALUE
IO_Buffer_write_to(VALUE self, VALUE io)
{
    struct buffer  *buf;
#if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
    rb_io_t        *fptr;
#else
    OpenFile       *fptr;
#endif

    Data_Get_Struct(self, struct buffer, buf);
    io = rb_convert_type(io, T_FILE, "IO", "to_io");
    GetOpenFile(io, fptr);
    rb_io_set_nonblock(fptr);

#ifdef HAVE_RB_IO_DESCRIPTOR
    return INT2NUM(buffer_write_to(buf, rb_io_descriptor(io)));
#else
    return INT2NUM(buffer_write_to(buf, FPTR_TO_FD(fptr)));
#endif
}