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.
31 32 33 34 35 36 37 |
# File 'lib/aspera/transfer/faux_file.rb', line 31 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.
29 30 31 |
# File 'lib/aspera/transfer/faux_file.rb', line 29 def path @path end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
29 30 31 |
# File 'lib/aspera/transfer/faux_file.rb', line 29 def size @size end |
Class Method Details
.create(name) ⇒ Object
Returns nil if not a faux: scheme, else a FauxFile instance.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aspera/transfer/faux_file.rb', line 15 def create(name) return unless name.start_with?(PREFIX) name_params = name[PREFIX.length..-1].split('?', 2) raise Error, 'Format: #{PREFIX}<file path>?<size>' unless name_params.length.eql?(2) raise Error, "Format: <integer>[#{SIZE_UNITS.join(',')}]" unless (m = name_params[1].downcase.match(/^(\d+)([#{SIZE_UNITS.join('')}])$/)) size = m[1].to_i suffix = m[2] SIZE_UNITS.each do |s| size *= 1024 break if s.eql?(suffix) end return FauxFile.new(name_params[0], size) end |
Instance Method Details
#close ⇒ Object
47 48 |
# File 'lib/aspera/transfer/faux_file.rb', line 47 def close end |
#eof? ⇒ Boolean
50 51 52 |
# File 'lib/aspera/transfer/faux_file.rb', line 50 def eof? return @offset >= @size end |
#read(chunk_size) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/aspera/transfer/faux_file.rb', line 39 def read(chunk_size) return 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 |