class AWS::S3::DataOptions::IOProxy

@api private
IO object that responds to #read and #eof.
A utility class that turns a block (with 2 args) into an

def eof?

def eof?
  @eof
end

def initialize write_block

def initialize write_block
  unless write_block.arity == 2
    msg = "a write block must accept 2 yield params: a buffer and "
    msg << "a number of bytes to write"
    raise ArgumentError, msg
  end
  @write_block = write_block
  @eof = false
end

def read bytes = nil, output_buffer = nil

def read bytes = nil, output_buffer = nil
  data = if bytes
    (@eof) ? nil : read_chunk(bytes)
  else
    (@eof) ? ""  : read_all
  end
  output_buffer ? output_buffer.replace(data || '') : data
end

def read_all

def read_all
  buffer = StringIO.new
  buffer << read_chunk(1024 * 1024 * 5) until @eof
  buffer.rewind
  buffer.read
end

def read_chunk bytes

def read_chunk bytes
  buffer = StringIO.new
  @write_block.call(buffer, bytes)
  buffer.rewind
  @eof = true if buffer.size < bytes
  buffer.read
end