Class: OCI::ObjectStorage::Transfer::Multipart::Internal::SeekableNonFilePartIOWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/oci/object_storage/transfer/multipart/internal/seekable_non_file_part_io_wrapper.rb

Overview

A class which wraps a seekable (but non-file) IO-like object (for example, a StringIO) so that we can extract a part of it to upload as an “upload part” of a multipart upload. This works by seeking to a given position in the IO-like object and then reading a certain number of bytes from that position onwards.

Reading a part of the IO-like object is synchronised so that only a single thread can access the IO at any one time. This prevents issues where we would do, for example, concurrent seeks and so potentially end up reading content we didn't intend to read.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:) ⇒ SeekableNonFilePartIOWrapper

Creates a new SeekableNonFilePartIOWrapper

Parameters:

  • source (IO)

    the IO-like object which will back this wrapper



24
25
26
27
28
29
# File 'lib/oci/object_storage/transfer/multipart/internal/seekable_non_file_part_io_wrapper.rb', line 24

def initialize(source:)
  raise 'The provided source is not seekable' unless source.respond_to?(:seek)

  @source = source
  @lock = Mutex.new
end

Instance Attribute Details

#sourceIO (readonly)

The underlying IO-like object which we'll extract content from

Returns:

  • (IO)


20
21
22
# File 'lib/oci/object_storage/transfer/multipart/internal/seekable_non_file_part_io_wrapper.rb', line 20

def source
  @source
end

Instance Method Details

#read(offset, part_size) ⇒ String

Reads a part/segment from the IO-like object by seeking to a given location and then reading a given number of bytes. This is synchronised so that only a single thread can perform this operation at any one time.

Parameters:

  • offset (Integer)

    the zero-based position in the IO-like object where we'll start reading from

  • part_size (Integer)

    the number of bytes to read

Returns:

  • (String)

    The content read from the IO-like object



39
40
41
42
43
44
# File 'lib/oci/object_storage/transfer/multipart/internal/seekable_non_file_part_io_wrapper.rb', line 39

def read(offset, part_size)
  @lock.synchronize do
    @source.seek(offset)
    @source.read(part_size)
  end
end