Class: ByteBuffer

Inherits:
Object show all
Defined in:
lib/mongo/util/byte_buffer.rb

Overview

A byte buffer.

Direct Known Subclasses

Mongo::Binary

Constant Summary collapse

INT_LOOKUP =

Commonly-used integers.

{
  0    => [0, 0, 0, 0],
  1    => [1, 0, 0, 0],
  2    => [2, 0, 0, 0],
  3    => [3, 0, 0, 0],
  4    => [4, 0, 0, 0],
  2001 => [209, 7, 0, 0],
  2002 => [210, 7, 0, 0],
  2004 => [212, 7, 0, 0],
  2005 => [213, 7, 0, 0],
  2006 => [214, 7, 0, 0]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = []) ⇒ ByteBuffer

Returns a new instance of ByteBuffer.



36
37
38
39
40
41
42
# File 'lib/mongo/util/byte_buffer.rb', line 36

def initialize(initial_data=[])
  @buf    = initial_data
  @cursor = @buf.length
  @order  = :little_endian
  @int_pack_order    = 'V'
  @double_pack_order = 'E'
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



34
35
36
# File 'lib/mongo/util/byte_buffer.rb', line 34

def order
  @order
end

Class Method Details

.serialize_cstr(buf, val) ⇒ Object



59
60
61
# File 'lib/mongo/util/byte_buffer.rb', line 59

def self.serialize_cstr(buf, val)
  buf.put_array(to_utf8(val.to_s).unpack("C*") + [0])
end

.to_utf8(str) ⇒ Object



45
46
47
# File 'lib/mongo/util/byte_buffer.rb', line 45

def self.to_utf8(str)
  str.encode("utf-8")
end

Instance Method Details

#append!(buffer) ⇒ Object

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



93
94
95
96
# File 'lib/mongo/util/byte_buffer.rb', line 93

def append!(buffer)
  @buf = @buf + buffer.to_a
  self
end

#clearObject



82
83
84
85
# File 'lib/mongo/util/byte_buffer.rb', line 82

def clear
  @buf = []
  rewind
end

#dumpObject



212
213
214
# File 'lib/mongo/util/byte_buffer.rb', line 212

def dump
  @buf.each_with_index { |c, i| $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}" }
end

#get(len = nil) ⇒ Object

If size == nil, returns one byte. Else returns array of bytes of length # size.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mongo/util/byte_buffer.rb', line 143

def get(len=nil)
  one_byte = len.nil?
  len ||= 1
  check_read_length(len)
  start = @cursor
  @cursor += len
  if one_byte
    @buf[start]
  else
    if @buf.respond_to? "unpack"
      @buf[start, len].unpack("C*")
    else
      @buf[start, len]
    end
  end
end

#get_doubleObject



178
179
180
181
182
183
184
# File 'lib/mongo/util/byte_buffer.rb', line 178

def get_double
  check_read_length(8)
  vals = ""
  (@cursor..@cursor+7).each { |i| vals << @buf[i].chr }
  @cursor += 8
  vals.unpack(@double_pack_order)[0]
end

#get_intObject



160
161
162
163
164
165
166
# File 'lib/mongo/util/byte_buffer.rb', line 160

def get_int
  check_read_length(4)
  vals = ""
  (@cursor..@cursor+3).each { |i| vals << @buf[i].chr }
  @cursor += 4
  vals.unpack(@int_pack_order)[0]
end

#get_longObject



168
169
170
171
172
173
174
175
176
# File 'lib/mongo/util/byte_buffer.rb', line 168

def get_long
  i1 = get_int
  i2 = get_int
  if @int_pack_order == 'N'
    (i1 << 32) + i2
  else
    (i2 << 32) + i1
  end
end

#more?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/mongo/util/byte_buffer.rb', line 186

def more?
  @cursor < @buf.size
end

#positionObject



74
75
76
# File 'lib/mongo/util/byte_buffer.rb', line 74

def position
  @cursor
end

#position=(val) ⇒ Object



78
79
80
# File 'lib/mongo/util/byte_buffer.rb', line 78

def position=(val)
  @cursor = val
end

#prepend!(buffer) ⇒ Object

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



99
100
101
102
# File 'lib/mongo/util/byte_buffer.rb', line 99

def prepend!(buffer)
  @buf = buffer.to_a + @buf
  self
end

#put(byte, offset = nil) ⇒ Object



104
105
106
107
108
# File 'lib/mongo/util/byte_buffer.rb', line 104

def put(byte, offset=nil)
  @cursor = offset if offset
  @buf[@cursor] = byte
  @cursor += 1
end

#put_array(array, offset = nil) ⇒ Object



110
111
112
113
114
# File 'lib/mongo/util/byte_buffer.rb', line 110

def put_array(array, offset=nil)
  @cursor = offset if offset
  @buf[@cursor, array.length] = array
  @cursor += array.length
end

#put_double(d, offset = nil) ⇒ Object



135
136
137
138
139
# File 'lib/mongo/util/byte_buffer.rb', line 135

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



116
117
118
119
120
121
122
# File 'lib/mongo/util/byte_buffer.rb', line 116

def put_int(i, offset=nil)
  unless a = INT_LOOKUP[i]
    a = []
    [i].pack(@int_pack_order).each_byte { |b| a << b }
  end
  put_array(a, offset)
end

#put_long(i, offset = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/mongo/util/byte_buffer.rb', line 124

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

#rewindObject



70
71
72
# File 'lib/mongo/util/byte_buffer.rb', line 70

def rewind
  @cursor = 0
end

#sizeObject Also known as: length



87
88
89
# File 'lib/mongo/util/byte_buffer.rb', line 87

def size
  @buf.size
end

#to_aObject



190
191
192
193
194
195
196
# File 'lib/mongo/util/byte_buffer.rb', line 190

def to_a
  if @buf.respond_to? "unpack"
    @buf.unpack("C*")
  else
    @buf
  end
end

#to_sObject



202
203
204
205
206
207
208
209
210
# File 'lib/mongo/util/byte_buffer.rb', line 202

def to_s
  if @buf.respond_to? :fast_pack
    @buf.fast_pack
  elsif @buf.respond_to? "pack"
    @buf.pack("C*")
  else
    @buf
  end
end

#unpack(args) ⇒ Object



198
199
200
# File 'lib/mongo/util/byte_buffer.rb', line 198

def unpack(args)
  to_a
end