Class: BinaryCodec::BinarySerializer
- Inherits:
-
Object
- Object
- BinaryCodec::BinarySerializer
- Defined in:
- lib/binary-codec/serdes/binary_serializer.rb
Instance Method Summary collapse
-
#initialize(sink) ⇒ BinarySerializer
constructor
A new instance of BinarySerializer.
-
#put(bytes) ⇒ Object
Adds raw bytes to the sink.
-
#write(value) ⇒ Object
Serializes a value into the sink.
-
#write_bytes_list(bytes_list) ⇒ Object
Writes a BytesList into the sink.
-
#write_field_and_value(field, value, is_unl_modify_workaround = false) ⇒ Object
Writes a field and its value into the sink.
-
#write_length_encoded(value, is_unl_modify_workaround = false) ⇒ Object
Writes a value with its length encoded prefix.
-
#write_type(type, value) ⇒ Object
Serializes a value of a given type.
Constructor Details
#initialize(sink) ⇒ BinarySerializer
Returns a new instance of BinarySerializer.
6 7 8 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 6 def initialize(sink) @sink = sink || BytesList.new end |
Instance Method Details
#put(bytes) ⇒ Object
Adds raw bytes to the sink.
18 19 20 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 18 def put(bytes) @sink.put(bytes) end |
#write(value) ⇒ Object
Serializes a value into the sink.
12 13 14 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 12 def write(value) value.to_byte_sink(@sink) end |
#write_bytes_list(bytes_list) ⇒ Object
Writes a BytesList into the sink.
31 32 33 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 31 def write_bytes_list(bytes_list) bytes_list.to_byte_sink(@sink) end |
#write_field_and_value(field, value, is_unl_modify_workaround = false) ⇒ Object
Writes a field and its value into the sink.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 39 def write_field_and_value(field, value, is_unl_modify_workaround = false) field_header = field.header associated_value = field.associated_type.from(value) @sink.put(field_header.to_bytes) if field.is_variable_length_encoded write_length_encoded(associated_value, is_unl_modify_workaround) else associated_value.to_byte_sink(@sink) end end |
#write_length_encoded(value, is_unl_modify_workaround = false) ⇒ Object
Writes a value with its length encoded prefix.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 55 def write_length_encoded(value, is_unl_modify_workaround = false) bytes = BytesList.new unless is_unl_modify_workaround # This part doesn't happen for the Account field in a UNLModify transaction value.to_byte_sink(bytes) end self.put(encode_variable_length(bytes.get_length)) write_bytes_list(bytes) end |
#write_type(type, value) ⇒ Object
Serializes a value of a given type.
25 26 27 |
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 25 def write_type(type, value) write(type.from(value)) end |