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

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

Overview

A body which reads from a file.

Constant Summary collapse

BLOCK_SIZE =

The default block size.

64*1024
MODE =

The default mode for opening files.

::File::RDONLY | ::File::BINARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#as_json, #call, #discard, #each, #finish, #stream?, #to_json

Constructor Details

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

Initialize the file body with the given file.



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

def initialize(file, range = nil, size: file.size, block_size: BLOCK_SIZE)
  @file = file
  @range = range
  
  @block_size = block_size
  
  if range
    @file.seek(range.min)
    @offset = range.min
    @length = @remaining = range.size
  else
    @file.seek(0)
    @offset = 0
    @length = @remaining = size
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#lengthObject (readonly)

Returns the value of attribute length.



66
67
68
# File 'lib/protocol/http/body/file.rb', line 66

def length
  @length
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

#the number of bytes to read.(numberofbytestoread.) ⇒ Object (readonly)



66
# File 'lib/protocol/http/body/file.rb', line 66

attr :length

#the offset to read from.(offsettoreadfrom.) ⇒ Object (readonly)



63
# File 'lib/protocol/http/body/file.rb', line 63

attr :offset

Class Method Details

.open(path, *arguments, **options) ⇒ Object

Open a file at the given path.



22
23
24
# File 'lib/protocol/http/body/file.rb', line 22

def self.open(path, *arguments, **options)
  self.new(::File.open(path, MODE), *arguments, **options)
end

Instance Method Details

#bufferedObject

Returns a copy of the body, by duplicating the file descriptor, including the same range if specified.



81
82
83
# File 'lib/protocol/http/body/file.rb', line 81

def buffered
  self.class.new(@file.dup, @range, block_size: @block_size)
end

#close(error = nil) ⇒ Object

Close the file.



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



69
70
71
# File 'lib/protocol/http/body/file.rb', line 69

def empty?
  @remaining == 0
end

#file the file to read from.=(thefiletoreadfrom. = (value)) ⇒ Object



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

attr :file

#inspectObject

Inspect the file body.



137
138
139
140
141
142
143
# File 'lib/protocol/http/body/file.rb', line 137

def inspect
  if @offset > 0
    "#<#{self.class} #{@file.inspect} +#{@offset}, #{@remaining} bytes remaining>"
  else
    "#<#{self.class} #{@file.inspect}, #{@remaining} bytes remaining>"
  end
end

#joinObject

Read all the remaining data from the file and return it as a single string.



124
125
126
127
128
129
130
131
132
# File 'lib/protocol/http/body/file.rb', line 124

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

#readObject

Read the next chunk of data from the file.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/protocol/http/body/file.rb', line 99

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



74
75
76
# File 'lib/protocol/http/body/file.rb', line 74

def ready?
  true
end

#rewindObject

Rewind the file to the beginning of the range.



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

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

#rewindable?Boolean



92
93
94
# File 'lib/protocol/http/body/file.rb', line 92

def rewindable?
  true
end