Module: Kitchen::Base64Stream

Defined in:
lib/kitchen/base64_stream.rb

Overview

Base64 encoder/decoder that operates on IO objects so as to minimize memory allocations on large payloads.

Author:

Class Method Summary collapse

Class Method Details

.strict_decode(io_in, io_out) ⇒ Object

Decodes a Base64 input stream into an output stream. The input and ouput objects must be opened IO resources. In other words, opening and closing the resources are not the responsibilty of this method.

Parameters:

  • io_in (#read)

    input stream

  • io_out (#write)

    output stream



47
48
49
50
51
52
53
# File 'lib/kitchen/base64_stream.rb', line 47

def self.strict_decode(io_in, io_out)
  buffer = "" # rubocop:disable Lint/UselessAssignment
  while io_in.read(3 * 1000, buffer)
    io_out.write(buffer.unpack("m0").first)
  end
  buffer = nil # rubocop:disable Lint/UselessAssignment
end

.strict_encode(io_in, io_out) ⇒ Object

Encodes an input stream into a Base64 output stream. The input and ouput objects must be opened IO resources. In other words, opening and closing the resources are not the responsibilty of this method.

Parameters:

  • io_in (#read)

    input stream

  • io_out (#write)

    output stream



33
34
35
36
37
38
39
# File 'lib/kitchen/base64_stream.rb', line 33

def self.strict_encode(io_in, io_out)
  buffer = "" # rubocop:disable Lint/UselessAssignment
  while io_in.read(3 * 1000, buffer)
    io_out.write([buffer].pack("m0"))
  end
  buffer = nil # rubocop:disable Lint/UselessAssignment
end