Class: Async::Aws::Handler::StreamingBody

Inherits:
Protocol::HTTP::Body::Readable
  • Object
show all
Defined in:
lib/async/aws/handler.rb

Constant Summary collapse

CHUNK_SIZE =
16 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(io, size: nil, max_buffer: nil) ⇒ void

Parameters:

  • io (#read, #rewind, #size, nil)
  • size (Integer, nil) (defaults to: nil)
  • max_buffer (Integer, nil) (defaults to: nil)


43
44
45
46
47
# File 'lib/async/aws/handler.rb', line 43

def initialize(io, size: nil, max_buffer: nil)
  @io = io
  @size = size
  @max_buffer = max_buffer
end

Instance Method Details

#buffered ⇒ Protocol::HTTP::Body::Readable?

Returns:

  • (Protocol::HTTP::Body::Readable, nil)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/async/aws/handler.rb', line 82

def buffered
  return nil unless rewindable?

  original_pos = @io.pos if @io.respond_to?(:pos)
  @io.rewind
  content = +''
  begin
    loop do
      chunk = read_chunk
      break if chunk.nil? || chunk.empty?

      if @max_buffer && (content.bytesize + chunk.bytesize) > @max_buffer
        raise BodyTooLargeError, "async_http_max_buffer_bytes exceeded (#{content.bytesize + chunk.bytesize} > #{@max_buffer})"
      end
      content << chunk
    end
  ensure
    if original_pos && @io.respond_to?(:pos=)
      @io.pos = original_pos
    else
      @io.rewind
    end
  end
  Protocol::HTTP::Body::Buffered.new([content], content.bytesize)
end

#close(error = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • error (Exception, nil) (defaults to: nil)


77
78
79
# File 'lib/async/aws/handler.rb', line 77

def close(error = nil)
  nil
end

#length ⇒ Integer?

Returns:

  • (Integer, nil)


50
51
52
# File 'lib/async/aws/handler.rb', line 50

def length
  @size
end

#read ⇒ String?

Returns:

  • (String, nil)


68
69
70
71
72
73
# File 'lib/async/aws/handler.rb', line 68

def read
  chunk = read_chunk
  return nil if chunk.nil? || chunk.empty?

  chunk
end

#rewind ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/async/aws/handler.rb', line 60

def rewind
  return false unless rewindable?

  @io.rewind
  true
end

#rewindable? ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/async/aws/handler.rb', line 55

def rewindable?
  @io.respond_to?(:rewind)
end