class BSON::ByteBuffer
Public Class Methods
Initialize a byte buffer.
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self) { VALUE bytes; rb_scan_args(argc, argv, "01", &bytes); if (!NIL_P(bytes)) { rb_bson_byte_buffer_put_bytes(self, bytes); } return self; }
Public Instance Methods
Get a single byte from the buffer.
VALUE rb_bson_byte_buffer_get_byte(VALUE self) { byte_buffer_t *b; VALUE byte; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 1); byte = rb_str_new(READ_PTR(b), 1); b->read_position += 1; return byte; }
Get bytes from the buffer.
VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i) { byte_buffer_t *b; VALUE bytes; const uint32_t length = FIX2LONG(i); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, length); bytes = rb_str_new(READ_PTR(b), length); b->read_position += length; return bytes; }
Get a cstring from the buffer.
VALUE rb_bson_byte_buffer_get_cstring(VALUE self) { byte_buffer_t *b; VALUE string; int length; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); length = (int)strlen(READ_PTR(b)); ENSURE_BSON_READ(b, length); string = rb_enc_str_new(READ_PTR(b), length, rb_utf8_encoding()); b->read_position += length + 1; return string; }
Get the 16 bytes representing the decimal128 from the buffer.
VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self) { byte_buffer_t *b; VALUE bytes; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 16); bytes = rb_str_new(READ_PTR(b), 16); b->read_position += 16; return bytes; }
Get a double from the buffer.
VALUE rb_bson_byte_buffer_get_double(VALUE self) { byte_buffer_t *b; double d; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 8); memcpy(&d, READ_PTR(b), 8); b->read_position += 8; return DBL2NUM(BSON_DOUBLE_FROM_LE(d)); }
Get a int32 from the buffer.
VALUE rb_bson_byte_buffer_get_int32(VALUE self) { byte_buffer_t *b; int32_t i32; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 4); memcpy(&i32, READ_PTR(b), 4); b->read_position += 4; return INT2NUM(BSON_UINT32_FROM_LE(i32)); }
Get a int64 from the buffer.
VALUE rb_bson_byte_buffer_get_int64(VALUE self) { byte_buffer_t *b; int64_t i64; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 8); memcpy(&i64, READ_PTR(b), 8); b->read_position += 8; return LL2NUM(BSON_UINT64_FROM_LE(i64)); }
Get a string from the buffer.
VALUE rb_bson_byte_buffer_get_string(VALUE self) { byte_buffer_t *b; int32_t length; int32_t length_le; VALUE string; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_READ(b, 4); memcpy(&length, READ_PTR(b), 4); length_le = BSON_UINT32_FROM_LE(length); b->read_position += 4; ENSURE_BSON_READ(b, length_le); string = rb_enc_str_new(READ_PTR(b), length_le - 1, rb_utf8_encoding()); b->read_position += length_le; return string; }
Get the length of the buffer.
VALUE rb_bson_byte_buffer_length(VALUE self) { byte_buffer_t *b; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); return UINT2NUM(READ_SIZE(b)); }
Writes a byte to the byte buffer.
VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte) { byte_buffer_t *b; const char *str = RSTRING_PTR(byte); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, 1); memcpy(WRITE_PTR(b), str, 1); b->write_position += 1; return self; }
Writes bytes to the byte buffer.
VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes) { byte_buffer_t *b; const char *str = RSTRING_PTR(bytes); const size_t length = RSTRING_LEN(bytes); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, length); memcpy(WRITE_PTR(b), str, length); b->write_position += length; return self; }
Writes a cstring to the byte buffer.
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string) { byte_buffer_t *b; char *c_str = RSTRING_PTR(string); size_t length = RSTRING_LEN(string) + 1; if (!rb_bson_utf8_validate(c_str, length - 1, false)) { rb_raise(rb_eArgError, "String %s is not a valid UTF-8 CString.", c_str); } TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, length); memcpy(WRITE_PTR(b), c_str, length); b->write_position += length; return self; }
Writes a 128 bit decimal to the byte buffer.
VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high) { byte_buffer_t *b; const int64_t low64 = BSON_UINT64_TO_LE(NUM2ULL(low)); const int64_t high64 = BSON_UINT64_TO_LE(NUM2ULL(high)); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, 8); memcpy(WRITE_PTR(b), &low64, 8); b->write_position += 8; ENSURE_BSON_WRITE(b, 8); memcpy(WRITE_PTR(b), &high64, 8); b->write_position += 8; return self; }
Writes a 64 bit double to the buffer.
VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f) { byte_buffer_t *b; const double d = BSON_DOUBLE_TO_LE(NUM2DBL(f)); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, 8); memcpy(WRITE_PTR(b), &d, 8); b->write_position += 8; return self; }
Writes a 32 bit integer to the byte buffer.
VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i) { byte_buffer_t *b; const int32_t i32 = BSON_UINT32_TO_LE(NUM2INT(i)); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, 4); memcpy(WRITE_PTR(b), &i32, 4); b->write_position += 4; return self; }
Writes a 64 bit integer to the byte buffer.
VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i) { byte_buffer_t *b; const int64_t i64 = BSON_UINT64_TO_LE(NUM2LL(i)); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, 8); memcpy(WRITE_PTR(b), &i64, 8); b->write_position += 8; return self; }
Writes a string to the byte buffer.
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string) { byte_buffer_t *b; int32_t length_le; char *str = RSTRING_PTR(string); const int32_t length = RSTRING_LEN(string) + 1; length_le = BSON_UINT32_TO_LE(length); if (!rb_bson_utf8_validate(str, length - 1, true)) { rb_raise(rb_eArgError, "String %s is not valid UTF-8.", str); } TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); ENSURE_BSON_WRITE(b, length + 4); memcpy(WRITE_PTR(b), &length_le, 4); b->write_position += 4; memcpy(WRITE_PTR(b), str, length); b->write_position += length; return self; }
Get the read position.
VALUE rb_bson_byte_buffer_read_position(VALUE self) { byte_buffer_t *b; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); return INT2NUM(b->read_position); }
Replace a 32 bit integer int the byte buffer.
VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i) { byte_buffer_t *b; const int32_t position = NUM2LONG(index); const int32_t i32 = BSON_UINT32_TO_LE(NUM2LONG(i)); TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); memcpy(READ_PTR(b) + position, &i32, 4); return self; }
Reset the read position to the beginning of the byte buffer.
VALUE rb_bson_byte_buffer_rewind(VALUE self) { byte_buffer_t *b; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); b->read_position = 0; return self; }
Convert the buffer to a string.
VALUE rb_bson_byte_buffer_to_s(VALUE self) { byte_buffer_t *b; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); return rb_str_new(READ_PTR(b), READ_SIZE(b)); }
Get the write position.
VALUE rb_bson_byte_buffer_write_position(VALUE self) { byte_buffer_t *b; TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b); return INT2NUM(b->write_position); }