Module: Aws::Crt::StringBlob

Defined in:
lib/aws-crt/string_blob.rb

Overview

module for CRT Blob utility methods CRT encodes lists of strings as [length, str*] using null padded, unsigned long

Class Method Summary collapse

Class Method Details

.decode(buffer) ⇒ Object

Decode a blob (StringBlob)/Buffer into an array of strings

Parameters:

  • buffer
    • array of chars (buffer)

Returns:

  • strings



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aws-crt/string_blob.rb', line 29

def self.decode(buffer)
  strings = []
  i = 0
  while i < buffer.size
    len = buffer[i, 4].pack('c*').unpack1('N')
    strings << buffer[i + 4, len].pack('c*')
                                 .force_encoding(Encoding::UTF_8)
    i += len + 4
  end
  strings
end

.encode(strings) ⇒ Object

Encode an array of strings into a buffer (blob)

Parameters:

  • strings (Array<String>)


15
16
17
18
19
20
21
22
23
# File 'lib/aws-crt/string_blob.rb', line 15

def self.encode(strings)
  buffer = StringIO.new
  strings.each do |s|
    e = s.to_s.unpack('c*')
    buffer << [e.length].pack('N')
    buffer << e.pack('c*')
  end
  buffer.string.unpack('c*')
end