Class: Microsandbox::FsReadStream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/microsandbox/fs.rb

Overview

A streaming reader over a guest file, from Microsandbox::FS#read_stream. Iterate it (it is Enumerable) to consume byte chunks (ASCII-8BIT) as they arrive, or call #read to drain it into one String.

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ FsReadStream

Returns a new instance of FsReadStream.



205
206
207
# File 'lib/microsandbox/fs.rb', line 205

def initialize(native)
  @native = native
end

Instance Method Details

#each {|chunk| ... } ⇒ self, Enumerator

Yield each chunk of bytes until the stream ends. Returns an Enumerator when called without a block.

Yield Parameters:

  • chunk (String)

    raw bytes (ASCII-8BIT)

Returns:

  • (self, Enumerator)


213
214
215
216
217
218
219
220
# File 'lib/microsandbox/fs.rb', line 213

def each
  return enum_for(:each) unless block_given?

  while (chunk = @native.recv)
    yield chunk
  end
  self
end

#readString

Drain the stream into a single byte String.

Returns:

  • (String)

    raw bytes (ASCII-8BIT)



224
225
226
227
228
# File 'lib/microsandbox/fs.rb', line 224

def read
  buffer = +"".b
  each { |chunk| buffer << chunk }
  buffer
end