Class: PacketGen::Types::IntString
- Inherits:
-
Object
- Object
- PacketGen::Types::IntString
- Defined in:
- lib/packetgen/types/int_string.rb
Overview
Provides a class for creating strings preceeded by their length as a Int. By default, a null string will have one byte length (length byte set to 0).
Instance Attribute Summary collapse
-
#string ⇒ String
internal string.
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Set length from internal string length.
-
#initialize(str = '', len_type = Int8) ⇒ IntString
constructor
A new instance of IntString.
- #length ⇒ Integer
- #length=(l) ⇒ Integer
-
#read(str) ⇒ IntString
Self.
-
#sz ⇒ Integer
Give binary string length (including
lengthfield). -
#to_s ⇒ ::String
Get binary string.
Constructor Details
#initialize(str = '', len_type = Int8) ⇒ IntString
Returns a new instance of IntString.
21 22 23 24 25 |
# File 'lib/packetgen/types/int_string.rb', line 21 def initialize(str='', len_type=Int8) @string = Types::String.new(str) @length = len_type.new calc_length end |
Instance Attribute Details
#string ⇒ String
internal string
17 18 19 |
# File 'lib/packetgen/types/int_string.rb', line 17 def string @string end |
Instance Method Details
#calc_length ⇒ Integer
Set length from internal string length
58 59 60 |
# File 'lib/packetgen/types/int_string.rb', line 58 def calc_length @length.read @string.length end |
#length ⇒ Integer
46 47 48 |
# File 'lib/packetgen/types/int_string.rb', line 46 def length @length.to_i end |
#length=(l) ⇒ Integer
40 41 42 43 |
# File 'lib/packetgen/types/int_string.rb', line 40 def length=(l) @length.read l l end |
#read(str) ⇒ IntString
Returns self.
29 30 31 32 33 34 35 36 |
# File 'lib/packetgen/types/int_string.rb', line 29 def read(str) unless str[0, @length.width].size == @length.width raise ParseError, "String too short for type #{@length.type}" end @length.read str[0, @length.width] @string.read str[@length.width, @length.to_i] self end |
#sz ⇒ Integer
Give binary string length (including length field)
64 65 66 |
# File 'lib/packetgen/types/int_string.rb', line 64 def sz to_s.size end |
#to_s ⇒ ::String
Get binary string
52 53 54 |
# File 'lib/packetgen/types/int_string.rb', line 52 def to_s @length.to_s << @string.to_s end |