Class: Aspera::Transfer::FauxFile

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

Overview

Generates a pseudo file stream.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, size) ⇒ FauxFile

Returns a new instance of FauxFile.

Parameters:

  • path (String) —

    virtual file path (from the faux URI)

  • size (Integer) —

    total number of bytes to produce



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)

Returns:

  • (String) —

    virtual file path

  • (Integer) —

    total size in bytes



36
37
38
# File 'lib/aspera/transfer/faux_file.rb', line 36

def path
  @path
end

#size ⇒ String, Integer (readonly)

Returns:

  • (String) —

    virtual file path

  • (Integer) —

    total size in bytes



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)

Parameters:

  • name (String) —

    source file name, possibly a faux URI

Returns:

  • (FauxFile, nil) —

    nil if not a faux scheme, else a FauxFile instance



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.

Returns:

  • (nil)


62
63
# File 'lib/aspera/transfer/faux_file.rb', line 62

def close
end

#eof? ⇒ Boolean

Returns true when all bytes have been read.

Returns:

  • (Boolean) —

    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.

Parameters:

  • chunk_size (Integer) —

    maximum number of bytes to read

Returns:

  • (String, nil) —

    null-byte string of the bytes actually read, or nil at EOF



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