Class: S3sync::ProgressStream

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/s3sync/HTTPStreaming.rb

Instance Method Summary collapse

Constructor Details

#initialize(s, size = 0) ⇒ ProgressStream

Returns a new instance of ProgressStream.



55
56
57
58
59
60
61
62
63
64
# File 'lib/s3sync/HTTPStreaming.rb', line 55

def initialize(s, size=0)
	@start = @last = Time.new
	@total = size
	@transferred = 0
	@closed = false
	@printed = false
	@innerStream = s
	super(@innerStream)
	__setobj__(@innerStream)
end

Instance Method Details

#closeObject



102
103
104
105
106
# File 'lib/s3sync/HTTPStreaming.rb', line 102

def close()
	$stdout.printf("\n") if @printed and not @closed
	@closed = true
	@innerStream.close
end

#read(i) ⇒ Object

need to catch reads and writes so we can count what’s being transferred



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/s3sync/HTTPStreaming.rb', line 66

def read(i)
	res = @innerStream.read(i)
	@transferred += res.respond_to?(:length) ? res.length : 0
	now = Time.new
	if(now - @last > 1) # don't do this oftener than once per second
		@printed = true
          begin
             $stdout.printf("\rProgress: %db  %db/s  %s       ", @transferred, (@transferred/(now - @start)).floor, 
                @total > 0? (100 * @transferred/@total).floor.to_s + "%" : ""  
             )
          rescue FloatDomainError
             #wtf?
          end
		$stdout.flush
		@last = now
	end
	res
end

#rewindObject



98
99
100
101
# File 'lib/s3sync/HTTPStreaming.rb', line 98

def rewind()
	@transferred = 0
	@innerStream.rewind if @innerStream.respond_to?(:rewind)
end

#write(s) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/s3sync/HTTPStreaming.rb', line 84

def write(s)
	@transferred += s.length
	res = @innerStream.write(s)
	now = Time.new
	if(now -@last > 1) # don't do this oftener than once per second
		@printed = true
		$stdout.printf("\rProgress: %db  %db/s  %s       ", @transferred, (@transferred/(now - @start)).floor, 
			@total > 0? (100 * @transferred/@total).floor.to_s + "%" : ""  
		)  
		$stdout.flush
		@last = now
	end
	res
end