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 ⇒ String, Integer readonly
- #size ⇒ String, Integer readonly
Class Method Summary collapse
-
.create(name) ⇒ FauxFile?
Parse a faux URI and return a
FauxFileinstance, ornilif the URI does not use the faux scheme.
Instance Method Summary collapse
-
#close ⇒ nil
No-op: required to satisfy the IO-like interface used by transfer agents.
-
#eof? ⇒ Boolean
True when all bytes have been read.
-
#initialize(path, size) ⇒ FauxFile
constructor
A new instance of FauxFile.
-
#read(chunk_size) ⇒ String?
Read up to
chunk_sizebytes from the stream and advance the internal offset.
Constructor Details
#initialize(path, size) ⇒ FauxFile
Returns a new instance of FauxFile.
40 41 42 43 44 45 46 |
# File 'lib/aspera/transfer/faux_file.rb', line 40 def initialize(path, size) @path = path @size = size @offset = 0 # cache chunks by size so repeated reads of the same chunk length reuse the same buffer @chunk_by_size = {} end |
Instance Attribute Details
#path ⇒ String, Integer (readonly)
36 37 38 |
# File 'lib/aspera/transfer/faux_file.rb', line 36 def path @path end |
#size ⇒ String, Integer (readonly)
36 37 38 |
# File 'lib/aspera/transfer/faux_file.rb', line 36 def size @size end |
Class Method Details
.create(name) ⇒ FauxFile?
Parse a faux URI and return a FauxFile instance, or nil if the URI does not use the faux scheme.
URI format: faux:///<path>?<size>, where <size> is a decimal integer with an optional
case-insensitive unit suffix: k, m, g, t, p, e (powers of 1024).
When no suffix is given, the size is interpreted as raw bytes.
Examples: faux:///file.bin?10 (10 bytes), faux:///file.bin?10m (10 MiB)
24 25 26 27 28 29 30 31 32 |
# File 'lib/aspera/transfer/faux_file.rb', line 24 def create(name) return unless name.start_with?(PREFIX) name_params = name.delete_prefix(PREFIX).split('?', 2) Aspera.assert(name_params.length.eql?(2), type: ParameterError) { "Format: #{PREFIX}<file path>?<size>" } m = name_params[1].downcase.match(/^(\d+)([#{SIZE_UNITS.join('')}]?)$/) Aspera.assert(m, type: ParameterError) { "Format: <integer>[#{SIZE_UNITS.join(',')}]" } size = m[2].empty? ? m[1].to_i : m[1].to_i * (1024**(SIZE_UNITS.index(m[2]) + 1)) return FauxFile.new(name_params[0], size) end |
Instance Method Details
#close ⇒ nil
No-op: required to satisfy the IO-like interface used by transfer agents.
62 63 |
# File 'lib/aspera/transfer/faux_file.rb', line 62 def close end |
#eof? ⇒ Boolean
Returns true when all bytes have been read.
66 67 68 |
# File 'lib/aspera/transfer/faux_file.rb', line 66 def eof? return @offset >= @size end |
#read(chunk_size) ⇒ String?
Read up to chunk_size bytes from the stream and advance the internal offset.
Returns nil when the stream is exhausted.
52 53 54 55 56 57 58 |
# File 'lib/aspera/transfer/faux_file.rb', line 52 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 return @chunk_by_size[bytes_to_read] end |