Class: Codec::Prefixedlength

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

Direct Known Subclasses

Headerlength, Tlv

Instance Attribute Summary

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #decode_with_length, #encode_with_length, #eval_length, #get_sub_codecs

Constructor Details

#initialize(id, length, content) ⇒ Prefixedlength

Returns a new instance of Prefixedlength.



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

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

Instance Method Details

#build_field(buf, length) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/codec/prefix.rb', line 35

def build_field(buf,length)
  begin
    f,r = decode(buf[0,length])
  rescue ErrorBufferUnderflow => e
    raise ParsingException, e.message
  end
  Logger.error "Error remain data in Prefixedlength" if r != ""
  return f
end

#decode(buffer) ⇒ Object



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

def decode(buffer)
  l, buf = @length_codec.decode(buffer)
 len = get_length(l)
 if len == 0
   f = Field.new
  f.set_id(@id)
  f.set_value(nil)
  return f,buf
 else
   begin
    f,remain = @value_codec.decode_with_length(buf,len)
  rescue => e
    Logger.error "Error in #{@id} decoder \n #{e.message}\n#{e.backtrace.join(10.chr)}"
    raise ParsingException, e.message
  end
    
    f.set_id(@id)
    return f,remain
 end
end

#encode(field) ⇒ Object



45
46
47
48
49
50
# File 'lib/codec/prefix.rb', line 45

def encode(field)
  l, val = @value_codec.encode_with_length(field)
  length = @length_codec.encode(Field.new.set_value(l))
  out = length + val
  return out
end

#get_length(length_field) ⇒ Object



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

def get_length(length_field)
	length_field.get_value.to_i
end