class AWS::S3::DataOptions::IOProxy

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

Public Class Methods

new(write_block) click to toggle source
# File lib/aws/s3/data_options.rb, line 147
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

Public Instance Methods

eof?() click to toggle source
# File lib/aws/s3/data_options.rb, line 166
def eof?
  @eof
end
read(bytes = nil, output_buffer = nil) click to toggle source
# File lib/aws/s3/data_options.rb, line 157
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

Protected Instance Methods

read_all() click to toggle source
# File lib/aws/s3/data_options.rb, line 180
def read_all
  buffer = StringIO.new
  buffer << read_chunk(1024 * 1024 * 5) until @eof
  buffer.rewind
  buffer.read
end
read_chunk(bytes) click to toggle source
# File lib/aws/s3/data_options.rb, line 172
def read_chunk bytes
  buffer = StringIO.new
  @write_block.call(buffer, bytes)
  buffer.rewind
  @eof = true if buffer.size < bytes
  buffer.read
end