Class: Protocol::HTTP::Body::File
Constant Summary
collapse
- BLOCK_SIZE =
Async::IO::BLOCK_SIZE
- MODE =
::File::RDONLY | ::File::BINARY
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Readable
#call, #each, #finish, #stream?
Constructor Details
#initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE) ⇒ File
Returns a new instance of File.
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/protocol/http/body/file.rb', line 37
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
Returns the value of attribute file.
59
60
61
|
# File 'lib/protocol/http/body/file.rb', line 59
def file
@file
end
|
#length ⇒ Object
Returns the value of attribute length.
62
63
64
|
# File 'lib/protocol/http/body/file.rb', line 62
def length
@length
end
|
#offset ⇒ Object
Returns the value of attribute offset.
61
62
63
|
# File 'lib/protocol/http/body/file.rb', line 61
def offset
@offset
end
|
Class Method Details
.open(path, *arguments, **options) ⇒ Object
33
34
35
|
# File 'lib/protocol/http/body/file.rb', line 33
def self.open(path, *arguments, **options)
self.new(::File.open(path, MODE), *arguments, **options)
end
|
Instance Method Details
#close(error = nil) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/protocol/http/body/file.rb', line 52
def close(error = nil)
@file.close
@remaining = 0
super
end
|
#empty? ⇒ Boolean
64
65
66
|
# File 'lib/protocol/http/body/file.rb', line 64
def empty?
@remaining == 0
end
|
#inspect ⇒ Object
98
99
100
|
# File 'lib/protocol/http/body/file.rb', line 98
def inspect
"\#<#{self.class} file=#{@file.inspect} offset=#{@offset} remaining=#{@remaining}>"
end
|
#join ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/protocol/http/body/file.rb', line 88
def join
return "" if @remaining == 0
buffer = @file.read(@remaining)
@remaining = 0
return buffer
end
|
#read ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/protocol/http/body/file.rb', line 76
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
68
69
70
|
# File 'lib/protocol/http/body/file.rb', line 68
def ready?
true
end
|
#rewind ⇒ Object
72
73
74
|
# File 'lib/protocol/http/body/file.rb', line 72
def rewind
@file.seek(@offset)
end
|