Method: Codec::Headerlength#encode

Defined in:
lib/codec/prefix.rb

#encode(buf, field) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/codec/prefix.rb', line 78

def encode(buf, field)
  # encode content
  content_buf = ""
  length = 0
  content = field.get_sub_field(@content_id)
  length  = @value_codec.encode(content_buf, content) unless content.nil?
  head = field.get_sub_field(@header_id)
  raise EncodingException.new "Missing header for encoding #{@id}" if head.empty?
  # update length field in header if length !=0
  head.set_value(length,@path,@separator) if length !=0
  # encode header
  head_buf =  ""
  h_len = @length_codec.encode(head_buf,head)
  # TODO : optimize computation for header length 
  if length != 0 && @total_length # re-encode header with total length
    length = head_buf.length + content_buf.length
    head.set_value(length,@path,@separator)
    head_buf = ""
    @length_codec.encode(head_buf,head)
  end
  buf << head_buf
  buf << content_buf
  return head_buf.length + content_buf.length
end