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

Instance Method Summary collapse

Instance Method Details

#close(*args) ⇒ Object



173
174
175
176
177
# File 'lib/lightio/library/io.rb', line 173

def close(*args)
  # close watcher before io closed
  io_watcher.close
  @obj.close
end

#eofObject Also known as: eof?



122
123
124
125
126
127
128
129
130
# File 'lib/lightio/library/io.rb', line 122

def eof
  # until eof have a value
  fill_read_buf
  while @readbuf.eof? && @eof.nil?
    wait_readable
    fill_read_buf
  end
  nonblock_eof?
end

#flushObject



168
169
170
171
# File 'lib/lightio/library/io.rb', line 168

def flush
  @obj.flush
  self
end

#getbyteObject



83
84
85
# File 'lib/lightio/library/io.rb', line 83

def getbyte
  read(1)
end

#getcObject



87
88
89
90
91
92
93
94
95
# File 'lib/lightio/library/io.rb', line 87

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

Raises:

  • (ArgumentError)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lightio/library/io.rb', line 134

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_initializeObject



17
18
19
20
21
22
# File 'lib/lightio/library/io.rb', line 17

def lightio_initialize
  @readbuf = StringIO.new
  @readbuf.set_encoding(@obj.external_encoding) if @obj.respond_to?(:external_encoding)
  @eof = nil
  @seek = 0
end


151
152
153
154
155
# File 'lib/lightio/library/io.rb', line 151

def print(*obj)
  obj.each do |s|
    write(s)
  end
end

#printf(*args) ⇒ Object



157
158
159
# File 'lib/lightio/library/io.rb', line 157

def printf(*args)
  write(sprintf(*args))
end

#puts(*obj) ⇒ Object



161
162
163
164
165
166
# File 'lib/lightio/library/io.rb', line 161

def puts(*obj)
  obj.each do |s|
    write(s)
    write($/)
  end
end

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



66
67
68
69
70
71
# File 'lib/lightio/library/io.rb', line 66

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

#readbyteObject

Raises:

  • (EOFError)


116
117
118
119
120
# File 'lib/lightio/library/io.rb', line 116

def readbyte
  b = getbyte
  raise EOFError, 'end of file reached' if b.nil?
  b
end

#readcharObject

Raises:

  • (EOFError)


110
111
112
113
114
# File 'lib/lightio/library/io.rb', line 110

def readchar
  c = getc
  raise EOFError, 'end of file reached' if c.nil?
  c
end

#readline(*args) ⇒ Object

Raises:

  • (EOFError)


97
98
99
100
101
# File 'lib/lightio/library/io.rb', line 97

def readline(*args)
  line = gets(*args)
  raise EOFError, 'end of file reached' if line.nil?
  line
end

#readlines(*args) ⇒ Object



103
104
105
106
107
108
# File 'lib/lightio/library/io.rb', line 103

def readlines(*args)
  until fill_read_buf
    wait_readable
  end
  @readbuf.readlines(*args)
end

#readpartial(maxlen, outbuf = nil) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
# File 'lib/lightio/library/io.rb', line 73

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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lightio/library/io.rb', line 24

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



38
39
40
# File 'lib/lightio/library/io.rb', line 38

def wait_readable(timeout = nil)
  wait(timeout, :read) && self
end

#wait_writable(timeout = nil) ⇒ Object



42
43
44
# File 'lib/lightio/library/io.rb', line 42

def wait_writable(timeout = nil)
  wait(timeout, :write) && self
end

#write(string) ⇒ Object Also known as: <<



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lightio/library/io.rb', line 46

def write(string)
  s = StringIO.new(string.to_s)
  remain_size = s.size
  loop do
    result = write_nonblock(s.read, exception: false)
    case result
      when :wait_writable
        io_watcher.wait_writable
      else
        remain_size -= result
        unless remain_size.zero?
          s.seek(result)
        end
        return result
    end
  end
end