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.



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

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



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

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



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

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



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

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

#write(s) ⇒ Object



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

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