Class: HrrRbSsh::DataType::String

Inherits:
HrrRbSsh::DataType show all
Defined in:
lib/hrr_rb_ssh/data_type/string.rb

Overview

String provides methods to convert Ruby’s string value and binary string each other.

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ ::String

Convert binary string into Ruby’s string value.

Parameters:

  • io (::IO)

    IO instance that has buffer to be read

Returns:

  • (::String)

    converted Ruby’s string value



28
29
30
31
# File 'lib/hrr_rb_ssh/data_type/string.rb', line 28

def self.decode io
  length = io.read(4).unpack("N")[0]
  io.read(length).unpack("a*")[0]
end

.encode(arg) ⇒ ::String

Convert Ruby’s string value into binary string.

Parameters:

  • arg (::String)

    Ruby’s string value to be converted

Returns:

  • (::String)

    converted binary string

Raises:

  • (::ArgumentError)

    when arg is not string

  • (::ArgumentError)

    when length of arg is longer than 0xffff_ffff



14
15
16
17
18
19
20
21
22
# File 'lib/hrr_rb_ssh/data_type/string.rb', line 14

def self.encode arg
  unless arg.kind_of? ::String
    raise ArgumentError, "must be a kind of String, but got #{arg.inspect}"
  end
  if arg.length > 0xffff_ffff
    raise ArgumentError, "must be shorter than or equal to #{0xffff_ffff}, but got length #{arg.length}"
  end
  [arg.length, arg].pack("Na*")
end