* lib/net/ftp.rb: use &block and yield for speed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2002-07-03 04:49:54 +00:00
Родитель 4e85b28085
Коммит f930648ae5
2 изменённых файлов: 20 добавлений и 49 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Wed Jul 3 13:45:42 2002 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/ftp.rb: use &block and yield for speed.
Wed Jul 3 02:32:31 2002 Wakou Aoyama <wakou@ruby-lang.org> Wed Jul 3 02:32:31 2002 Wakou Aoyama <wakou@ruby-lang.org>
* lib/cgi.rb (CGI#initialize): improvement for mod_ruby. * lib/cgi.rb (CGI#initialize): improvement for mod_ruby.

Просмотреть файл

@ -261,26 +261,21 @@ module Net
@welcome = resp @welcome = resp
end end
def retrbinary(cmd, blocksize, rest_offset = nil, callback = Proc.new) def retrbinary(cmd, blocksize, rest_offset = nil)
synchronize do synchronize do
voidcmd("TYPE I") voidcmd("TYPE I")
conn = transfercmd(cmd, rest_offset) conn = transfercmd(cmd, rest_offset)
loop do loop do
data = conn.read(blocksize) data = conn.read(blocksize)
break if data == nil break if data == nil
callback.call(data) yield(data)
end end
conn.close conn.close
voidresp voidresp
end end
end end
def retrlines(cmd, callback = nil) def retrlines(cmd)
if block_given?
callback = Proc.new
elsif not callback.is_a?(Proc)
callback = Proc.new {|line| print line, "\n"}
end
synchronize do synchronize do
voidcmd("TYPE A") voidcmd("TYPE A")
conn = transfercmd(cmd) conn = transfercmd(cmd)
@ -292,18 +287,14 @@ module Net
elsif line[-1] == ?\n elsif line[-1] == ?\n
line = line[0 .. -2] line = line[0 .. -2]
end end
callback.call(line) yield(line)
end end
conn.close conn.close
voidresp voidresp
end end
end end
def storbinary(cmd, file, blocksize, rest_offset = nil, callback = nil) def storbinary(cmd, file, blocksize, rest_offset = nil, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
synchronize do synchronize do
voidcmd("TYPE I") voidcmd("TYPE I")
conn = transfercmd(cmd, rest_offset) conn = transfercmd(cmd, rest_offset)
@ -311,18 +302,14 @@ module Net
buf = file.read(blocksize) buf = file.read(blocksize)
break if buf == nil break if buf == nil
conn.write(buf) conn.write(buf)
callback.call(buf) if use_callback yield(buf) if block
end end
conn.close conn.close
voidresp voidresp
end end
end end
def storlines(cmd, file, callback = nil) def storlines(cmd, file, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
synchronize do synchronize do
voidcmd("TYPE A") voidcmd("TYPE A")
conn = transfercmd(cmd) conn = transfercmd(cmd)
@ -333,7 +320,7 @@ module Net
buf = buf.chomp + CRLF buf = buf.chomp + CRLF
end end
conn.write(buf) conn.write(buf)
callback.call(buf) if use_callback yield(buf) if block
end end
conn.close conn.close
voidresp voidresp
@ -341,11 +328,7 @@ module Net
end end
def getbinaryfile(remotefile, localfile, def getbinaryfile(remotefile, localfile,
blocksize = DEFAULT_BLOCKSIZE, callback = nil) blocksize = DEFAULT_BLOCKSIZE, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
if @resume if @resume
rest_offset = File.size?(localfile) rest_offset = File.size?(localfile)
f = open(localfile, "a") f = open(localfile, "a")
@ -357,24 +340,20 @@ module Net
f.binmode f.binmode
retrbinary("RETR " + remotefile, blocksize, rest_offset) do |data| retrbinary("RETR " + remotefile, blocksize, rest_offset) do |data|
f.write(data) f.write(data)
callback.call(data) if use_callback yield(data) if block
end end
ensure ensure
f.close f.close
end end
end end
def gettextfile(remotefile, localfile, callback = nil) def gettextfile(remotefile, localfile, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
f = open(localfile, "w") f = open(localfile, "w")
begin begin
retrlines("RETR " + remotefile) do |line| retrlines("RETR " + remotefile) do |line|
line = line + @return_code line = line + @return_code
f.write(line) f.write(line)
callback.call(line) if use_callback yield(line) if block
end end
ensure ensure
f.close f.close
@ -382,11 +361,7 @@ module Net
end end
def putbinaryfile(localfile, remotefile, def putbinaryfile(localfile, remotefile,
blocksize = DEFAULT_BLOCKSIZE, callback = nil) blocksize = DEFAULT_BLOCKSIZE, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
if @resume if @resume
rest_offset = size(remotefile) rest_offset = size(remotefile)
else else
@ -395,24 +370,16 @@ module Net
f = open(localfile) f = open(localfile)
begin begin
f.binmode f.binmode
storbinary("STOR " + remotefile, f, blocksize, rest_offset) do |data| storbinary("STOR " + remotefile, f, blocksize, rest_offset, &block)
callback.call(data) if use_callback
end
ensure ensure
f.close f.close
end end
end end
def puttextfile(localfile, remotefile, callback = nil) def puttextfile(localfile, remotefile, &block)
if block_given?
callback = Proc.new
end
use_callback = callback.is_a?(Proc)
f = open(localfile) f = open(localfile)
begin begin
storlines("STOR " + remotefile, f) do |line| storlines("STOR " + remotefile, f, &block)
callback.call(line) if use_callback
end
ensure ensure
f.close f.close
end end