Class: BERT::Encode
Constant Summary
Constants included
from Types
Types::ATOM, Types::BIN, Types::FLOAT, Types::FUN, Types::INT, Types::LARGE_BIGNUM, Types::LARGE_TUPLE, Types::LIST, Types::MAGIC, Types::MAX_INT, Types::MIN_INT, Types::NEW_FUN, Types::NIL, Types::PID, Types::SMALL_BIGNUM, Types::SMALL_INT, Types::SMALL_TUPLE, Types::STRING
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(out) ⇒ Encode
Returns a new instance of Encode.
8
9
10
|
# File 'lib/bert/encode.rb', line 8
def initialize(out)
self.out = out
end
|
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
6
7
8
|
# File 'lib/bert/encode.rb', line 6
def out
@out
end
|
Class Method Details
.encode(data) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/bert/encode.rb', line 12
def self.encode(data)
io = StringIO.new
self.new(io).write_any(data)
if io.string.respond_to?(:force_encoding)
io.string.force_encoding("ASCII-8BIT")
else
io.string
end
end
|
.write_byte(byte) ⇒ Object
43
44
45
|
# File 'lib/bert/encode.rb', line 43
def self.write_byte(byte)
[byte].pack("C")
end
|
.write_long(long) ⇒ Object
63
64
65
|
# File 'lib/bert/encode.rb', line 63
def self.write_long(long)
[long].pack("N")
end
|
.write_short(short) ⇒ Object
53
54
55
|
# File 'lib/bert/encode.rb', line 53
def self.write_short(short)
[short].pack("n")
end
|
.write_string(string) ⇒ Object
73
74
75
|
# File 'lib/bert/encode.rb', line 73
def self.write_string(string)
string
end
|
Instance Method Details
#write_1(byte) ⇒ Object
47
48
49
50
51
|
# File 'lib/bert/encode.rb', line 47
def write_1(byte)
out.write(
self.class.write_byte(byte)
)
end
|
#write_2(short) ⇒ Object
57
58
59
60
61
|
# File 'lib/bert/encode.rb', line 57
def write_2(short)
out.write(
self.class.write_short(short)
)
end
|
#write_4(long) ⇒ Object
67
68
69
70
71
|
# File 'lib/bert/encode.rb', line 67
def write_4(long)
out.write(
self.class.write_long(long)
)
end
|
#write_any(obj) ⇒ Object
24
25
26
27
|
# File 'lib/bert/encode.rb', line 24
def write_any obj
write_1 MAGIC
write_any_raw obj
end
|
#write_any_raw(obj) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/bert/encode.rb', line 29
def write_any_raw obj
case obj
when Symbol then write_symbol(obj)
when Fixnum, Bignum then write_fixnum(obj)
when Float then write_float(obj)
when Tuple then write_tuple(obj)
when Array then write_list(obj)
when String then write_binary(obj)
when Pid then write_pid(obj)
else
fail(obj)
end
end
|
#write_bignum(num) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/bert/encode.rb', line 120
def write_bignum(num)
n = (num.to_s(2).size / 8.0).ceil
if n < 256
write_1 SMALL_BIGNUM
write_1 n
write_bignum_guts(num)
else
write_1 LARGE_BIGNUM
write_4 n
write_bignum_guts(num)
end
end
|
#write_bignum_guts(num) ⇒ Object
133
134
135
136
137
138
139
140
141
|
# File 'lib/bert/encode.rb', line 133
def write_bignum_guts(num)
write_1 (num >= 0 ? 0 : 1)
num = num.abs
while num != 0
rem = num % 256
write_1 rem
num = num >> 8
end
end
|
#write_binary(data) ⇒ Object
166
167
168
169
170
|
# File 'lib/bert/encode.rb', line 166
def write_binary(data)
write_1 BIN
write_4 data.bytes.count
write_string data
end
|
#write_boolean(bool) ⇒ Object
83
84
85
|
# File 'lib/bert/encode.rb', line 83
def write_boolean(bool)
write_symbol(bool.to_s.to_sym)
end
|
#write_fixnum(num) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/bert/encode.rb', line 103
def write_fixnum(num)
if num >= 0 && num < 256
write_1 SMALL_INT
write_1 num
elsif num <= MAX_INT && num >= MIN_INT
write_1 INT
write_4 num
else
write_bignum num
end
end
|
#write_float(float) ⇒ Object
115
116
117
118
|
# File 'lib/bert/encode.rb', line 115
def write_float(float)
write_1 FLOAT
write_string format("%15.15e", float).ljust(31, "\000")
end
|
#write_list(data) ⇒ Object
157
158
159
160
161
162
163
164
|
# File 'lib/bert/encode.rb', line 157
def write_list(data)
fail(data) unless data.is_a? Array
write_1 NIL and return if data.empty?
write_1 LIST
write_4 data.length
data.each{|e| write_any_raw e }
write_1 NIL
end
|
#write_pid(pid) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/bert/encode.rb', line 87
def write_pid(pid)
write_1(PID)
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
77
78
79
80
81
|
# File 'lib/bert/encode.rb', line 77
def write_string(string)
out.write(
self.class.write_string(string)
)
end
|
#write_symbol(sym) ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/bert/encode.rb', line 95
def write_symbol(sym)
fail(sym) unless sym.is_a?(Symbol)
data = sym.to_s
write_1 ATOM
write_2 data.length
write_string data
end
|
#write_tuple(data) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/bert/encode.rb', line 143
def write_tuple(data)
fail(data) unless data.is_a? Array
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
|