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
VALUE rb_bson_byte_buffer_get_array(VALUE self){
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;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
return pvt_get_double(b);
}
VALUE rb_bson_byte_buffer_get_hash(VALUE self){
Get a int32 from the buffer.
VALUE rb_bson_byte_buffer_get_int32(VALUE self)
{
byte_buffer_t *b;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
return pvt_get_int32(b);
}
Get a int64 from the buffer.
VALUE rb_bson_byte_buffer_get_int64(VALUE self)
{
byte_buffer_t *b;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
return pvt_get_int64(b);
}
Get a string from the buffer.
VALUE rb_bson_byte_buffer_get_string(VALUE self)
{
byte_buffer_t *b;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
return pvt_get_string(b);
}
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));
}
serializes an array into the byte buffer
VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array, VALUE validating_keys){
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;
if (!RB_TYPE_P(byte, T_STRING))
rb_raise(rb_eArgError, "Invalid input");
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;
size_t length;
if (!RB_TYPE_P(bytes, T_STRING) && !RB_TYPE_P(bytes, RUBY_T_DATA))
rb_raise(rb_eArgError, "Invalid input");
str = RSTRING_PTR(bytes);
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. This magically supports both Ruby symbols and strings.
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string)
{
int32_t length;
if (TYPE(string) == T_SYMBOL) {
const char *sym = rb_id2name(SYM2ID(string));
length = strlen(sym);
return rb_bson_byte_buffer_put_bson_partial_string(self, sym, length);
} else if (TYPE(string) == T_STRING) {
const char *str = RSTRING_PTR(string);
length = RSTRING_LEN(string);
return rb_bson_byte_buffer_put_bson_partial_string(self, str, length);
} else if (TYPE(string) == T_FIXNUM) {
const char *str = RSTRING_PTR(rb_fix2str(string, 10));
length = strlen(str);
return rb_bson_byte_buffer_put_bson_partial_string(self, str, length);
} else {
rb_raise(rb_eTypeError, "Invalid type for string");
}
}
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;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
pvt_put_double(b,NUM2DBL(f));
return self;
}
serializes a hash into the byte buffer
VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash, VALUE validating_keys){
byte_buffer_t *b = NULL;
put_hash_context context = {0};
size_t position = 0;
size_t new_position = 0;
int32_t new_length = 0;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
Check_Type(hash, T_HASH);
position = READ_SIZE(b);
/* insert length placeholder */
pvt_put_int32(b, 0);
context.buffer = self;
context.validating_keys = validating_keys;
context.b = b;
rb_hash_foreach(hash, put_hash_callback, (VALUE)&context);
pvt_put_byte(b, 0);
/* update length placeholder with actual value */
new_position = READ_SIZE(b);
new_length = new_position - position;
pvt_replace_int32(b, position, new_length);
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 = NUM2INT(i);
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
pvt_put_int32(b, i32);
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 = NUM2LL(i);
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
pvt_put_int64(b, i64);
return self;
}
Writes a string to the byte buffer.
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
{
const char *str = RSTRING_PTR(string);
const int32_t length = RSTRING_LEN(string) + 1;
return rb_bson_byte_buffer_put_bson_string(self, str, length);
}
Writes a symbol to the byte buffer.
VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol)
{
const char *sym = rb_id2name(SYM2ID(symbol));
const int32_t length = strlen(sym) + 1;
return rb_bson_byte_buffer_put_bson_string(self, sym, length);
}
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;
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
pvt_replace_int32(b, NUM2LONG(index), NUM2LONG(i));
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);
}