Class: ByteBuffer

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

Overview

A byte buffer.

Direct Known Subclasses

Mongo::Binary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_data = []) ⇒ ByteBuffer

Returns a new instance of ByteBuffer.



22
23
24
25
26
# File 'lib/mongo/util/byte_buffer.rb', line 22

def initialize(initial_data=[])
  @buf = initial_data
  @cursor = 0
  self.order = :little_endian
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



20
21
22
# File 'lib/mongo/util/byte_buffer.rb', line 20

def order
  @order
end

Instance Method Details

#clearObject



47
48
49
50
# File 'lib/mongo/util/byte_buffer.rb', line 47

def clear
  @buf = []
  rewind
end

#dumpObject



157
158
159
# File 'lib/mongo/util/byte_buffer.rb', line 157

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mongo/util/byte_buffer.rb', line 94

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



129
130
131
132
133
134
135
# File 'lib/mongo/util/byte_buffer.rb', line 129

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



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

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



119
120
121
122
123
124
125
126
127
# File 'lib/mongo/util/byte_buffer.rb', line 119

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)


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

def more?
  @cursor < @buf.size
end

#positionObject



39
40
41
# File 'lib/mongo/util/byte_buffer.rb', line 39

def position
  @cursor
end

#position=(val) ⇒ Object



43
44
45
# File 'lib/mongo/util/byte_buffer.rb', line 43

def position=(val)
  @cursor = val
end

#put(byte, offset = nil) ⇒ Object



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

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

#put_array(array, offset = nil) ⇒ Object



63
64
65
66
67
# File 'lib/mongo/util/byte_buffer.rb', line 63

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



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

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



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

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

#put_long(i, offset = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/mongo/util/byte_buffer.rb', line 75

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



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

def rewind
  @cursor = 0
end

#sizeObject Also known as: length



52
53
54
# File 'lib/mongo/util/byte_buffer.rb', line 52

def size
  @buf.size
end

#to_aObject



141
142
143
144
145
146
147
# File 'lib/mongo/util/byte_buffer.rb', line 141

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

#to_sObject



149
150
151
152
153
154
155
# File 'lib/mongo/util/byte_buffer.rb', line 149

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