@@ -135,27 +135,15 @@ def read(size=nil, buf=nil)
135
135
# See IO#readpartial for full details.
136
136
137
137
def readpartial ( maxlen , buf = nil )
138
- if maxlen == 0
139
- if buf
140
- buf . clear
141
- return buf
142
- else
143
- return ""
144
- end
145
- end
138
+ # JRuby: sysread does `maxlen == 0` short-circuit check internally
146
139
if @rbuffer . empty?
147
140
begin
148
141
return sysread ( maxlen , buf )
149
142
rescue Errno ::EAGAIN
150
143
retry
151
144
end
152
145
end
153
- ret = consume_rbuff ( maxlen )
154
- if buf
155
- buf . replace ( ret )
156
- ret = buf
157
- end
158
- ret
146
+ do_consume_rbuff ( maxlen , buf )
159
147
end
160
148
161
149
##
@@ -193,6 +181,15 @@ def readpartial(maxlen, buf=nil)
193
181
# it will return +nil+ instead of raising EOFError.
194
182
195
183
def read_nonblock ( maxlen , buf = nil , exception : true )
184
+ # JRuby: sysread does `maxlen == 0` short-circuit check internally
185
+ if @rbuffer . empty?
186
+ return sysread_nonblock ( maxlen , buf , exception : exception )
187
+ end
188
+ do_consume_rbuff ( maxlen , buf )
189
+ end
190
+
191
+ # @private
192
+ def do_consume_rbuff ( maxlen , buf )
196
193
if maxlen == 0
197
194
if buf
198
195
buf . clear
@@ -201,16 +198,15 @@ def read_nonblock(maxlen, buf=nil, exception: true)
201
198
return ""
202
199
end
203
200
end
204
- if @rbuffer . empty?
205
- return sysread_nonblock ( maxlen , buf , exception : exception )
206
- end
201
+
207
202
ret = consume_rbuff ( maxlen )
208
203
if buf
209
204
buf . replace ( ret )
210
205
ret = buf
211
206
end
212
207
ret
213
208
end
209
+ private :do_consume_rbuff
214
210
215
211
##
216
212
# Reads the next "line" from the stream. Lines are separated by _eol_. If
@@ -335,9 +331,8 @@ def eof?
335
331
# buffer is flushed to the underlying socket.
336
332
337
333
def do_write ( s )
338
- @wbuffer = Buffer . new unless defined? @wbuffer
334
+ @wbuffer || = Buffer . new
339
335
@wbuffer << s
340
- @wbuffer . force_encoding ( Encoding ::BINARY )
341
336
@sync ||= false
342
337
if @sync or @wbuffer . size > BLOCK_SIZE
343
338
until @wbuffer . empty?
@@ -424,11 +419,12 @@ def puts(*args)
424
419
s = Buffer . new
425
420
if args . empty?
426
421
s << "\n "
422
+ else
423
+ args . each do |arg |
424
+ s << arg . to_s
425
+ s . sub! ( /(?<!\n )\z / , "\n " )
426
+ end
427
427
end
428
- args . each { |arg |
429
- s << arg . to_s
430
- s . sub! ( /(?<!\n )\z / , "\n " )
431
- }
432
428
do_write ( s )
433
429
nil
434
430
end
0 commit comments