Sha256: cfb482a56395990da7c1682f82480360c17e0ab66c398b883f9c9949ba222604

Contents?: true

Size: 1.71 KB

Versions: 27

Compression:

Stored size: 1.71 KB

Contents

module Aws
  module S3

    # A utility class that provides an IO-like interface to a portion of a file
    # on disk.
    # @api private
    class FilePart

      # @option options [required, String, Pathname, File, Tempfile] :source
      #   The file to upload.
      #
      # @option options [required, Integer] :offset The file part will read
      #   starting at this byte offset.
      #
      # @option options [required, Integer] :size The maximum number of bytes to
      #   read from the `:offset`.
      def initialize(options = {})
        @source = options[:source]
        @first_byte = options[:offset]
        @last_byte = @first_byte + options[:size]
        @size = options[:size]
        @file = nil
      end

      # @return [String, Pathname, File, Tempfile]
      attr_reader :source

      # @return [Integer]
      attr_reader :first_byte

      # @return [Integer]
      attr_reader :last_byte

      # @return [Integer]
      attr_reader :size

      def read(bytes = nil, output_buffer = nil)
        open_file unless @file
        read_from_file(bytes, output_buffer)
      end

      def rewind
        if @file
          @file.seek(@first_byte)
          @position = @first_byte
        end
        0
      end

      def close
        @file.close if @file
      end

      private

      def open_file
        @file = File.open(@source, 'rb')
        rewind
      end

      def read_from_file(bytes, output_buffer)
        length = [remaining_bytes, *bytes].min
        data   = @file.read(length, output_buffer)

        @position += data ? data.bytesize : 0

        data.to_s unless bytes && (data.nil? || data.empty?)
      end

      def remaining_bytes
        @last_byte - @position
      end

    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
aws-sdk-s3-1.68.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.67.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.67.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.66.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.65.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.64.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.63.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.63.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.62.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.61.2 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.61.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.61.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.60.2 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.60.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.60.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.59.1 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.59.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.58.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.57.0 lib/aws-sdk-s3/file_part.rb
aws-sdk-s3-1.56.0 lib/aws-sdk-s3/file_part.rb