Skip to content

Commit c325cd5

Browse files
committed
[refactory] simplify Buffering internals (on JRuby)
1 parent acf4204 commit c325cd5

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

lib/openssl/buffering.rb

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,15 @@ def read(size=nil, buf=nil)
135135
# See IO#readpartial for full details.
136136

137137
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
146139
if @rbuffer.empty?
147140
begin
148141
return sysread(maxlen, buf)
149142
rescue Errno::EAGAIN
150143
retry
151144
end
152145
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)
159147
end
160148

161149
##
@@ -193,6 +181,15 @@ def readpartial(maxlen, buf=nil)
193181
# it will return +nil+ instead of raising EOFError.
194182

195183
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)
196193
if maxlen == 0
197194
if buf
198195
buf.clear
@@ -201,16 +198,15 @@ def read_nonblock(maxlen, buf=nil, exception: true)
201198
return ""
202199
end
203200
end
204-
if @rbuffer.empty?
205-
return sysread_nonblock(maxlen, buf, exception: exception)
206-
end
201+
207202
ret = consume_rbuff(maxlen)
208203
if buf
209204
buf.replace(ret)
210205
ret = buf
211206
end
212207
ret
213208
end
209+
private :do_consume_rbuff
214210

215211
##
216212
# Reads the next "line" from the stream. Lines are separated by _eol_. If
@@ -335,9 +331,8 @@ def eof?
335331
# buffer is flushed to the underlying socket.
336332

337333
def do_write(s)
338-
@wbuffer = Buffer.new unless defined? @wbuffer
334+
@wbuffer ||= Buffer.new
339335
@wbuffer << s
340-
@wbuffer.force_encoding(Encoding::BINARY)
341336
@sync ||= false
342337
if @sync or @wbuffer.size > BLOCK_SIZE
343338
until @wbuffer.empty?
@@ -424,11 +419,12 @@ def puts(*args)
424419
s = Buffer.new
425420
if args.empty?
426421
s << "\n"
422+
else
423+
args.each do |arg|
424+
s << arg.to_s
425+
s.sub!(/(?<!\n)\z/, "\n")
426+
end
427427
end
428-
args.each{|arg|
429-
s << arg.to_s
430-
s.sub!(/(?<!\n)\z/, "\n")
431-
}
432428
do_write(s)
433429
nil
434430
end

0 commit comments

Comments
 (0)