Class: HTTPX::Transcoder::Deflater

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/transcoder/utils/deflater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Deflater

Returns a new instance of Deflater.



13
14
15
16
17
# File 'lib/httpx/transcoder/utils/deflater.rb', line 13

def initialize(body)
  @content_type = body.content_type
  @body = BodyReader.new(body)
  @closed = false
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



11
12
13
# File 'lib/httpx/transcoder/utils/deflater.rb', line 11

def content_type
  @content_type
end

Instance Method Details

#bytesizeObject



19
20
21
22
23
# File 'lib/httpx/transcoder/utils/deflater.rb', line 19

def bytesize
  buffer_deflate!

  @buffer.size
end

#closeObject



44
45
46
47
48
49
50
51
52
# File 'lib/httpx/transcoder/utils/deflater.rb', line 44

def close
  return if @closed

  @buffer.close if @buffer

  @body.close

  @closed = true
end

#read(length = nil, outbuf = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/httpx/transcoder/utils/deflater.rb', line 25

def read(length = nil, outbuf = nil)
  return @buffer.read(length, outbuf) if @buffer

  return if @closed

  chunk = @body.read(length)

  compressed_chunk = deflate(chunk)

  return unless compressed_chunk

  if outbuf
    outbuf.clear.force_encoding(Encoding::BINARY)
    outbuf << compressed_chunk
  else
    compressed_chunk
  end
end