Class: Async::HTTP::Body::File
- Defined in:
- lib/async/http/body/file.rb
Constant Summary collapse
- BLOCK_SIZE =
Async::IO::Stream::BLOCK_SIZE
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(file, range = nil, block_size: BLOCK_SIZE) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #join ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
Methods inherited from Readable
Constructor Details
#initialize(file, range = nil, block_size: BLOCK_SIZE) ⇒ File
Returns a new instance of File.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/async/http/body/file.rb', line 33 def initialize(file, range = nil, block_size: BLOCK_SIZE) @file = file @block_size = block_size if range @file.seek(range.min) @offset = range.min @length = @remaining = range.size else @offset = 0 @length = @remaining = @file.size end end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
49 50 51 |
# File 'lib/async/http/body/file.rb', line 49 def length @length end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
48 49 50 |
# File 'lib/async/http/body/file.rb', line 48 def offset @offset end |
Class Method Details
.open(path, *args) ⇒ Object
29 30 31 |
# File 'lib/async/http/body/file.rb', line 29 def self.open(path, *args) self.new(::File.open(path), *args) end |
Instance Method Details
#close ⇒ Object
59 60 61 62 |
# File 'lib/async/http/body/file.rb', line 59 def close @file.close @remaining = 0 end |
#empty? ⇒ Boolean
51 52 53 |
# File 'lib/async/http/body/file.rb', line 51 def empty? @remaining == 0 end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/async/http/body/file.rb', line 88 def inspect "\#<#{self.class} file=#{@file.inspect} offset=#{@offset} remaining=#{@remaining}>" end |
#join ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/async/http/body/file.rb', line 78 def join return "" if @remaining == 0 buffer = @file.read(@remaining) @remaining = 0 return buffer end |
#read ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/async/http/body/file.rb', line 64 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 |
#rewind ⇒ Object
55 56 57 |
# File 'lib/async/http/body/file.rb', line 55 def rewind @file.seek(@offset) end |