class Protocol::HTTP::Body::File

Constants

BLOCK_SIZE
MODE

Attributes

file[R]
length[R]
offset[R]

Public Class Methods

new(file, range = nil, size: file.size, block_size: BLOCK_SIZE) click to toggle source
# 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
open(path, *arguments, **options) click to toggle source
# File lib/protocol/http/body/file.rb, line 33
def self.open(path, *arguments, **options)
        self.new(::File.open(path, MODE), *arguments, **options)
end

Public Instance Methods

close(error = nil) click to toggle source
Calls superclass method
# File lib/protocol/http/body/file.rb, line 52
def close(error = nil)
        @file.close
        @remaining = 0
        
        super
end
empty?() click to toggle source
# File lib/protocol/http/body/file.rb, line 64
def empty?
        @remaining == 0
end
inspect() click to toggle source
# File lib/protocol/http/body/file.rb, line 98
def inspect
        "\#<#{self.class} file=#{@file.inspect} offset=#{@offset} remaining=#{@remaining}>"
end
join() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/protocol/http/body/file.rb, line 68
def ready?
        true
end
rewind() click to toggle source
# File lib/protocol/http/body/file.rb, line 72
def rewind
        @file.seek(@offset)
end