Class: Defile::Backend::S3::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/defile/backend/s3.rb

Overview

Emulates an IO-object like interface on top of S3Object#read. To avoid memory allocations and unnecessary complexity, this treats the ‘length` parameter to read as a boolean flag instead. If given, it will read the file in chunks of undetermined size, if not given it will read the entire file.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
# File 'lib/defile/backend/s3.rb', line 12

def initialize(object)
  @object = object
  @closed = false
end

Instance Method Details

#closeObject



44
45
46
# File 'lib/defile/backend/s3.rb', line 44

def close
  @closed = true
end

#eof?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/defile/backend/s3.rb', line 33

def eof?
  @peek ||= enumerator.next
  false
rescue StopIteration
  true
end

#read(length = nil, buffer = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/defile/backend/s3.rb', line 17

def read(length = nil, buffer = nil)
  result = if length
    raise "closed" if @closed

    unless eof? # sets @peek
      @peek
    end
  else
    @object.read
  end
  buffer.replace(result) if buffer and result
  result
ensure
  @peek = nil
end

#sizeObject



40
41
42
# File 'lib/defile/backend/s3.rb', line 40

def size
  @object.content_length
end