class Beefcake::Buffer
Constants
- MaxInt32
- MaxInt64
- MaxUint32
- MaxUint64
- MinInt32
- MinInt64
- MinUint32
- MinUint64
- WIRES
Attributes
buf[R]
to_s[R]
to_str[R]
Public Class Methods
encodable?(type)
click to toggle source
# File lib/beefcake/buffer/base.rb, line 47 def self.encodable?(type) return false if ! type.is_a?(Class) type.public_method_defined?(:encode) end
new(buf="")
click to toggle source
# File lib/beefcake/buffer/base.rb, line 75 def initialize(buf="") self.buf = buf end
wire_for(type)
click to toggle source
# File lib/beefcake/buffer/base.rb, line 33 def self.wire_for(type) wire = WIRES[type] if wire wire elsif Class === type && encodable?(type) 2 elsif Module === type 0 else raise UnknownType, type end end
Public Instance Methods
<<(bytes)
click to toggle source
# File lib/beefcake/buffer/base.rb, line 89 def <<(bytes) bytes = bytes.force_encoding('BINARY') if bytes.respond_to? :force_encoding buf << bytes end
append(type, val, fn)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 7 def append(type, val, fn) if fn != 0 wire = Buffer.wire_for(type) append_info(fn, wire) end __send__("append_#{type}", val) end
append_bool(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 103 def append_bool(n) append_int64(n ? 1 : 0) end
append_double(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 99 def append_double(n) self << [n].pack("E") end
append_fixed32(n, tag=false)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 20 def append_fixed32(n, tag=false) if n < MinUint32 || n > MaxUint32 raise OutOfRangeError, n end self << [n].pack("V") end
append_fixed64(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 28 def append_fixed64(n) if n < MinUint64 || n > MaxUint64 raise OutOfRangeError, n end self << [n & 0xFFFFFFFF, n >> 32].pack("VV") end
append_float(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 95 def append_float(n) self << [n].pack("e") end
append_info(fn, wire)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 16 def append_info(fn, wire) append_uint32((fn << 3) | wire) end
append_int32(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 36 def append_int32(n) if n < MinInt32 || n > MaxInt32 raise OutOfRangeError, n end append_int64(n) end
append_int64(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 52 def append_int64(n) if n < MinInt64 || n > MaxInt64 raise OutOfRangeError, n end if n < 0 n += (1 << 64) end append_uint64(n) end
append_sfixed32(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 68 def append_sfixed32(n) append_fixed32((n << 1) ^ (n >> 31)) end
append_sfixed64(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 76 def append_sfixed64(n) append_fixed64((n << 1) ^ (n >> 63)) end
append_sint32(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 64 def append_sint32(n) append_uint32((n << 1) ^ (n >> 31)) end
append_sint64(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 72 def append_sint64(n) append_uint64((n << 1) ^ (n >> 63)) end
append_string(s)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 107 def append_string(s) actual_string = thaw_string s encoded = actual_string.dup.force_encoding 'binary' append_uint64(encoded.length) self << encoded end
Also aliased as: append_bytes
append_uint32(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 44 def append_uint32(n) if n < MinUint32 || n > MaxUint32 raise OutOfRangeError, n end append_uint64(n) end
append_uint64(n)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 80 def append_uint64(n) if n < MinUint64 || n > MaxUint64 raise OutOfRangeError, n end while true bits = n & 0x7F n >>= 7 if n == 0 return self << bits end self << (bits | 0x80) end end
buf=(new_buf)
click to toggle source
# File lib/beefcake/buffer/base.rb, line 79 def buf=(new_buf) @buf = new_buf.force_encoding('BINARY') @cursor = 0 end
length()
click to toggle source
# File lib/beefcake/buffer/base.rb, line 84 def length remain = buf.slice(@cursor..-1) remain.bytesize end
read(n)
click to toggle source
# File lib/beefcake/buffer/base.rb, line 94 def read(n) case n when Class n.decode(read_string) when Symbol __send__("read_#{n}") when Module read_uint64 else read_slice = buf.byteslice(@cursor, n) @cursor += n return read_slice end end
read_bool()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 83 def read_bool read_int32 != 0 end
read_bytes()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 15 def read_bytes read(read_uint64) end
read_double()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 78 def read_double bytes = read(8) bytes.unpack("E").first end
read_fixed32()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 23 def read_fixed32 bytes = read(4) bytes.unpack("V").first end
read_fixed64()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 28 def read_fixed64 bytes = read(8) x, y = bytes.unpack("VV") x + (y << 32) end
read_float()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 73 def read_float bytes = read(4) bytes.unpack("e").first end
read_info()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 7 def read_info n = read_uint64 fn = n >> 3 wire = n & 0x7 [fn, wire] end
read_int64()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 34 def read_int64 n = read_uint64 if n > MaxInt64 n -= (1 << 64) end n end
Also aliased as: read_int32
read_sfixed32()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 65 def read_sfixed32 decode_zigzag(read_fixed32) end
read_sfixed64()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 69 def read_sfixed64 decode_zigzag(read_fixed64) end
read_sint64()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 60 def read_sint64 decode_zigzag(read_uint64) end
Also aliased as: read_sint32
read_string()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 19 def read_string read_bytes.force_encoding Encoding.find('utf-8') end
read_uint64()
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 43 def read_uint64 n = shift = 0 while true if shift >= 64 raise BufferOverflowError, "varint" end b = read(1).ord n |= ((b & 0x7F) << shift) shift += 7 if (b & 0x80) == 0 return n end end end
Also aliased as: read_uint32
skip(wire)
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 87 def skip(wire) case wire when 0 then read_uint64 when 1 then read_fixed64 when 2 then read_string when 5 then read_fixed32 end end
Private Instance Methods
decode_zigzag(n)
click to toggle source
# File lib/beefcake/buffer/decode.rb, line 99 def decode_zigzag(n) (n >> 1) ^ -(n & 1) end
thaw_string(s)
click to toggle source
# File lib/beefcake/buffer/encode.rb, line 117 def thaw_string(s) if s.frozen? s = s.dup end s.to_s end