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
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#order ⇒ Object
Returns the value of attribute order.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#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 = "", max_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ 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(format = "C*") ⇒ Object
- #to_s ⇒ Object
- #unpack(format = "C*") ⇒ Object
Constructor Details
#initialize(initial_data = "", max_size = BSON::DEFAULT_MAX_BSON_SIZE) ⇒ ByteBuffer
Returns a new instance of ByteBuffer.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bson/byte_buffer.rb', line 25 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 @order = :little_endian @int_pack_order = 'V' @double_pack_order = 'E' @max_size = max_size end |
Instance Attribute Details
#max_size ⇒ Object (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 |
#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
67 68 69 70 |
# File 'lib/bson/byte_buffer.rb', line 67 def self.serialize_cstr(buf, val) buf.append!(to_utf8_binary(val.to_s)) buf.append!(NULL_BYTE) end |
.to_utf8_binary(str) ⇒ Object
51 52 53 |
# File 'lib/bson/byte_buffer.rb', line 51 def self.to_utf8_binary(str) str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING) end |
Instance Method Details
#==(other) ⇒ Object
236 237 238 |
# File 'lib/bson/byte_buffer.rb', line 236 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.
103 104 105 106 |
# File 'lib/bson/byte_buffer.rb', line 103 def append!(buffer) @str << buffer.to_s self end |
#clear ⇒ Object
91 92 93 94 95 |
# File 'lib/bson/byte_buffer.rb', line 91 def clear @str = "" @str.force_encoding('binary') if @str.respond_to?(:force_encoding) rewind end |
#dump ⇒ Object
252 253 254 255 256 257 |
# File 'lib/bson/byte_buffer.rb', line 252 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
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/bson/byte_buffer.rb', line 181 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
225 226 227 228 229 230 |
# File 'lib/bson/byte_buffer.rb', line 225 def get_double check_read_length(8) vals = @str[@cursor..@cursor+7] @cursor += 8 vals.unpack(@double_pack_order)[0] end |
#get_int ⇒ Object
208 209 210 211 212 213 |
# File 'lib/bson/byte_buffer.rb', line 208 def get_int check_read_length(4) vals = @str[@cursor..@cursor+3] @cursor += 4 vals.unpack(@int_pack_order)[0] end |
#get_long ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'lib/bson/byte_buffer.rb', line 215 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
232 233 234 |
# File 'lib/bson/byte_buffer.rb', line 232 def more? @cursor < @str.size end |
#position ⇒ Object
83 84 85 |
# File 'lib/bson/byte_buffer.rb', line 83 def position @cursor end |
#position=(val) ⇒ Object
87 88 89 |
# File 'lib/bson/byte_buffer.rb', line 87 def position=(val) @cursor = val end |
#prepend!(buffer) ⇒ Object
Prepends a second ByteBuffer object, buffer
, to the current buffer.
109 110 111 112 |
# File 'lib/bson/byte_buffer.rb', line 109 def prepend!(buffer) @str = buffer.to_s + @str self end |
#put(byte, offset = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bson/byte_buffer.rb', line 114 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
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bson/byte_buffer.rb', line 139 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
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bson/byte_buffer.rb', line 125 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
172 173 174 175 176 |
# File 'lib/bson/byte_buffer.rb', line 172 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
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/bson/byte_buffer.rb', line 150 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
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bson/byte_buffer.rb', line 161 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
79 80 81 |
# File 'lib/bson/byte_buffer.rb', line 79 def rewind @cursor = 0 end |
#size ⇒ Object Also known as: length
97 98 99 |
# File 'lib/bson/byte_buffer.rb', line 97 def size @str.size end |
#to_a(format = "C*") ⇒ Object
240 241 242 |
# File 'lib/bson/byte_buffer.rb', line 240 def to_a(format="C*") @str.unpack(format) end |
#to_s ⇒ Object
248 249 250 |
# File 'lib/bson/byte_buffer.rb', line 248 def to_s @str end |
#unpack(format = "C*") ⇒ Object
244 245 246 |
# File 'lib/bson/byte_buffer.rb', line 244 def unpack(format="C*") to_a(format) end |