Class: Protocol::HTTP::Body::File
- Defined in:
- lib/protocol/http/body/file.rb
Constant Summary collapse
- BLOCK_SIZE =
4096
- MODE =
::File::RDONLY | ::File::BINARY
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#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(error = nil) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #join ⇒ Object
- #read ⇒ Object
- #ready? ⇒ Boolean
- #rewind ⇒ Object
- #stream? ⇒ Boolean
Methods inherited from Readable
Constructor Details
#initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE) ⇒ File
Returns a new instance of File.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/protocol/http/body/file.rb', line 19 def initialize(file, range = nil, size: file.size, 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 = size end end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
41 42 43 |
# File 'lib/protocol/http/body/file.rb', line 41 def file @file end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
44 45 46 |
# File 'lib/protocol/http/body/file.rb', line 44 def length @length end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
43 44 45 |
# File 'lib/protocol/http/body/file.rb', line 43 def offset @offset end |
Class Method Details
.open(path, *arguments, **options) ⇒ Object
15 16 17 |
# File 'lib/protocol/http/body/file.rb', line 15 def self.open(path, *arguments, **) self.new(::File.open(path, MODE), *arguments, **) end |
Instance Method Details
#close(error = nil) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/protocol/http/body/file.rb', line 34 def close(error = nil) @file.close @remaining = 0 super end |
#empty? ⇒ Boolean
46 47 48 |
# File 'lib/protocol/http/body/file.rb', line 46 def empty? @remaining == 0 end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/protocol/http/body/file.rb', line 85 def inspect "\#<#{self.class} file=#{@file.inspect} offset=#{@offset} remaining=#{@remaining}>" end |
#join ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/protocol/http/body/file.rb', line 75 def join return "" if @remaining == 0 buffer = @file.read(@remaining) @remaining = 0 return buffer end |
#read ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/protocol/http/body/file.rb', line 63 def read if @remaining > 0 amount = [@remaining, @block_size].min if chunk = @file.read(amount) @remaining -= chunk.bytesize return chunk end end end |
#ready? ⇒ Boolean
50 51 52 |
# File 'lib/protocol/http/body/file.rb', line 50 def ready? true end |
#rewind ⇒ Object
54 55 56 57 |
# File 'lib/protocol/http/body/file.rb', line 54 def rewind @file.seek(@offset) @remaining = @length end |
#stream? ⇒ Boolean
59 60 61 |
# File 'lib/protocol/http/body/file.rb', line 59 def stream? false end |