Class: Erlang::Encoder
Constant Summary
Erlang::External::Types::ATOM, Erlang::External::Types::BIN, Erlang::External::Types::FLOAT, Erlang::External::Types::FUN, Erlang::External::Types::INT, Erlang::External::Types::LARGE_BIGNUM, Erlang::External::Types::LARGE_TUPLE, Erlang::External::Types::LIST, Erlang::External::Types::NEW_FLOAT, Erlang::External::Types::NEW_FUN, Erlang::External::Types::NEW_REF, Erlang::External::Types::NIL, Erlang::External::Types::PID, Erlang::External::Types::PORT, Erlang::External::Types::REF, Erlang::External::Types::SMALL_BIGNUM, Erlang::External::Types::SMALL_INT, Erlang::External::Types::SMALL_TUPLE, Erlang::External::Types::STRING
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Encoder.
7
8
9
|
# File 'lib/rinterface/erlang/encoder.rb', line 7
def initialize
@out = StringIO.new('', 'w')
end
|
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
6
7
8
|
# File 'lib/rinterface/erlang/encoder.rb', line 6
def out
@out
end
|
Instance Method Details
#rewind ⇒ Object
11
12
13
|
# File 'lib/rinterface/erlang/encoder.rb', line 11
def rewind
@out.rewind
end
|
#term_to_binary(obj) ⇒ Object
15
16
17
18
|
# File 'lib/rinterface/erlang/encoder.rb', line 15
def term_to_binary obj
write_1 External::VERSION
write_any_raw obj
end
|
#write_1(byte) ⇒ Object
34
35
36
|
# File 'lib/rinterface/erlang/encoder.rb', line 34
def write_1(byte)
@out.write([byte].pack("C"))
end
|
#write_2(short) ⇒ Object
38
39
40
|
# File 'lib/rinterface/erlang/encoder.rb', line 38
def write_2(short)
@out.write([short].pack("n"))
end
|
#write_4(long) ⇒ Object
42
43
44
|
# File 'lib/rinterface/erlang/encoder.rb', line 42
def write_4(long)
@out.write([long].pack("N"))
end
|
#write_any_raw(obj) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rinterface/erlang/encoder.rb', line 20
def write_any_raw obj
case obj
when Symbol then write_symbol(obj)
when Fixnum, Bignum then write_integer(obj)
when Float then write_double(obj)
when Array then write_tuple(obj)
when String then write_binary(obj)
when Pid then write_pid(obj)
when List then write_list(obj)
else
raise "Failed encoding!"
end
end
|
#write_binary(data) ⇒ Object
100
101
102
103
104
|
# File 'lib/rinterface/erlang/encoder.rb', line 100
def write_binary(data)
write_1 BIN
write_4 data.length
write_string data
end
|
#write_double(num) ⇒ Object
68
69
70
71
|
# File 'lib/rinterface/erlang/encoder.rb', line 68
def write_double(num)
write_1 NEW_FLOAT
@out.write([num].pack('G'))
end
|
#write_integer(num) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/rinterface/erlang/encoder.rb', line 58
def write_integer(num)
if 0 <= num && num < 256
write_1 SMALL_INT
write_1 num
else
write_1 INT
write_4 num
end
end
|
#write_list(list) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/rinterface/erlang/encoder.rb', line 92
def write_list(list)
len = list.data.size
write_1(108)
write_4(len)
list.data.each{ |i| write_any_raw i }
write_1(106)
end
|
#write_pid(pid) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/rinterface/erlang/encoder.rb', line 84
def write_pid(pid)
write_1(103)
write_symbol(pid.node)
write_4((pid.node_id & 0x7fff))
write_4((pid.serial & 0x1fff))
write_1((pid.creation & 0x3))
end
|
#write_string(string) ⇒ Object
46
47
48
|
# File 'lib/rinterface/erlang/encoder.rb', line 46
def write_string(string)
@out.write(string)
end
|
#write_symbol(sym) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/rinterface/erlang/encoder.rb', line 50
def write_symbol(sym)
data = sym.to_s
write_1 ATOM
write_2 data.length
write_string data
end
|
#write_tuple(data) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/rinterface/erlang/encoder.rb', line 73
def write_tuple(data)
if data.length < 256
write_1 SMALL_TUPLE
write_1 data.length
else
write_1 LARGE_TUPLE
write_4 data.length
end
data.each{|e| write_any_raw e }
end
|