Class: FPM::Fry::JoinedIO

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fpm/fry/joined_io.rb

Overview

Joins together multiple IOs

Instance Method Summary collapse

Constructor Details

#initialize(*ios) ⇒ JoinedIO

Returns a new instance of JoinedIO.

Parameters:

  • ios (IO)


7
8
9
10
11
# File 'lib/fpm/fry/joined_io.rb', line 7

def initialize(*ios)
  @ios = ios
  @pos = 0
  @readbytes = 0
end

Instance Method Details

#closeObject

Closes all IOs.



74
75
76
# File 'lib/fpm/fry/joined_io.rb', line 74

def close
  @ios.each(&:close)
end

#eof?true, false

Returns:

  • (true, false)


69
70
71
# File 'lib/fpm/fry/joined_io.rb', line 69

def eof?
  @pos == @ios.size
end

#posNumeric

Returns number bytes read.

Returns:

  • (Numeric)

    number bytes read



64
65
66
# File 'lib/fpm/fry/joined_io.rb', line 64

def pos
  @readbytes
end

#read(len = nil) ⇒ String

Reads length bytes or all if length is nil.

Parameters:

  • len (Numeric, nil) (defaults to: nil)

Returns:

  • (String)

    resulting bytes



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fpm/fry/joined_io.rb', line 16

def read( len = nil )
  buf = []
  if len.nil?
    while chunk = readpartial(512)
      buf << chunk
      @readbytes += chunk.bytesize
    end
    return buf.join
  else
    con = 0
    while con < len
      chunk = readpartial(len - con)
      if chunk.nil?
        if con == 0
          return nil
        else
          return buf.join
        end
      end
      @readbytes += chunk.bytesize
      con += chunk.bytesize
      buf << chunk
    end
    return buf.join
  end
end

#readpartial(length) ⇒ String?

Reads up to length bytes.

Parameters:

  • length (Numeric)

Returns:

  • (String)

    chunk

  • (nil)

    at eof



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fpm/fry/joined_io.rb', line 47

def readpartial( length )
  while (io = @ios[@pos])
    r = io.read( length )
    if r.nil?
      @pos = @pos + 1
      next
    else
      if io.eof?
        @pos = @pos + 1
      end
      return r
    end
  end
  return nil
end