@@ -84,71 +84,75 @@ def wrap(*args)
8484
8585 wraps ::IO , :external_encoding , :internal_encoding , :autoclose? , :autoclose= , :pid , :stat , :binmode , :flush , :set_encoding , :set_encoding_by_bom , :to_path , :to_io , :to_i , :reopen , :fileno , :fsync , :fdatasync , :sync , :sync= , :tell , :seek , :rewind , :path , :pos , :pos= , :eof , :eof? , :close_on_exec? , :close_on_exec= , :closed? , :close_read , :close_write , :isatty , :tty? , :binmode? , :sysseek , :advise , :ioctl , :fcntl , :nread , :ready? , :pread , :pwrite , :pathconf
8686
87- # Read the specified number of bytes from the input stream. This is fast path.
88- # @example
89- # data = io.sysread(512)
90- wrap_blocking_method :sysread , :read_nonblock
91-
92- alias readpartial read_nonblock
93-
94- # Read `length` bytes of data from the underlying I/O. If length is unspecified, read everything.
95- def read ( length = nil , buffer = nil )
96- if buffer
97- buffer . clear
98- else
99- buffer = String . new
100- end
87+ if Async ::Scheduler . supported?
88+ def_delegators :@io , :read , :write , :sysread , :syswrite , :read_nonblock , :write_nonblock , :readpartial
89+ else
90+ # Read the specified number of bytes from the input stream. This is fast path.
91+ # @example
92+ # data = io.sysread(512)
93+ wrap_blocking_method :sysread , :read_nonblock
94+
95+ alias readpartial read_nonblock
10196
102- if length
103- return String . new ( encoding : Encoding ::BINARY ) if length <= 0
97+ # Read `length` bytes of data from the underlying I/O. If length is unspecified, read everything.
98+ def read ( length = nil , buffer = nil )
99+ if buffer
100+ buffer . clear
101+ else
102+ buffer = String . new
103+ end
104104
105- # Fast path:
106- if buffer = self . sysread ( length , buffer )
105+ if length
106+ return "" if length <= 0
107107
108- # Slow path:
109- while buffer . bytesize < length
108+ # Fast path:
109+ if buffer = self . sysread ( length , buffer )
110+
110111 # Slow path:
111- if chunk = self . sysread ( length - buffer . bytesize )
112- buffer << chunk
113- else
114- break
112+ while buffer . bytesize < length
113+ # Slow path:
114+ if chunk = self . sysread ( length - buffer . bytesize )
115+ buffer << chunk
116+ else
117+ break
118+ end
115119 end
120+
121+ return buffer
122+ else
123+ return nil
124+ end
125+ else
126+ buffer = self . sysread ( BLOCK_SIZE , buffer )
127+
128+ while chunk = self . sysread ( BLOCK_SIZE )
129+ buffer << chunk
116130 end
117131
118132 return buffer
119- else
120- return nil
121133 end
122- else
123- buffer = self . sysread ( BLOCK_SIZE , buffer )
124-
125- while chunk = self . sysread ( BLOCK_SIZE )
126- buffer << chunk
127- end
128-
129- return buffer
130134 end
131- end
132-
133- # Write entire buffer to output stream. This is fast path.
134- # @example
135- # io.syswrite("Hello World")
136- wrap_blocking_method :syswrite , :write_nonblock
137-
138- def write ( buffer )
139- # Fast path:
140- written = self . syswrite ( buffer )
141- remaining = buffer . bytesize - written
142135
143- while remaining > 0
144- # Slow path:
145- length = self . syswrite ( buffer . byteslice ( written , remaining ) )
136+ # Write entire buffer to output stream. This is fast path.
137+ # @example
138+ # io.syswrite("Hello World")
139+ wrap_blocking_method :syswrite , :write_nonblock
140+
141+ def write ( buffer )
142+ # Fast path:
143+ written = self . syswrite ( buffer )
144+ remaining = buffer . bytesize - written
146145
147- remaining -= length
148- written += length
146+ while remaining > 0
147+ # Slow path:
148+ length = self . syswrite ( buffer . byteslice ( written , remaining ) )
149+
150+ remaining -= length
151+ written += length
152+ end
153+
154+ return written
149155 end
150-
151- return written
152156 end
153157
154158 def << buffer
0 commit comments