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. @private

Public Class Methods

new(write_block) click to toggle source
# File lib/aws/s3/data_options.rb, line 148
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 170
def eof?
  @eof
end
read(bytes = nil) click to toggle source
# File lib/aws/s3/data_options.rb, line 158
def read bytes = nil
  if bytes
    buffer = StringIO.new
    @write_block.call(buffer, bytes)
    buffer.rewind
    @eof = true if buffer.size < bytes
    buffer.size == 0 ? nil : buffer.read
  else
    read_all
  end
end

Protected Instance Methods

read_all() click to toggle source
# File lib/aws/s3/data_options.rb, line 176
def read_all
  buffer = StringIO.new
  buffer << read(1024 * 1024 * 5) until eof?
  buffer.rewind
  buffer.read
end