Class: BSON::ByteBuffer
- Inherits:
-
Object
- Object
- BSON::ByteBuffer
- Defined in:
- lib/bson/byte_buffer.rb
Direct Known Subclasses
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
-
#order ⇒ Object
Returns the value of attribute order.
Class Method Summary collapse
Instance Method Summary collapse
-
#append!(buffer) ⇒ Object
Appends a second ByteBuffer object,
buffer
, to the current buffer. - #clear ⇒ Object
- #dump ⇒ Object
-
#get(len = nil) ⇒ Object
If
size
== nil, returns one byte. - #get_double ⇒ Object
- #get_int ⇒ Object
- #get_long ⇒ Object
-
#initialize(initial_data = []) ⇒ ByteBuffer
constructor
A new instance of ByteBuffer.
- #more? ⇒ Boolean
- #position ⇒ Object
- #position=(val) ⇒ Object
-
#prepend!(buffer) ⇒ Object
Prepends a second ByteBuffer object,
buffer
, to the current buffer. - #put(byte, offset = nil) ⇒ Object
- #put_array(array, offset = nil) ⇒ Object
- #put_double(d, offset = nil) ⇒ Object
- #put_int(i, offset = nil) ⇒ Object
- #put_long(i, offset = nil) ⇒ Object
- #rewind ⇒ Object
- #size ⇒ Object (also: #length)
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unpack(args) ⇒ Object
Constructor Details
#initialize(initial_data = []) ⇒ ByteBuffer
Returns a new instance of ByteBuffer.
39 40 41 42 43 44 45 |
# File 'lib/bson/byte_buffer.rb', line 39 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
#order ⇒ Object
Returns the value of attribute order.
37 38 39 |
# File 'lib/bson/byte_buffer.rb', line 37 def order @order end |
Class Method Details
.serialize_cstr(buf, val) ⇒ Object
62 63 64 |
# File 'lib/bson/byte_buffer.rb', line 62 def self.serialize_cstr(buf, val) buf.put_array(to_utf8(val.to_s).unpack("C*") + [0]) end |
.to_utf8(str) ⇒ Object
48 49 50 |
# File 'lib/bson/byte_buffer.rb', line 48 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.
96 97 98 99 |
# File 'lib/bson/byte_buffer.rb', line 96 def append!(buffer) @buf = @buf + buffer.to_a self end |
#clear ⇒ Object
85 86 87 88 |
# File 'lib/bson/byte_buffer.rb', line 85 def clear @buf = [] rewind end |
#dump ⇒ Object
215 216 217 |
# File 'lib/bson/byte_buffer.rb', line 215 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
.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bson/byte_buffer.rb', line 146 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_double ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/bson/byte_buffer.rb', line 181 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_int ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/bson/byte_buffer.rb', line 163 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_long ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/bson/byte_buffer.rb', line 171 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
189 190 191 |
# File 'lib/bson/byte_buffer.rb', line 189 def more? @cursor < @buf.size end |
#position ⇒ Object
77 78 79 |
# File 'lib/bson/byte_buffer.rb', line 77 def position @cursor end |
#position=(val) ⇒ Object
81 82 83 |
# File 'lib/bson/byte_buffer.rb', line 81 def position=(val) @cursor = val end |
#prepend!(buffer) ⇒ Object
Prepends a second ByteBuffer object, buffer
, to the current buffer.
102 103 104 105 |
# File 'lib/bson/byte_buffer.rb', line 102 def prepend!(buffer) @buf = buffer.to_a + @buf self end |
#put(byte, offset = nil) ⇒ Object
107 108 109 110 111 |
# File 'lib/bson/byte_buffer.rb', line 107 def put(byte, offset=nil) @cursor = offset if offset @buf[@cursor] = byte @cursor += 1 end |
#put_array(array, offset = nil) ⇒ Object
113 114 115 116 117 |
# File 'lib/bson/byte_buffer.rb', line 113 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
138 139 140 141 142 |
# File 'lib/bson/byte_buffer.rb', line 138 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
119 120 121 122 123 124 125 |
# File 'lib/bson/byte_buffer.rb', line 119 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
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/bson/byte_buffer.rb', line 127 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 ⇒ Object
73 74 75 |
# File 'lib/bson/byte_buffer.rb', line 73 def rewind @cursor = 0 end |
#size ⇒ Object Also known as: length
90 91 92 |
# File 'lib/bson/byte_buffer.rb', line 90 def size @buf.size end |
#to_a ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/bson/byte_buffer.rb', line 193 def to_a if @buf.respond_to? "unpack" @buf.unpack("C*") else @buf end end |
#to_s ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/bson/byte_buffer.rb', line 205 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
201 202 203 |
# File 'lib/bson/byte_buffer.rb', line 201 def unpack(args) to_a end |