Class: Codec::Packed

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

Direct Known Subclasses

Numpck, Strpck

Instance Attribute Summary

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #build_field, #encode, #eval_length, #get_sub_codecs, #initialize

Constructor Details

This class inherits a constructor from Codec::Base

Instance Method Details

#decode(buf) ⇒ Object



24
25
26
# File 'lib/codec/packed.rb', line 24

def decode(buf)
  return decode_with_length(buf,@length)
end

#decode_with_length(buf, length) ⇒ Object



19
20
21
22
# File 'lib/codec/packed.rb', line 19

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

#encode_with_length(field) ⇒ Object



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

def encode_with_length(field)
  return get_length(field),encode(field)
end

#get_length(field) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/codec/packed.rb', line 7

def get_length(field)
  if @length > 0
    return @length 
  else
    return field.get_value.to_s.length
  end
end

#get_pck_length(length) ⇒ Object



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

def get_pck_length(length)
  ((length + 1) / 2)
end