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.

[View source]

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

[View source]

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

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

[View source]

65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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
		$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

#rewindObject

[View source]

93
94
95
96
# File 'lib/s3sync/HTTPStreaming.rb', line 93

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

#write(s) ⇒ Object

[View source]

79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/s3sync/HTTPStreaming.rb', line 79

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