Class: BSON::ByteBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/bson/byte_buffer.rb

Direct Known Subclasses

Binary

Constant Summary collapse

INT32_PACK =
'l<'.freeze
INT64_PACK =
'q<'.freeze
DOUBLE_PACK =
'E'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = "", max_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ ByteBuffer

Returns a new instance of ByteBuffer.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bson/byte_buffer.rb', line 29

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
  @max_size = max_size
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



23
24
25
# File 'lib/bson/byte_buffer.rb', line 23

def max_size
  @max_size
end

#orderObject (readonly)

Returns the value of attribute order.



23
24
25
# File 'lib/bson/byte_buffer.rb', line 23

def order
  @order
end

Instance Method Details

#==(other) ⇒ Object



199
200
201
# File 'lib/bson/byte_buffer.rb', line 199

def ==(other)
  other.respond_to?(:to_s) && @str == other.to_s
end

#append!(buffer) ⇒ Object

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



71
72
73
74
# File 'lib/bson/byte_buffer.rb', line 71

def append!(buffer)
  @str << buffer.to_s
  self
end

#clearObject



59
60
61
62
63
# File 'lib/bson/byte_buffer.rb', line 59

def clear
  @str = ""
  @str.force_encoding('binary') if @str.respond_to?(:force_encoding)
  rewind
end

#dumpObject



215
216
217
218
219
220
# File 'lib/bson/byte_buffer.rb', line 215

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) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bson/byte_buffer.rb', line 147

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_doubleObject



188
189
190
191
192
193
# File 'lib/bson/byte_buffer.rb', line 188

def get_double
  check_read_length(8)
  vals = @str[@cursor..@cursor+7]
  @cursor += 8
  vals.unpack(DOUBLE_PACK)[0]
end

#get_intObject



174
175
176
177
178
179
# File 'lib/bson/byte_buffer.rb', line 174

def get_int
  check_read_length(4)
  vals = @str[@cursor..@cursor+3]
  @cursor += 4
  vals.unpack(INT32_PACK)[0]
end

#get_longObject



181
182
183
184
185
186
# File 'lib/bson/byte_buffer.rb', line 181

def get_long
  check_read_length(8)
  vals = @str[@cursor..@cursor+7]
  @cursor += 8
  vals.unpack(INT64_PACK)[0]
end

#more?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/bson/byte_buffer.rb', line 195

def more?
  @cursor < @str.size
end

#positionObject



51
52
53
# File 'lib/bson/byte_buffer.rb', line 51

def position
  @cursor
end

#position=(val) ⇒ Object



55
56
57
# File 'lib/bson/byte_buffer.rb', line 55

def position=(val)
  @cursor = val
end

#prepend!(buffer) ⇒ Object

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



77
78
79
80
# File 'lib/bson/byte_buffer.rb', line 77

def prepend!(buffer)
  @str = buffer.to_s + @str
  self
end

#put(byte, offset = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/bson/byte_buffer.rb', line 82

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) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/bson/byte_buffer.rb', line 107

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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bson/byte_buffer.rb', line 93

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) ⇒ Object



138
139
140
141
142
# File 'lib/bson/byte_buffer.rb', line 138

def put_double(d, offset=nil)
  a = []
  [d].pack(DOUBLE_PACK).each_byte { |b| a << b }
  put_array(a, offset)
end

#put_int(i, offset = nil) ⇒ Object



130
131
132
# File 'lib/bson/byte_buffer.rb', line 130

def put_int(i, offset=nil)
  put_num(i, offset, 4)
end

#put_long(i, offset = nil) ⇒ Object



134
135
136
# File 'lib/bson/byte_buffer.rb', line 134

def put_long(i, offset=nil)
  put_num(i, offset, 8)
end

#put_num(i, offset, bytes) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bson/byte_buffer.rb', line 118

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

#rewindObject



47
48
49
# File 'lib/bson/byte_buffer.rb', line 47

def rewind
  @cursor = 0
end

#sizeObject Also known as: length



65
66
67
# File 'lib/bson/byte_buffer.rb', line 65

def size
  @str.size
end

#to_a(format = "C*") ⇒ Object



203
204
205
# File 'lib/bson/byte_buffer.rb', line 203

def to_a(format="C*")
  @str.unpack(format)
end

#to_sObject



211
212
213
# File 'lib/bson/byte_buffer.rb', line 211

def to_s
  @str
end

#unpack(format = "C*") ⇒ Object



207
208
209
# File 'lib/bson/byte_buffer.rb', line 207

def unpack(format="C*")
  to_a(format)
end