class BSON::ByteBuffer

Constants

BINARY_ENCODING
NULL_BYTE
UTF8_ENCODING

Attributes

max_size[R]
order[R]

Public Class Methods

new(initial_data="", max_size=BSON::DEFAULT_MAX_BSON_SIZE) click to toggle source
# File lib/bson/byte_buffer.rb, line 24
def initialize(initial_data="", max_size=BSON::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
  @order  = :little_endian
  @int_pack_order    = 'V'
  @double_pack_order = 'E'
  @max_size = max_size
end
serialize_cstr(buf, val) click to toggle source
# File lib/bson/byte_buffer.rb, line 66
def self.serialize_cstr(buf, val)
  buf.append!(to_utf8_binary(val.to_s))
  buf.append!(NULL_BYTE)
end
to_utf8_binary(str) click to toggle source
# File lib/bson/byte_buffer.rb, line 50
def self.to_utf8_binary(str)
  str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
end

Public Instance Methods

==(other) click to toggle source
# File lib/bson/byte_buffer.rb, line 235
def ==(other)
  other.respond_to?(:to_s) && @str == other.to_s
end
append!(buffer) click to toggle source

Appends a second ByteBuffer object, buffer, to the current buffer.

# File lib/bson/byte_buffer.rb, line 102
def append!(buffer)
  @str << buffer.to_s
  self
end
clear() click to toggle source
# File lib/bson/byte_buffer.rb, line 90
def clear
  @str = ""
  @str.force_encoding('binary') if @str.respond_to?(:force_encoding)
  rewind
end
dump() click to toggle source
# File lib/bson/byte_buffer.rb, line 251
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
get(len=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 180
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
get_double() click to toggle source
# File lib/bson/byte_buffer.rb, line 224
def get_double
  check_read_length(8)
  vals = @str[@cursor..@cursor+7]
  @cursor += 8
  vals.unpack(@double_pack_order)[0]
end
get_int() click to toggle source
# File lib/bson/byte_buffer.rb, line 207
def get_int
  check_read_length(4)
  vals = @str[@cursor..@cursor+3]
  @cursor += 4
  vals.unpack(@int_pack_order)[0]
end
get_long() click to toggle source
# File lib/bson/byte_buffer.rb, line 214
def get_long
  i1 = get_int
  i2 = get_int
  if @int_pack_order == 'N'
    (i1 << 32) + i2
  else
    (i2 << 32) + i1
  end
end
length()
Alias for: size
more?() click to toggle source
# File lib/bson/byte_buffer.rb, line 231
def more?
  @cursor < @str.size
end
order=(endianness) click to toggle source

endianness should be :little_endian or :big_endian. Default is :little_endian

# File lib/bson/byte_buffer.rb, line 72
def order=(endianness)
  @order = endianness
  @int_pack_order = endianness == :little_endian ? 'V' : 'N'
  @double_pack_order = endianness == :little_endian ? 'E' : 'G'
end
position() click to toggle source
# File lib/bson/byte_buffer.rb, line 82
def position
  @cursor
end
position=(val) click to toggle source
# File lib/bson/byte_buffer.rb, line 86
def position=(val)
  @cursor = val
end
prepend!(buffer) click to toggle source

Prepends a second ByteBuffer object, buffer, to the current buffer.

# File lib/bson/byte_buffer.rb, line 108
def prepend!(buffer)
  @str = buffer.to_s + @str
  self
end
put(byte, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 113
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
put_array(array, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 138
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
put_binary(data, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 124
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
put_double(d, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 171
def put_double(d, offset=nil)
  a = []
  [d].pack(@double_pack_order).each_byte { |b| a << b }
  put_array(a, offset)
end
put_int(i, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 149
def put_int(i, offset=nil)
  @cursor = offset if offset
  if more?
    @str[@cursor, 4] = [i].pack(@int_pack_order)
  else
    ensure_length(@cursor)
    @str << [i].pack(@int_pack_order)
  end
  @cursor += 4
end
put_long(i, offset=nil) click to toggle source
# File lib/bson/byte_buffer.rb, line 160
def put_long(i, offset=nil)
  offset = @cursor unless offset
  if @int_pack_order == 'N'
    put_int(i >> 32, offset)
    put_int(i & 0xffffffff, offset + 4)
  else
    put_int(i & 0xffffffff, offset)
    put_int(i >> 32, offset + 4)
  end
end
rewind() click to toggle source
# File lib/bson/byte_buffer.rb, line 78
def rewind
  @cursor = 0
end
size() click to toggle source
# File lib/bson/byte_buffer.rb, line 96
def size
  @str.size
end
Also aliased as: length
to_a(format="C*") click to toggle source
# File lib/bson/byte_buffer.rb, line 239
def to_a(format="C*")
  @str.unpack(format)
end
to_s() click to toggle source
# File lib/bson/byte_buffer.rb, line 247
def to_s
  @str
end
unpack(format="C*") click to toggle source
# File lib/bson/byte_buffer.rb, line 243
def unpack(format="C*")
  to_a(format)
end

Private Instance Methods

check_read_length(len) click to toggle source
# File lib/bson/byte_buffer.rb, line 274
def check_read_length(len)
  raise "attempt to read past end of buffer" if @cursor + len > @str.length
end
chr(byte) click to toggle source
# File lib/bson/byte_buffer.rb, line 266
def chr(byte)
  if byte < 0
    [byte].pack('c')
  else
    byte.chr
  end
end
ensure_length(length) click to toggle source
# File lib/bson/byte_buffer.rb, line 260
def ensure_length(length)
  if @str.size < length
    @str << NULL_BYTE * (length - @str.size)
  end
end