Class: Aspera::Transfer::FauxFile
- Inherits:
-
Object
- Object
- Aspera::Transfer::FauxFile
- Defined in:
- lib/aspera/transfer/faux_file.rb
Overview
generates a pseudo file stream
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
-
.create(name) ⇒ Object
Nil if not a faux: scheme, else a FauxFile instance.
Instance Method Summary collapse
- #close ⇒ Object
- #eof? ⇒ Boolean
-
#initialize(path, size) ⇒ FauxFile
constructor
A new instance of FauxFile.
- #read(chunk_size) ⇒ Object
Constructor Details
#initialize(path, size) ⇒ FauxFile
Returns a new instance of FauxFile.
30 31 32 33 34 35 36 |
# File 'lib/aspera/transfer/faux_file.rb', line 30 def initialize(path, size) @path = path @size = size @offset = 0 # we cache large chunks, anyway most of them will be the same size @chunk_by_size = {} end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
28 29 30 |
# File 'lib/aspera/transfer/faux_file.rb', line 28 def path @path end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
28 29 30 |
# File 'lib/aspera/transfer/faux_file.rb', line 28 def size @size end |
Class Method Details
.create(name) ⇒ Object
Returns nil if not a faux: scheme, else a FauxFile instance.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/aspera/transfer/faux_file.rb', line 14 def create(name) return nil unless name.start_with?(PREFIX) url_parts = name[PREFIX.length..-1].split('?') raise 'Format: #{PREFIX}<file path>?<size>' unless url_parts.length.eql?(2) raise "Format: <integer>[#{SUFFIX.join(',')}]" unless (m = url_parts[1].downcase.match(/^(\d+)([#{SUFFIX.join('')}])$/)) size = m[1].to_i suffix = m[2] SUFFIX.each do |s| size *= 1024 break if s.eql?(suffix) end return FauxFile.new(url_parts[0], size) end |
Instance Method Details
#close ⇒ Object
46 47 |
# File 'lib/aspera/transfer/faux_file.rb', line 46 def close end |
#eof? ⇒ Boolean
49 50 51 |
# File 'lib/aspera/transfer/faux_file.rb', line 49 def eof? return @offset >= @size end |
#read(chunk_size) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/aspera/transfer/faux_file.rb', line 38 def read(chunk_size) return nil if eof? bytes_to_read = [chunk_size, @size - @offset].min @offset += bytes_to_read @chunk_by_size[bytes_to_read] = "\x00" * bytes_to_read unless @chunk_by_size.key?(bytes_to_read) return @chunk_by_size[bytes_to_read] end |