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

Class Method Details

.included(base) ⇒ Object



16
17
18
19
# File 'lib/lightio/library/io.rb', line 16

def included(base)
  base.send(:wrap_blocking_methods, :read, :write)
  base.send(:alias_method, :<<, :write)
end

Instance Method Details

#close(*args) ⇒ Object



138
139
140
141
142
# File 'lib/lightio/library/io.rb', line 138

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

#eofObject Also known as: eof?



95
96
97
98
# File 'lib/lightio/library/io.rb', line 95

def eof
  wait_readable
  @obj.eof
end

#getbyteObject



60
61
62
# File 'lib/lightio/library/io.rb', line 60

def getbyte
  read(1)
end

#getcObject



64
65
66
67
# File 'lib/lightio/library/io.rb', line 64

def getc
  wait_readable
  @obj.getc
end

#gets(*args) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lightio/library/io.rb', line 102

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.shift
  else
    sep = args.shift if args.size > 0
    limit = args.shift if args.first.is_a?(Numeric)
  end
  s = ''
  while (c = getbyte)
    s << c
    break if limit && s.size == limit
    break if c == sep
  end
  s = nil if s.empty?
  $_ = s
end


121
122
123
124
125
# File 'lib/lightio/library/io.rb', line 121

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

#printf(*args) ⇒ Object



127
128
129
# File 'lib/lightio/library/io.rb', line 127

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

#puts(*obj) ⇒ Object



131
132
133
134
135
136
# File 'lib/lightio/library/io.rb', line 131

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

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

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lightio/library/io.rb', line 34

def read(length=nil, outbuf=nil)
  raise ArgumentError, "negative length #{length} given" if length && length < 0
  (outbuf ||= "").clear
  loop do
    readlen = length.nil? ? 4096 : length - outbuf.size
    if (data = wait_nonblock(:read_nonblock, readlen))
      outbuf << data
      if length == outbuf.size
        return outbuf
      end
    else
      return length.nil? ? outbuf : nil
    end
  end
end

#readbyteObject

Raises:

  • (EOFError)


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

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

#readcharObject

Raises:

  • (EOFError)


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

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

#readline(*args) ⇒ Object

Raises:

  • (EOFError)


69
70
71
72
73
# File 'lib/lightio/library/io.rb', line 69

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

#readlines(*args) ⇒ Object



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

def readlines(*args)
  result = []
  until eof?
    result << readline(*args)
  end
  result
end

#readpartial(maxlen, outbuf = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/lightio/library/io.rb', line 50

def readpartial(maxlen, outbuf=nil)
  (outbuf ||= "").clear
  if (data = wait_nonblock(:read_nonblock, maxlen))
    outbuf << data
  else
    raise EOFError, 'end of file reached'
  end
  outbuf
end

#wait(timeout = nil, mode = :read) ⇒ Object



22
23
24
# File 'lib/lightio/library/io.rb', line 22

def wait(timeout = nil, mode = :read)
  io_watcher.wait(timeout, mode) && self
end

#wait_readable(timeout = nil) ⇒ Object



26
27
28
# File 'lib/lightio/library/io.rb', line 26

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

#wait_writable(timeout = nil) ⇒ Object



30
31
32
# File 'lib/lightio/library/io.rb', line 30

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