Class: Tros::IO::BinaryEncoder
- Inherits:
-
Object
- Object
- Tros::IO::BinaryEncoder
- Defined in:
- lib/tros/io.rb
Overview
Write leaf values
Instance Attribute Summary collapse
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
-
#initialize(writer) ⇒ BinaryEncoder
constructor
A new instance of BinaryEncoder.
-
#write(datum) ⇒ Object
Write an arbritary datum.
-
#write_boolean(datum) ⇒ Object
a boolean is written as a single byte whose value is either 0 (false) or 1 (true).
-
#write_bytes(datum) ⇒ Object
Bytes are encoded as a long followed by that many bytes of data.
-
#write_double(datum) ⇒ Object
A double is written as 8 bytes.
-
#write_float(datum) ⇒ Object
A float is written as 4 bytes.
-
#write_long(n) ⇒ Object
(also: #write_int)
int and long values are written using variable-length, zig-zag coding.
-
#write_null(datum) ⇒ Object
null is written as zero bytes.
-
#write_string(datum) ⇒ Object
A string is encoded as a long followed by that many bytes of UTF-8 encoded character data.
Constructor Details
#initialize(writer) ⇒ BinaryEncoder
Returns a new instance of BinaryEncoder.
153 154 155 |
# File 'lib/tros/io.rb', line 153 def initialize(writer) @writer = writer end |
Instance Attribute Details
#writer ⇒ Object (readonly)
Returns the value of attribute writer.
151 152 153 |
# File 'lib/tros/io.rb', line 151 def writer @writer end |
Instance Method Details
#write(datum) ⇒ Object
Write an arbritary datum.
212 213 214 |
# File 'lib/tros/io.rb', line 212 def write(datum) writer.write(datum) end |
#write_boolean(datum) ⇒ Object
a boolean is written as a single byte whose value is either 0 (false) or 1 (true).
164 165 166 167 |
# File 'lib/tros/io.rb', line 164 def write_boolean(datum) on_disk = datum ? 1.chr : 0.chr writer.write(on_disk) end |
#write_bytes(datum) ⇒ Object
Bytes are encoded as a long followed by that many bytes of data.
200 201 202 203 |
# File 'lib/tros/io.rb', line 200 def write_bytes(datum) write_long(datum.bytesize) @writer.write(datum) end |
#write_double(datum) ⇒ Object
A double is written as 8 bytes. The double is converted into a 64-bit integer using a method equivalent to Java’s doubleToLongBits and then encoded in little-endian format.
195 196 197 |
# File 'lib/tros/io.rb', line 195 def write_double(datum) @writer.write([datum].pack('E')) end |
#write_float(datum) ⇒ Object
A float is written as 4 bytes. The float is converted into a 32-bit integer using a method equivalent to Java’s floatToIntBits and then encoded in little-endian format.
187 188 189 |
# File 'lib/tros/io.rb', line 187 def write_float(datum) @writer.write([datum].pack('e')) end |
#write_long(n) ⇒ Object Also known as: write_int
int and long values are written using variable-length, zig-zag coding.
171 172 173 174 175 176 177 178 179 |
# File 'lib/tros/io.rb', line 171 def write_long(n) foo = n n = (n << 1) ^ (n >> 63) while (n & ~0x7F) != 0 @writer.write(((n & 0x7f) | 0x80).chr) n >>= 7 end @writer.write(n.chr) end |
#write_null(datum) ⇒ Object
null is written as zero bytes
158 159 160 |
# File 'lib/tros/io.rb', line 158 def write_null(datum) nil end |
#write_string(datum) ⇒ Object
A string is encoded as a long followed by that many bytes of UTF-8 encoded character data
207 208 209 |
# File 'lib/tros/io.rb', line 207 def write_string(datum) write_bytes(datum.encode('UTF-8').force_encoding('binary')) end |