# File lib/bson/byte_buffer.rb, line 24 def initialize(initial_data="", max_size=DEFAULT_MAX_BSON_SIZE) @str = case initial_data when String then if initial_data.respond_to?(:force_encoding) initial_data.force_encoding('binary') else initial_data end when BSON::ByteBuffer then initial_data.to_a.pack('C*') else initial_data.pack('C*') end @cursor = @str.length @max_size = max_size end
# File lib/bson/byte_buffer.rb, line 194 def ==(other) other.respond_to?(:to_s) && @str == other.to_s end
Appends a second ByteBuffer object,
buffer
, to the current buffer.
# File lib/bson/byte_buffer.rb, line 66 def append!(buffer) @str << buffer.to_s self end
# File lib/bson/byte_buffer.rb, line 54 def clear @str = "" @str.force_encoding('binary') if @str.respond_to?(:force_encoding) rewind end
# File lib/bson/byte_buffer.rb, line 210 def dump @str.each_byte do |c, i| $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}" i += 1 end end
# File lib/bson/byte_buffer.rb, line 142 def get(len=nil) one_byte = len.nil? len ||= 1 check_read_length(len) start = @cursor @cursor += len if one_byte @str[start] else @str[start, len].unpack("C*") end end
# File lib/bson/byte_buffer.rb, line 183 def get_double check_read_length(8) vals = @str[@cursor..@cursor+7] @cursor += 8 vals.unpack(DOUBLE_PACK)[0] end
# File lib/bson/byte_buffer.rb, line 169 def get_int check_read_length(4) vals = @str[@cursor..@cursor+3] @cursor += 4 vals.unpack(INT32_PACK)[0] end
# File lib/bson/byte_buffer.rb, line 176 def get_long check_read_length(8) vals = @str[@cursor..@cursor+7] @cursor += 8 vals.unpack(INT64_PACK)[0] end
# File lib/bson/byte_buffer.rb, line 190 def more? @cursor < @str.size end
# File lib/bson/byte_buffer.rb, line 46 def position @cursor end
# File lib/bson/byte_buffer.rb, line 50 def position=(val) @cursor = val end
Prepends a second ByteBuffer object,
buffer
, to the current buffer.
# File lib/bson/byte_buffer.rb, line 72 def prepend!(buffer) @str = buffer.to_s + @str self end
# File lib/bson/byte_buffer.rb, line 77 def put(byte, offset=nil) @cursor = offset if offset if more? @str[@cursor] = chr(byte) else ensure_length(@cursor) @str << chr(byte) end @cursor += 1 end
# File lib/bson/byte_buffer.rb, line 102 def put_array(array, offset=nil) @cursor = offset if offset if more? @str[@cursor, array.length] = array.pack("C*") else ensure_length(@cursor) @str << array.pack("C*") end @cursor += array.length end
# File lib/bson/byte_buffer.rb, line 88 def put_binary(data, offset=nil) @cursor = offset if offset if defined?(BINARY_ENCODING) data = data.dup.force_encoding(BINARY_ENCODING) end if more? @str[@cursor, data.length] = data else ensure_length(@cursor) @str << data end @cursor += data.length end
# File lib/bson/byte_buffer.rb, line 133 def put_double(d, offset=nil) a = [] [d].pack(DOUBLE_PACK).each_byte { |b| a << b } put_array(a, offset) end
# File lib/bson/byte_buffer.rb, line 125 def put_int(i, offset=nil) put_num(i, offset, 4) end
# File lib/bson/byte_buffer.rb, line 129 def put_long(i, offset=nil) put_num(i, offset, 8) end
# File lib/bson/byte_buffer.rb, line 113 def put_num(i, offset, bytes) pack_type = bytes == 4 ? INT32_PACK : INT64_PACK @cursor = offset if offset if more? @str[@cursor, bytes] = [i].pack(pack_type) else ensure_length(@cursor) @str << [i].pack(pack_type) end @cursor += bytes end
# File lib/bson/byte_buffer.rb, line 42 def rewind @cursor = 0 end
# File lib/bson/byte_buffer.rb, line 60 def size @str.size end
# File lib/bson/byte_buffer.rb, line 198 def to_a(format="C*") @str.unpack(format) end
# File lib/bson/byte_buffer.rb, line 206 def to_s @str end
# File lib/bson/byte_buffer.rb, line 202 def unpack(format="C*") to_a(format) end
# File lib/bson/byte_buffer.rb, line 233 def check_read_length(len) raise "attempt to read past end of buffer" if @cursor + len > @str.length end
# File lib/bson/byte_buffer.rb, line 225 def chr(byte) if byte < 0 [byte].pack('c') else byte.chr end end
# File lib/bson/byte_buffer.rb, line 219 def ensure_length(length) if @str.size < length @str << NULL_BYTE * (length - @str.size) end end