Module: LightIO::Library::IO::IOMethods
- Included in:
- LightIO::Library::IO, OpenSSL::SSL::SSLSocket
- Defined in:
- lib/lightio/library/io.rb
Overview
abstract for io-like operations
Class Method Summary collapse
Instance Method Summary collapse
- #close(*args) ⇒ Object
- #eof ⇒ Object (also: #eof?)
- #flush ⇒ Object
- #getbyte ⇒ Object
- #getc ⇒ Object
- #gets(*args) ⇒ Object
- #lightio_initialize ⇒ Object
- #print(*obj) ⇒ Object
- #printf(*args) ⇒ Object
- #puts(*obj) ⇒ Object
- #read(length = nil, outbuf = nil) ⇒ Object
- #readbyte ⇒ Object
- #readchar ⇒ Object
- #readline(*args) ⇒ Object
- #readlines(*args) ⇒ Object
- #readpartial(maxlen, outbuf = nil) ⇒ Object
- #wait(timeout = nil, mode = :read) ⇒ Object
- #wait_readable(timeout = nil) ⇒ Object
- #wait_writable(timeout = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
18 19 20 21 |
# File 'lib/lightio/library/io.rb', line 18 def included(base) base.send(:wrap_blocking_methods, :read, :write) base.send(:alias_method, :<<, :write) end |
Instance Method Details
#close(*args) ⇒ Object
160 161 162 163 164 |
# File 'lib/lightio/library/io.rb', line 160 def close(*args) # close watcher before io closed io_watcher.close @obj.close end |
#eof ⇒ Object Also known as: eof?
109 110 111 112 113 114 115 116 117 |
# File 'lib/lightio/library/io.rb', line 109 def eof # until eof have a value fill_read_buf while @readbuf.eof? && @eof.nil? wait_readable fill_read_buf end nonblock_eof? end |
#flush ⇒ Object
155 156 157 158 |
# File 'lib/lightio/library/io.rb', line 155 def flush @obj.flush self end |
#getbyte ⇒ Object
70 71 72 |
# File 'lib/lightio/library/io.rb', line 70 def getbyte read(1) end |
#getc ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/lightio/library/io.rb', line 74 def getc fill_read_buf until (c = @readbuf.getc) return nil if nonblock_eof? wait_readable fill_read_buf end c end |
#gets(*args) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/lightio/library/io.rb', line 121 def gets(*args) raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..2)" if args.size > 2 sep = $/ if args[0].is_a?(Numeric) limit = args[0] else sep = args[0] if args.size > 0 limit = args[1] if args[1].is_a?(Numeric) end until fill_read_buf break if limit && limit <= @readbuf.length break if sep && @readbuf.string.index(sep) wait_readable end @readbuf.gets(*args) end |
#lightio_initialize ⇒ Object
24 25 26 27 28 29 |
# File 'lib/lightio/library/io.rb', line 24 def lightio_initialize @readbuf = StringIO.new @readbuf.set_encoding(@obj.external_encoding) if @obj.respond_to?(:external_encoding) @eof = nil @seek = 0 end |
#print(*obj) ⇒ Object
138 139 140 141 142 |
# File 'lib/lightio/library/io.rb', line 138 def print(*obj) obj.each do |s| write(s) end end |
#printf(*args) ⇒ Object
144 145 146 |
# File 'lib/lightio/library/io.rb', line 144 def printf(*args) write(sprintf(*args)) end |
#puts(*obj) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/lightio/library/io.rb', line 148 def puts(*obj) obj.each do |s| write(s) write($/) end end |
#read(length = nil, outbuf = nil) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/lightio/library/io.rb', line 53 def read(length = nil, outbuf = nil) while !fill_read_buf && (length.nil? || length > @readbuf.length - @readbuf.pos) wait_readable end @readbuf.read(length, outbuf) end |
#readbyte ⇒ Object
103 104 105 106 107 |
# File 'lib/lightio/library/io.rb', line 103 def readbyte b = getbyte raise EOFError, 'end of file reached' if b.nil? b end |
#readchar ⇒ Object
97 98 99 100 101 |
# File 'lib/lightio/library/io.rb', line 97 def readchar c = getc raise EOFError, 'end of file reached' if c.nil? c end |
#readline(*args) ⇒ Object
84 85 86 87 88 |
# File 'lib/lightio/library/io.rb', line 84 def readline(*args) line = gets(*args) raise EOFError, 'end of file reached' if line.nil? line end |
#readlines(*args) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/lightio/library/io.rb', line 90 def readlines(*args) until fill_read_buf wait_readable end @readbuf.readlines(*args) end |
#readpartial(maxlen, outbuf = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/lightio/library/io.rb', line 60 def readpartial(maxlen, outbuf = nil) raise ArgumentError, "negative length #{maxlen} given" if maxlen < 0 fill_read_buf while @readbuf.eof? && !io_eof? wait_readable fill_read_buf end @readbuf.readpartial(maxlen, outbuf) end |
#wait(timeout = nil, mode = :read) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lightio/library/io.rb', line 31 def wait(timeout = nil, mode = :read) # avoid wait if can immediately return wait_result = if RUBY_VERSION < '2.4' if mode == :read @obj.wait_readable(0) elsif mode == :write @obj.wait_writable(0) end else @obj.wait(0, mode) end (wait_result || io_watcher.wait(timeout, mode)) && self end |
#wait_readable(timeout = nil) ⇒ Object
45 46 47 |
# File 'lib/lightio/library/io.rb', line 45 def wait_readable(timeout = nil) wait(timeout, :read) && self end |
#wait_writable(timeout = nil) ⇒ Object
49 50 51 |
# File 'lib/lightio/library/io.rb', line 49 def wait_writable(timeout = nil) wait(timeout, :write) && self end |