Class: Aspera::Transfer::FauxFile

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/transfer/faux_file.rb

Overview

generates a pseudo file stream

Constant Summary collapse

PREFIX =

marker for faux file

'faux:///'
SUFFIX =

size suffix

%w[k m g t p e]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, size) ⇒ FauxFile

Returns a new instance of FauxFile.



28
29
30
31
32
33
34
# File 'lib/aspera/transfer/faux_file.rb', line 28

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

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/aspera/transfer/faux_file.rb', line 26

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



26
27
28
# File 'lib/aspera/transfer/faux_file.rb', line 26

def size
  @size
end

Class Method Details

.open(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aspera/transfer/faux_file.rb', line 12

def open(name)
  return nil unless name.start_with?(PREFIX)
  parts = name[PREFIX.length..-1].split('?')
  raise 'Format: #{PREFIX}<file path>?<size>' unless parts.length.eql?(2)
  raise "Format: <integer>[#{SUFFIX.join(',')}]" unless (m = 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(parts[0], size)
end

Instance Method Details

#closeObject



44
45
# File 'lib/aspera/transfer/faux_file.rb', line 44

def close
end

#eof?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/aspera/transfer/faux_file.rb', line 47

def eof?
  return @offset >= @size
end

#read(chunk_size) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/aspera/transfer/faux_file.rb', line 36

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