Class: Protocol::HTTP::Body::File

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

Constant Summary collapse

BLOCK_SIZE =
4096
MODE =
::File::RDONLY | ::File::BINARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#call, #each, #finish

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

#fileObject (readonly)

Returns the value of attribute file.



41
42
43
# File 'lib/protocol/http/body/file.rb', line 41

def file
  @file
end

#lengthObject (readonly)

Returns the value of attribute length.



44
45
46
# File 'lib/protocol/http/body/file.rb', line 44

def length
  @length
end

#offsetObject (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, **options)
	self.new(::File.open(path, MODE), *arguments, **options)
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

Returns:

  • (Boolean)


46
47
48
# File 'lib/protocol/http/body/file.rb', line 46

def empty?
	@remaining == 0
end

#inspectObject



85
86
87
# File 'lib/protocol/http/body/file.rb', line 85

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

#joinObject



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

#readObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/protocol/http/body/file.rb', line 50

def ready?
	true
end

#rewindObject



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

def rewind
	@file.seek(@offset)
	@remaining = @length
end

#stream?Boolean

Returns:

  • (Boolean)


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

def stream?
	false
end