Class: Async::HTTP::Body::File

Inherits:
Readable
  • Object
show all
Defined in:
lib/async/http/body/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#close, #each, #stop

Constructor Details

#initialize(path, range = nil, block_size: Async::IO::Stream::BLOCK_SIZE) ⇒ File

Returns a new instance of File.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/async/http/body/file.rb', line 27

def initialize(path, range = nil, block_size: Async::IO::Stream::BLOCK_SIZE)
  @path = path
  @file = File.open(path)
  
  @block_size = block_size
  
  if range
    @offset = range.min
    @length = @remaining = range.size
  else
    @offset = 0
    @length = @remaining = @file.size
  end
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



42
43
44
# File 'lib/async/http/body/file.rb', line 42

def length
  @length
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/async/http/body/file.rb', line 44

def empty?
  @remaining == 0
end

#inspectObject



70
71
72
# File 'lib/async/http/body/file.rb', line 70

def inspect
  "\#<#{self.class} path=#{@path} offset=#{@offset} remaining=#{@remaining}>"
end

#joinObject



62
63
64
65
66
67
68
# File 'lib/async/http/body/file.rb', line 62

def join
  buffer = @file.read(@remaining)
  
  @remaining = 0
  
  return buffer
end

#readObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/http/body/file.rb', line 48

def read
  if @remaining > 0
    amount = [@remaining, @block_size].min
    
    if chunk = @file.read(amount)
      @remaining -= chunk.bytesize
      
      return chunk
    else
      @file.close
    end
  end
end