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, #finish

Constructor Details

#initialize(file, range = nil, block_size: BLOCK_SIZE) ⇒ File

Returns a new instance of File.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/async/http/body/file.rb', line 34

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.



57
58
59
# File 'lib/async/http/body/file.rb', line 57

def length
  @length
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

Class Method Details

.open(path, *args) ⇒ Object



30
31
32
# File 'lib/async/http/body/file.rb', line 30

def self.open(path, *args)
	self.new(::File.open(path), *args)
end

Instance Method Details

#close(error = nil) ⇒ Object



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

def close(error = nil)
	@file.close
	@remaining = 0
	
	super
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@remaining == 0
end

#inspectObject



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

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

#joinObject



79
80
81
82
83
84
85
86
87
# File 'lib/async/http/body/file.rb', line 79

def join
	return "" if @remaining == 0
	
	buffer = @file.read(@remaining)
	
	@remaining = 0
	
	return buffer
end

#readObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/async/http/body/file.rb', line 67

def read
	if @remaining > 0
		amount = [@remaining, @block_size].min
		
		if chunk = @file.read(amount)
			@remaining -= chunk.bytesize
			
			return chunk
		end
	end
end

#rewindObject



63
64
65
# File 'lib/async/http/body/file.rb', line 63

def rewind
	@file.seek(@offset)
end