Sha256: 3dbe335cd6eab0f8966526ff5edeb5d86885fa86d60591e7341dc0fdadc6804b

Contents?: true

Size: 1.74 KB

Versions: 1030

Compression:

Stored size: 1.74 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
      # @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)
        if bytes
          data = @file.read([remaining_bytes, bytes].min)
          data = nil if data == ''
        else
          data = @file.read(remaining_bytes)
        end
        @position += data ? data.bytesize : 0
        output_buffer ? output_buffer.replace(data || '') : data
      end

      def remaining_bytes
        @last_byte - @position
      end

    end
  end
end

Version data entries

1,030 entries across 1,030 versions & 3 rubygems

Version Path
aws-sdk-resources-2.11.592 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.591 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.590 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.589 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.588 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.587 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.586 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.585 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.584 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.583 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.582 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.581 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.580 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.579 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.578 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.577 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.576 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.575 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.574 lib/aws-sdk-resources/services/s3/file_part.rb
aws-sdk-resources-2.11.573 lib/aws-sdk-resources/services/s3/file_part.rb