Class: Codec::Tagged

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

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #get_sub_codecs

Constructor Details

#initialize(tag_codec) ⇒ Tagged

Returns a new instance of Tagged.



105
106
107
108
# File 'lib/codec/prefix.rb', line 105

def initialize(tag_codec)
  @subCodecs = {}
  @tag_codec = tag_codec
end

Instance Method Details

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



110
111
112
113
114
115
116
117
118
119
# File 'lib/codec/prefix.rb', line 110

def decode(buffer, field, length = nil)
  buffer = buffer.slice!(0...length) if length && length > 0
  tag = Field.new("TAG")
  @tag_codec.decode(buffer,tag)
  field.set_id(tag.get_value.to_s)
  if @subCodecs[tag.get_value.to_s].nil?
    raise ParsingException.new "Unknown tag #{tag.get_value.to_s} when decoding #{field.get_id}"
  end
  @subCodecs[tag.get_value.to_s].decode(buffer,field)
end

#encode(buffer, field) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/codec/prefix.rb', line 121

def encode(buffer, field)
  head = Field.new("tag", field.get_id)
  out = ""
  @tag_codec.encode(out, head)
  if @subCodecs[field.get_id].nil?
    raise EncodingException.new "Unknown tag #{field.get_id} for #{@id} encoder"
  end
  @subCodecs[field.get_id].encode(out, field)
  buffer << out
  return out.length
end