Class: FPM::Fry::JoinedIO
- Inherits:
-
Object
- Object
- FPM::Fry::JoinedIO
- Includes:
- Enumerable
- Defined in:
- lib/fpm/fry/joined_io.rb
Overview
Joins together multiple IOs
Instance Method Summary collapse
-
#close ⇒ Object
Closes all IOs.
- #eof? ⇒ true, false
-
#initialize(*ios) ⇒ JoinedIO
constructor
A new instance of JoinedIO.
-
#pos ⇒ Numeric
Number bytes read.
-
#read(len = nil) ⇒ String
Reads length bytes or all if length is nil.
-
#readpartial(length) ⇒ String?
Reads up to length bytes.
Constructor Details
#initialize(*ios) ⇒ JoinedIO
Returns a new instance of JoinedIO.
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
#close ⇒ Object
Closes all IOs.
74 75 76 |
# File 'lib/fpm/fry/joined_io.rb', line 74 def close @ios.each(&:close) end |
#eof? ⇒ true, false
69 70 71 |
# File 'lib/fpm/fry/joined_io.rb', line 69 def eof? @pos == @ios.size end |
#pos ⇒ Numeric
Returns 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.
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.
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 |