Class: Codec::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, length) ⇒ Base

Returns a new instance of Base.



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

def initialize(id,length)
    @length = length.to_i
    @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/codec/base.rb', line 3

def id
  @id
end

Instance Method Details

#add_sub_codec(id_field, codec) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/codec/base.rb', line 50

def add_sub_codec(id_field,codec)
 if codec.nil?
   raise InitializeException, "Invalid codec reference in subcodec #{id_field} for codec #{@id}"
 end
  if @subCodecs.kind_of? Hash
    @subCodecs[id_field] = codec 
  end
end

#build_field(buf, length) ⇒ Object



9
10
11
12
13
# File 'lib/codec/base.rb', line 9

def build_field(buf,length)
  f = Field.new(@id)
  f.set_value(buf[0,length])
  return f
end

#decode(buf) ⇒ Object



20
21
22
23
# File 'lib/codec/base.rb', line 20

def decode(buf)
  l = eval_length(buf,@length)
  return build_field(buf,l),buf[l,buf.length]
end

#decode_with_length(buf, length) ⇒ Object



15
16
17
18
# File 'lib/codec/base.rb', line 15

def decode_with_length(buf,length)
  l = eval_length(buf,length)
  return build_field(buf,l),buf[l,buf.length]
end

#encode(field) ⇒ Object



25
26
27
# File 'lib/codec/base.rb', line 25

def encode(field)
  return field.get_value
end

#encode_with_length(field) ⇒ Object



29
30
31
32
# File 'lib/codec/base.rb', line 29

def encode_with_length(field)
  buf = encode(field)
  return buf.length, buf
end

#eval_length(buf, length) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/codec/base.rb', line 38

def eval_length(buf,length)
 length = 0 if length.nil?
  if(length != 0)
   if buf.length < length
     raise BufferUnderflow, "Not enough data for parsing #{@id} (#{length}/#{buf.length})"
   end
    return length
  else
    return buf.length
  end
end

#get_length(field) ⇒ Object



34
35
36
# File 'lib/codec/base.rb', line 34

def get_length(field)
  field.get_value.length
end

#get_sub_codecsObject



59
60
61
# File 'lib/codec/base.rb', line 59

def get_sub_codecs
  return @subCodecs
end