Class: Rack::Deflater::DeflateStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/deflater.rb

Constant Summary collapse

DEFLATE_ARGS =
[
  Zlib::DEFAULT_COMPRESSION,
  # drop the zlib header which causes both Safari and IE to choke
  -Zlib::MAX_WBITS,
  Zlib::DEF_MEM_LEVEL,
  Zlib::DEFAULT_STRATEGY
]

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ DeflateStream

Returns a new instance of DeflateStream.



113
114
115
116
# File 'lib/rack/deflater.rb', line 113

def initialize(body)
  @body = body
  @closed = false
end

Instance Method Details

#closeObject



127
128
129
130
131
# File 'lib/rack/deflater.rb', line 127

def close
  return if @closed
  @closed = true
  @body.close if @body.respond_to?(:close)
end

#eachObject



118
119
120
121
122
123
124
125
# File 'lib/rack/deflater.rb', line 118

def each
  deflator = ::Zlib::Deflate.new(*DEFLATE_ARGS)
  @body.each { |part| yield deflator.deflate(part, Zlib::SYNC_FLUSH) }
  yield fin = deflator.finish
ensure
  deflator.finish unless fin
  deflator.close
end