123456789_123456789_123456789_123456789_123456789_

Class: Gem::Net::HTTPResponse::Inflater

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/rubygems/vendor/net-http/lib/net/http/response.rb

Overview

Inflater is a wrapper around ::Gem::Net::BufferedIO that transparently inflates zlib and gzip streams.

Class Method Summary

Instance Method Summary

Constructor Details

.new(socket) ⇒ Inflater

Creates a new Inflater wrapping socket

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 665

def initialize socket
  @socket = socket
  # zlib with automatic gzip detection
  @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
end

Instance Method Details

#bytes_inflated

The number of bytes inflated, used to update the Content-Length of the response.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 683

def bytes_inflated
  @inflate.total_out
end

#finish

Finishes the inflate stream.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 674

def finish
  return if @inflate.total_in == 0
  @inflate.finish
end

#inflate_adapter(dest)

Returns a ::Gem::Net::ReadAdapter that inflates each read chunk into dest.

This allows a large response body to be inflated without storing the entire body in memory.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 693

def inflate_adapter(dest)
  if dest.respond_to?(:set_encoding)
    dest.set_encoding(Encoding::ASCII_8BIT)
  elsif dest.respond_to?(:force_encoding)
    dest.force_encoding(Encoding::ASCII_8BIT)
  end
  block = proc do |compressed_chunk|
    @inflate.inflate(compressed_chunk) do |chunk|
      compressed_chunk.clear
      dest << chunk
    end
  end

  Gem::Net::ReadAdapter.new(block)
end

#read(clen, dest, ignore_eof = false)

Reads clen bytes from the socket, inflates them, then writes them to dest. Gem::Net::HTTPResponse#ignore_eof is passed down to BufferedIO#read

Unlike BufferedIO#read, this method returns more than clen bytes. At this time there is no way for a user of ::Gem::Net::HTTPResponse to read a specific number of bytes from the ::Gem::Net::HTTP response body, so this internal API does not return the same number of bytes as were requested.

See bugs.ruby-lang.org/issues/6492 for further discussion.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 720

def read clen, dest, ignore_eof = false
  temp_dest = inflate_adapter(dest)

  @socket.read clen, temp_dest, ignore_eof
end

#read_all(dest)

Reads the rest of the socket, inflates it, then writes it to dest.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/lib/net/http/response.rb', line 729

def read_all dest
  temp_dest = inflate_adapter(dest)

  @socket.read_all temp_dest
end