Changed BINARY BSON format based on spec change.

This commit is contained in:
Jim Menard 2009-01-20 09:03:43 -05:00
parent fa96e7c1cf
commit 62502a3f24

View File

@ -46,6 +46,8 @@ class BSON
NUMBER_INT = 16 NUMBER_INT = 16
MAXKEY = 127 MAXKEY = 127
BYTE_TYPE = 2
if RUBY_VERSION >= '1.9' if RUBY_VERSION >= '1.9'
def self.to_utf8(str) def self.to_utf8(str)
str.encode("utf-8") str.encode("utf-8")
@ -258,6 +260,8 @@ class BSON
end end
def deserialize_binary_data(buf) def deserialize_binary_data(buf)
buf.get_int # length + 4; ignored
buf.get # byte type; ignored
len = buf.get_int len = buf.get_int
bytes = buf.get(len) bytes = buf.get(len)
str = '' str = ''
@ -282,7 +286,7 @@ class BSON
def serialize_binary_element(buf, key, val) def serialize_binary_element(buf, key, val)
buf.put(BINARY) buf.put(BINARY)
self.class.serialize_cstr(buf, key) self.class.serialize_cstr(buf, key)
buf.put_int(val.length)
bytes = if RUBY_VERSION >= '1.9' bytes = if RUBY_VERSION >= '1.9'
val.bytes.to_a val.bytes.to_a
else else
@ -290,6 +294,11 @@ class BSON
val.each_byte { |byte| a << byte } val.each_byte { |byte| a << byte }
a a
end end
num_bytes = bytes.length
buf.put_int(num_bytes + 4)
buf.put(BYTE_TYPE)
buf.put_int(num_bytes)
buf.put_array(bytes) buf.put_array(bytes)
end end