Class: Rack::Deflater::GzipStream
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rack/deflater.rb |
Overview
Body class used for gzip encoded responses.
Constant Summary
-
BUFFER_LENGTH =
# File 'lib/rack/deflater.rb', line 85128 * 1_024
Class Method Summary
-
.new(body, mtime, sync) ⇒ GzipStream
constructor
Initialize the gzip stream.
Instance Method Summary
-
#close
Close the original body if possible.
-
#each(&block)
Yield gzip compressed strings to the given block.
-
#write(data)
Call the block passed to #each with the gzipped data.
Constructor Details
.new(body, mtime, sync) ⇒ GzipStream
Initialize the gzip stream. Arguments:
- body
-
Response
body to compress with gzip - mtime
-
The modification time of the body, used to set the modification time in the gzip header.
- sync
-
Whether to flush each gzip chunk as soon as it is ready.
# File 'lib/rack/deflater.rb', line 92
def initialize(body, mtime, sync) @body = body @mtime = mtime @sync = sync end
Instance Method Details
#close
Close the original body if possible.
# File 'lib/rack/deflater.rb', line 128
def close @body.close if @body.respond_to?(:close) end
#each(&block)
Yield gzip compressed strings to the given block.
# File 'lib/rack/deflater.rb', line 99
def each(&block) @writer = block gzip = ::Zlib::GzipWriter.new(self) gzip.mtime = @mtime if @mtime # @body.each is equivalent to @body.gets (slow) if @body.is_a? ::File # XXX: Should probably be ::IO while part = @body.read(BUFFER_LENGTH) gzip.write(part) gzip.flush if @sync end else @body.each { |part| # Skip empty strings, as they would result in no output, # and flushing empty parts would raise Zlib::BufError. next if part.empty? gzip.write(part) gzip.flush if @sync } end ensure gzip.finish end
#write(data)
Call the block passed to #each with the gzipped data.
# File 'lib/rack/deflater.rb', line 123
def write(data) @writer.call(data) end