Class: BSON::ByteBuffer
- Inherits:
-
Object
- Object
- BSON::ByteBuffer
- Defined in:
- lib/bson/byte_buffer.rb
Direct Known Subclasses
Constant Summary collapse
- NULL_BYTE =
"\0"
- UTF8_ENCODING =
Encoding.find('utf-8')
- BINARY_ENCODING =
Encoding.find('binary')
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
- #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_binary(data, 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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bson/byte_buffer.rb', line 25 def initialize(initial_data="") if initial_data.is_a?(String) if initial_data.respond_to?(:force_encoding) @str = initial_data.force_encoding('binary') else @str = initial_data end else @str = initial_data.pack('C*') end @cursor = @str.length @order = :little_endian @int_pack_order = 'V' @double_pack_order = 'E' end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
23 24 25 |
# File 'lib/bson/byte_buffer.rb', line 23 def order @order end |
Class Method Details
.serialize_cstr(buf, val) ⇒ Object
62 63 64 65 |
# File 'lib/bson/byte_buffer.rb', line 62 def self.serialize_cstr(buf, val) buf.append!(to_utf8_binary(val.to_s)) buf.append!(NULL_BYTE) end |
.to_utf8_binary(str) ⇒ Object
46 47 48 |
# File 'lib/bson/byte_buffer.rb', line 46 def self.to_utf8_binary(str) str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING) end |
Instance Method Details
#append!(buffer) ⇒ Object
Appends a second ByteBuffer object, buffer
, to the current buffer.
98 99 100 101 |
# File 'lib/bson/byte_buffer.rb', line 98 def append!(buffer) @str << buffer.to_s self end |
#clear ⇒ Object
86 87 88 89 90 |
# File 'lib/bson/byte_buffer.rb', line 86 def clear @str = "" @str.force_encoding('binary') if @str.respond_to?(:force_encoding) rewind end |
#dump ⇒ Object
243 244 245 246 247 248 249 |
# File 'lib/bson/byte_buffer.rb', line 243 def dump i = 0 @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
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bson/byte_buffer.rb', line 176 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 ⇒ Object
220 221 222 223 224 225 |
# File 'lib/bson/byte_buffer.rb', line 220 def get_double check_read_length(8) vals = @str[@cursor..@cursor+7] @cursor += 8 vals.unpack(@double_pack_order)[0] end |
#get_int ⇒ Object
203 204 205 206 207 208 |
# File 'lib/bson/byte_buffer.rb', line 203 def get_int check_read_length(4) vals = @str[@cursor..@cursor+3] @cursor += 4 vals.unpack(@int_pack_order)[0] end |
#get_long ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/bson/byte_buffer.rb', line 210 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
227 228 229 |
# File 'lib/bson/byte_buffer.rb', line 227 def more? @cursor < @str.size end |
#position ⇒ Object
78 79 80 |
# File 'lib/bson/byte_buffer.rb', line 78 def position @cursor end |
#position=(val) ⇒ Object
82 83 84 |
# File 'lib/bson/byte_buffer.rb', line 82 def position=(val) @cursor = val end |
#prepend!(buffer) ⇒ Object
Prepends a second ByteBuffer object, buffer
, to the current buffer.
104 105 106 107 |
# File 'lib/bson/byte_buffer.rb', line 104 def prepend!(buffer) @str = buffer.to_s + @str self end |
#put(byte, offset = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bson/byte_buffer.rb', line 109 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
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bson/byte_buffer.rb', line 134 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
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/bson/byte_buffer.rb', line 120 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
167 168 169 170 171 |
# File 'lib/bson/byte_buffer.rb', line 167 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
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/bson/byte_buffer.rb', line 145 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) ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/bson/byte_buffer.rb', line 156 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
74 75 76 |
# File 'lib/bson/byte_buffer.rb', line 74 def rewind @cursor = 0 end |
#size ⇒ Object Also known as: length
92 93 94 |
# File 'lib/bson/byte_buffer.rb', line 92 def size @str.size end |
#to_a ⇒ Object
231 232 233 |
# File 'lib/bson/byte_buffer.rb', line 231 def to_a @str.unpack("C*") end |
#to_s ⇒ Object
239 240 241 |
# File 'lib/bson/byte_buffer.rb', line 239 def to_s @str end |
#unpack(args) ⇒ Object
235 236 237 |
# File 'lib/bson/byte_buffer.rb', line 235 def unpack(args) to_a end |