Class: IPFS::IO::ReadFromWriterIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ipfs-api/io.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ReadFromWriterIO

Returns a new instance of ReadFromWriterIO.



9
10
11
12
13
14
15
# File 'lib/ipfs-api/io.rb', line 9

def initialize &block
  @block = block
  @stream = StringIO.new
  @d = @p = 0
  @eof = false
  fetch_data
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ipfs-api/io.rb', line 43

def eof?
  @eof
end

#posObject



39
40
41
# File 'lib/ipfs-api/io.rb', line 39

def pos
  @d + @p
end

#read(length = nil, outbuf = '') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ipfs-api/io.rb', line 17

def read length = nil, outbuf = ''
  return '' if length == 0
  outbuf.slice!(length..-1) if !length.nil?
  q = 0
  while @stream.size > 0
    s = @stream.size - @p
    s = [length - q, s].min if !length.nil?
    outbuf[q, s] = @stream.string[@p, s]
    @p, q = @p + s, q + s
    @eof = true if length.nil? and @p > 0 and @p == q
    break if q == length or @eof
    fetch_data if @stream.size == @p
  end
  if length.nil? || outbuf.size < length
    @eof = true
  end
  if q == 0 and not length.nil?
    outbuf = nil
  end
  outbuf
end