Class: Microsandbox::FsReadStream

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

Overview

Note:

Single-pass, forward-only, single-consumer. each/read drain a one-shot native channel — not rewindable, and not safe to share across threads. Consume it once: a second each, or read after a partial each, yields only the remaining bytes.

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.



210
211
212
# File 'lib/microsandbox/fs.rb', line 210

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)


218
219
220
221
222
223
224
225
# File 'lib/microsandbox/fs.rb', line 218

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)



229
230
231
232
233
# File 'lib/microsandbox/fs.rb', line 229

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