class Seahorse::Client::Plugins::ReadCallbackIO

@api private

def self.readpartial(*args)

def self.readpartial(*args)
  @io.readpartial(*args).tap do |chunk|
    handle_chunk(chunk)
  end
end

def handle_chunk(chunk)

def handle_chunk(chunk)
  @bytes_read += chunk.bytesize if chunk && chunk.respond_to?(:bytesize)
  total_size = @io.respond_to?(:size) ? @io.size : nil
  @on_read.call(chunk, @bytes_read, total_size) if @on_read
end

def initialize(io, on_read = nil)

def initialize(io, on_read = nil)
  @io = io
  @on_read = on_read if on_read.is_a? Proc
  @bytes_read = 0
  # Some IO objects support readpartial - IO.copy_stream used by the
  # request will call readpartial if available, so define a wrapper
  # for it if the underlying IO supports it.
  if @io.respond_to?(:readpartial)
    def self.readpartial(*args)
      @io.readpartial(*args).tap do |chunk|
        handle_chunk(chunk)
      end
    end
  end
end

def read(*args)

def read(*args)
  @io.read(*args).tap do |chunk|
    handle_chunk(chunk)
  end
end