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

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

Constant Summary collapse

BLOCK_SIZE =
Async::IO::Stream::BLOCK_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#each, #stop

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

#lengthObject (readonly)

Returns the value of attribute length.



49
50
51
# File 'lib/async/http/body/file.rb', line 49

def length
  @length
end

#offsetObject (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

#closeObject



59
60
61
62
# File 'lib/async/http/body/file.rb', line 59

def close
	@file.close
	@remaining = 0
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/async/http/body/file.rb', line 51

def empty?
	@remaining == 0
end

#inspectObject



88
89
90
# File 'lib/async/http/body/file.rb', line 88

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

#joinObject



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

#readObject



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

#rewindObject



55
56
57
# File 'lib/async/http/body/file.rb', line 55

def rewind
	@file.seek(@offset)
end