Class: BinaryCodec::BinarySerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/serdes/binary_serializer.rb

Instance Method Summary collapse

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.

Parameters:

  • bytes (Array<Integer>)

    The bytes to add.



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.

Parameters:



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.

Parameters:

  • bytes_list (BytesList)

    The bytes list to write.



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.

Parameters:

  • field (FieldInstance)

    The field to write.

  • value (Object)

    The value of the field.

  • is_unl_modify_workaround (Boolean) (defaults to: false)

    Whether to apply the UNLModify workaround.



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.

Parameters:

  • value (SerializedType)

    The value to write.

  • is_unl_modify_workaround (Boolean) (defaults to: false)

    Whether to apply the UNLModify workaround.



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.

Parameters:

  • type (Class)

    The class of the type (subclass of SerializedType).

  • value (Object)

    The value to serialize.



25
26
27
# File 'lib/binary-codec/serdes/binary_serializer.rb', line 25

def write_type(type, value)
  write(type.from(value))
end