Class: Codec::Prefixedlength

Inherits:
Base
  • Object
show all
Defined in:
lib/codec/prefix.rb

Direct Known Subclasses

Headerlength, Tlv

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #get_sub_codecs

Constructor Details

#initialize(length, content) ⇒ Prefixedlength

Returns a new instance of Prefixedlength.



3
4
5
6
7
# File 'lib/codec/prefix.rb', line 3

def initialize(length,content)
  # TODO : Check that value_codec has a null length attribute
  @length_codec = length
  @value_codec = content
end

Instance Method Details

#decode(buffer, f, length = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/codec/prefix.rb', line 13

def decode(buffer, f, length=nil)
  Logger.warn {"Call decode with length on Prefixedlength codec should never happen"} unless length.nil?
  len_field = Field.new
  @length_codec.decode(buffer,len_field)
 len = get_length(len_field)
 if len == 0
  f.set_value("")
 else
   begin
    @value_codec.decode(buffer, f, len)
  rescue => e
    Logger.error "Error when decoding field #{f.get_id} \n #{e.message}\n#{e.backtrace.join(10.chr)}"
    raise ParsingException.new e.message
  end
 end
end

#encode(buf, field) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/codec/prefix.rb', line 30

def encode(buf, field)
  out = ""
  content_buf = ""
  len = @value_codec.encode(content_buf, field)
  @length_codec.encode(out, Field.new.set_value(len))
  out << content_buf
  buf << out
  return out.length
end

#get_length(length_field) ⇒ Object



9
10
11
# File 'lib/codec/prefix.rb', line 9

def get_length(length_field)
	length_field.get_value.to_i
end